@@ -35,13 +35,17 @@ struct async_server_base : server_storage_base, socket_options_base {
35
35
// / Defines the type for the connection pointer.
36
36
typedef std::shared_ptr<connection> connection_ptr;
37
37
38
+ // / Defines the type for the options.
39
+ typedef server_options<Tag, Handler> options;
40
+
38
41
// / 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)
40
43
: server_storage_base(options),
41
44
socket_options_base(options),
42
45
handler(options.handler()),
43
46
address_(options.address()),
44
47
port_(options.port()),
48
+ protocol_family(options.protocol_family()),
45
49
thread_pool(options.thread_pool()
46
50
? options.thread_pool()
47
51
: std::make_shared<utils::thread_pool>()),
@@ -108,11 +112,19 @@ struct async_server_base : server_storage_base, socket_options_base {
108
112
}
109
113
}
110
114
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
+
111
122
private:
112
123
typedef std::unique_lock<std::mutex> scoped_mutex_lock;
113
124
114
125
Handler &handler;
115
126
string_type address_, port_;
127
+ typename options::protocol_family_t protocol_family;
116
128
std::shared_ptr<utils::thread_pool> thread_pool;
117
129
boost::asio::ip::tcp::acceptor acceptor;
118
130
bool stopping;
@@ -165,7 +177,15 @@ struct async_server_base : server_storage_base, socket_options_base {
165
177
// this allows repeated cycles of run -> stop -> run
166
178
service_.reset ();
167
179
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
+ }}());
169
189
tcp::resolver::iterator endpoint_iterator = resolver.resolve (query, error);
170
190
if (error) {
171
191
BOOST_NETWORK_MESSAGE (" Error resolving '" << address_ << ' :' << port_);
@@ -185,6 +205,8 @@ struct async_server_base : server_storage_base, socket_options_base {
185
205
<< port_);
186
206
return ;
187
207
}
208
+ address_ = acceptor.local_endpoint ().address ().to_string ();
209
+ port_ = std::to_string (acceptor.local_endpoint ().port ());
188
210
acceptor.listen (boost::asio::socket_base::max_connections, error);
189
211
if (error) {
190
212
BOOST_NETWORK_MESSAGE (" Error listening on socket: '"
0 commit comments