Skip to content

Commit 22442ec

Browse files
committed
Expose OS-chosen address and port from async_server
1 parent 92d41b7 commit 22442ec

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ struct async_server_base : server_storage_base, socket_options_base {
3535
/// Defines the type for the connection pointer.
3636
typedef std::shared_ptr<connection> connection_ptr;
3737

38+
/// Defines the type for the options.
39+
typedef server_options<Tag, Handler> options;
40+
3841
/// Constructs and initializes the asynchronous server core.
39-
explicit async_server_base(server_options<Tag, Handler> const &options)
42+
explicit async_server_base(options const &options)
4043
: server_storage_base(options),
4144
socket_options_base(options),
4245
handler(options.handler()),
4346
address_(options.address()),
4447
port_(options.port()),
48+
protocol_family(options.protocol_family()),
4549
thread_pool(options.thread_pool()
4650
? options.thread_pool()
4751
: std::make_shared<utils::thread_pool>()),
@@ -108,11 +112,19 @@ struct async_server_base : server_storage_base, socket_options_base {
108112
}
109113
}
110114

115+
/// Returns the server socket address, either IPv4 or IPv6 depending on
116+
/// options.protocol_family()
117+
const string_type& address() const { return address_; }
118+
119+
/// Returns the server socket port
120+
const string_type& port() const { return port_; }
121+
111122
private:
112123
typedef std::unique_lock<std::mutex> scoped_mutex_lock;
113124

114125
Handler &handler;
115126
string_type address_, port_;
127+
typename options::protocol_family_t protocol_family;
116128
std::shared_ptr<utils::thread_pool> thread_pool;
117129
boost::asio::ip::tcp::acceptor acceptor;
118130
bool stopping;
@@ -165,7 +177,15 @@ struct async_server_base : server_storage_base, socket_options_base {
165177
// this allows repeated cycles of run -> stop -> run
166178
service_.reset();
167179
tcp::resolver resolver(service_);
168-
tcp::resolver::query query(address_, port_);
180+
tcp::resolver::query query( [&]{
181+
switch(protocol_family) {
182+
case options::ipv4:
183+
return tcp::resolver::query(tcp::v4(), address_, port_);
184+
case options::ipv6:
185+
return tcp::resolver::query(tcp::v6(), address_, port_);
186+
default:
187+
return tcp::resolver::query(address_, port_);
188+
}}());
169189
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
170190
if (error) {
171191
BOOST_NETWORK_MESSAGE("Error resolving '" << address_ << ':' << port_);
@@ -185,6 +205,8 @@ struct async_server_base : server_storage_base, socket_options_base {
185205
<< port_);
186206
return;
187207
}
208+
address_ = acceptor.local_endpoint().address().to_string();
209+
port_ = std::to_string(acceptor.local_endpoint().port());
188210
acceptor.listen(boost::asio::socket_base::max_connections, error);
189211
if (error) {
190212
BOOST_NETWORK_MESSAGE("Error listening on socket: '"

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// http://www.boost.org/LICENSE_1_0.txt)
1010

1111
#include <memory>
12+
#include <boost/asio/ip/tcp.hpp>
1213
#include <boost/asio/io_service.hpp>
1314
#include <boost/asio/socket_base.hpp>
1415
#include <boost/network/protocol/stream_handler.hpp>
@@ -34,6 +35,7 @@ struct server_options {
3435
handler_(handler),
3536
address_("localhost"),
3637
port_("80"),
38+
protocol_family_(undefined),
3739
reuse_address_(false),
3840
report_aborted_(false),
3941
non_blocking_io_(true),
@@ -88,6 +90,14 @@ struct server_options {
8890
return *this;
8991
}
9092

93+
enum protocol_family_t { ipv4, ipv6, undefined };
94+
95+
/// Set the protocol family for address resolving. Default is AF_UNSPEC.
96+
server_options &protocol_family(protocol_family_t v) {
97+
protocol_family_ = v;
98+
return *this;
99+
}
100+
91101
/// Set whether to reuse the address (SO_REUSE_ADDR). Default is false.
92102
server_options &reuse_address(bool v) {
93103
reuse_address_ = v;
@@ -159,6 +169,9 @@ struct server_options {
159169
/// Returns the port to listen on.
160170
string_type port() const { return port_; }
161171

172+
/// Returns the protocol family used for address resolving.
173+
protocol_family_t protocol_family() const { return protocol_family_; }
174+
162175
/// Returns a reference to the provided handler.
163176
Handler &handler() const { return handler_; }
164177

@@ -215,6 +228,7 @@ struct server_options {
215228
swap(io_service_, other.io_service_);
216229
swap(address_, other.address_);
217230
swap(port_, other.port_);
231+
swap(protocol_family_, other.protocol_family_);
218232
swap(reuse_address_, other.reuse_address_);
219233
swap(report_aborted_, other.report_aborted_);
220234
swap(non_blocking_io_, other.non_blocking_io_);
@@ -233,6 +247,7 @@ struct server_options {
233247
Handler &handler_;
234248
string_type address_;
235249
string_type port_;
250+
protocol_family_t protocol_family_;
236251
bool reuse_address_;
237252
bool report_aborted_;
238253
bool non_blocking_io_;

0 commit comments

Comments
 (0)