|
6 | 6 | // (See accompanying file LICENSE_1_0.txt or copy at |
7 | 7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | 8 |
|
| 9 | +#include <boost/network/detail/debug.hpp> |
9 | 10 | #include <boost/network/protocol/http/server/async_connection.hpp> |
10 | 11 | #include <boost/thread/mutex.hpp> |
11 | 12 | #include <boost/network/protocol/http/server/storage_base.hpp> |
@@ -56,12 +57,17 @@ namespace boost { namespace network { namespace http { |
56 | 57 | // handlers finish. |
57 | 58 | stopping = true; |
58 | 59 | system::error_code ignored; |
59 | | - acceptor.cancel(ignored); |
| 60 | + acceptor.close(ignored); |
60 | 61 | } |
61 | 62 |
|
62 | 63 | void listen() { |
63 | 64 | boost::unique_lock<boost::mutex> listening_lock(listening_mutex_); |
| 65 | + BOOST_NETWORK_MESSAGE("Listening on " << address_ << ':' << port_); |
64 | 66 | if (!listening) start_listening(); |
| 67 | + if (!listening) { |
| 68 | + BOOST_NETWORK_MESSAGE("Error listening on " << address_ << ':' << port_); |
| 69 | + boost::throw_exception(std::runtime_error("Error listening on provided port.")); |
| 70 | + } |
65 | 71 | } |
66 | 72 |
|
67 | 73 | private: |
@@ -94,33 +100,50 @@ namespace boost { namespace network { namespace http { |
94 | 100 | ) |
95 | 101 | ); |
96 | 102 | } |
| 103 | + } else { |
| 104 | + BOOST_NETWORK_MESSAGE("Error accepting connection, reason: " << ec); |
97 | 105 | } |
98 | 106 | } |
99 | | - |
| 107 | + |
100 | 108 | void start_listening() { |
101 | 109 | using boost::asio::ip::tcp; |
102 | | - tcp::resolver resolver(service_); |
103 | | - tcp::resolver::query query(address_, port_); |
104 | | - tcp::endpoint endpoint = *resolver.resolve(query); |
105 | 110 |
|
106 | 111 | system::error_code error; |
| 112 | + tcp::resolver resolver(service_); |
| 113 | + tcp::resolver::query query(address_, port_); |
| 114 | + tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error); |
| 115 | + if (error) { |
| 116 | + BOOST_NETWORK_MESSAGE("Error resolving '" << address_ << ':' << port_); |
| 117 | + return; |
| 118 | + } |
| 119 | + tcp::endpoint endpoint = *endpoint_iterator; |
107 | 120 | acceptor.open(endpoint.protocol(), error); |
108 | | - if (error) return; |
| 121 | + if (error) { |
| 122 | + BOOST_NETWORK_MESSAGE("Error opening socket: " << address_ << ":" << port_); |
| 123 | + return; |
| 124 | + } |
109 | 125 | acceptor.bind(endpoint, error); |
110 | | - if (error) return; |
| 126 | + if (error) { |
| 127 | + BOOST_NETWORK_MESSAGE("Error binding socket: " << address_ << ":" << port_); |
| 128 | + return; |
| 129 | + } |
111 | 130 | socket_options_base::acceptor_options(acceptor); |
112 | 131 | acceptor.listen(asio::socket_base::max_connections, error); |
113 | | - if (error) return; |
| 132 | + if (error) { |
| 133 | + BOOST_NETWORK_MESSAGE("Error listening on socket: '" << error << "' on " << address_ << ":" << port_); |
| 134 | + return; |
| 135 | + } |
114 | 136 | new_connection.reset(new connection(service_, handler, thread_pool)); |
115 | 137 | acceptor.async_accept(new_connection->socket(), |
116 | 138 | boost::bind( |
117 | 139 | &async_server_base<Tag,Handler>::handle_accept |
118 | 140 | , this |
119 | 141 | , boost::asio::placeholders::error)); |
120 | 142 | listening = true; |
| 143 | + BOOST_NETWORK_MESSAGE("Now listening on socket: '" << address_ << ":" << port_ << "'"); |
121 | 144 | } |
122 | 145 | }; |
123 | | - |
| 146 | + |
124 | 147 | } /* http */ |
125 | 148 |
|
126 | 149 | } /* network */ |
|
0 commit comments