Skip to content

Commit f835058

Browse files
committed
Using acceptor::close instead of acceptor::cancel
As raised by vusak and as documented by Boost.Asio the recommended way for stopping all acceptor operations is to use close instead of cancel.
1 parent 7b88441 commit f835058

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

boost/network/protocol/http/server/async_server.hpp

+32-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// (See accompanying file LICENSE_1_0.txt or copy at
77
// http://www.boost.org/LICENSE_1_0.txt)
88

9+
#include <boost/network/detail/debug.hpp>
910
#include <boost/network/protocol/http/server/async_connection.hpp>
1011
#include <boost/thread/mutex.hpp>
1112
#include <boost/network/protocol/http/server/storage_base.hpp>
@@ -56,12 +57,17 @@ namespace boost { namespace network { namespace http {
5657
// handlers finish.
5758
stopping = true;
5859
system::error_code ignored;
59-
acceptor.cancel(ignored);
60+
acceptor.close(ignored);
6061
}
6162

6263
void listen() {
6364
boost::unique_lock<boost::mutex> listening_lock(listening_mutex_);
65+
BOOST_NETWORK_MESSAGE("Listening on " << address_ << ':' << port_);
6466
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+
}
6571
}
6672

6773
private:
@@ -94,33 +100,50 @@ namespace boost { namespace network { namespace http {
94100
)
95101
);
96102
}
103+
} else {
104+
BOOST_NETWORK_MESSAGE("Error accepting connection, reason: " << ec);
97105
}
98106
}
99-
107+
100108
void start_listening() {
101109
using boost::asio::ip::tcp;
102-
tcp::resolver resolver(service_);
103-
tcp::resolver::query query(address_, port_);
104-
tcp::endpoint endpoint = *resolver.resolve(query);
105110

106111
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;
107120
acceptor.open(endpoint.protocol(), error);
108-
if (error) return;
121+
if (error) {
122+
BOOST_NETWORK_MESSAGE("Error opening socket: " << address_ << ":" << port_);
123+
return;
124+
}
109125
acceptor.bind(endpoint, error);
110-
if (error) return;
126+
if (error) {
127+
BOOST_NETWORK_MESSAGE("Error binding socket: " << address_ << ":" << port_);
128+
return;
129+
}
111130
socket_options_base::acceptor_options(acceptor);
112131
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+
}
114136
new_connection.reset(new connection(service_, handler, thread_pool));
115137
acceptor.async_accept(new_connection->socket(),
116138
boost::bind(
117139
&async_server_base<Tag,Handler>::handle_accept
118140
, this
119141
, boost::asio::placeholders::error));
120142
listening = true;
143+
BOOST_NETWORK_MESSAGE("Now listening on socket: '" << address_ << ":" << port_ << "'");
121144
}
122145
};
123-
146+
124147
} /* http */
125148

126149
} /* network */

boost/network/protocol/http/server/sync_server.hpp

+24-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_SYNC_SERVER_HPP_20101025
99
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_SYNC_SERVER_HPP_20101025
1010

11+
#include <boost/network/detail/debug.hpp>
1112
#include <boost/shared_ptr.hpp>
1213
#include <boost/bind.hpp>
1314
#include <boost/asio/ip/tcp.hpp>
@@ -58,7 +59,7 @@ namespace boost { namespace network { namespace http {
5859
void stop() {
5960
// stop accepting new connections and let all the existing handlers finish.
6061
system::error_code ignored;
61-
acceptor_.cancel(ignored);
62+
acceptor_.close(ignored);
6263
service_.stop();
6364
}
6465

@@ -89,13 +90,31 @@ namespace boost { namespace network { namespace http {
8990

9091
void start_listening() {
9192
using boost::asio::ip::tcp;
93+
system::error_code error;
9294
tcp::resolver resolver(service_);
9395
tcp::resolver::query query(address_, port_);
94-
tcp::endpoint endpoint = *resolver.resolve(query);
95-
acceptor_.open(endpoint.protocol());
96+
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
97+
if (error) {
98+
BOOST_NETWORK_MESSAGE("Error resolving address: " << address_ << ':' << port_);
99+
return;
100+
}
101+
tcp::endpoint endpoint = *endpoint_iterator;
102+
acceptor_.open(endpoint.protocol(), error);
103+
if (error) {
104+
BOOST_NETWORK_MESSAGE("Error opening socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
105+
return;
106+
}
96107
socket_options_base::acceptor_options(acceptor_);
97-
acceptor_.bind(endpoint);
98-
acceptor_.listen();
108+
acceptor_.bind(endpoint, error);
109+
if (error) {
110+
BOOST_NETWORK_MESSAGE("Error binding to socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
111+
return;
112+
}
113+
acceptor_.listen(tcp::socket::max_connections, error);
114+
if (error) {
115+
BOOST_NETWORK_MESSAGE("Error listening on socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
116+
return;
117+
}
99118
new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));
100119
acceptor_.async_accept(new_connection->socket(),
101120
boost::bind(&sync_server_base<Tag,Handler>::handle_accept,

0 commit comments

Comments
 (0)