Skip to content

Commit 3439043

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

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct async_server_base : server_storage_base, socket_options_base {
4242
handler(options.handler()),
4343
address_(options.address()),
4444
port_(options.port()),
45+
protocol_(options.protocol()),
4546
thread_pool(options.thread_pool()
4647
? options.thread_pool()
4748
: std::make_shared<utils::thread_pool>()),
@@ -108,11 +109,19 @@ struct async_server_base : server_storage_base, socket_options_base {
108109
}
109110
}
110111

112+
/// Returns the server socket address, either IPv4 or IPv6 depending on
113+
/// server_options.protocol()
114+
const string_type& address() const { return address_; }
115+
116+
/// Returns the server socket port
117+
const string_type& port() const { return port_; }
118+
111119
private:
112120
typedef std::unique_lock<std::mutex> scoped_mutex_lock;
113121

114122
Handler &handler;
115123
string_type address_, port_;
124+
boost::asio::ip::tcp protocol_;
116125
std::shared_ptr<utils::thread_pool> thread_pool;
117126
boost::asio::ip::tcp::acceptor acceptor;
118127
bool stopping;
@@ -165,7 +174,7 @@ struct async_server_base : server_storage_base, socket_options_base {
165174
// this allows repeated cycles of run -> stop -> run
166175
service_.reset();
167176
tcp::resolver resolver(service_);
168-
tcp::resolver::query query(address_, port_);
177+
tcp::resolver::query query(protocol_, address_, port_);
169178
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
170179
if (error) {
171180
BOOST_NETWORK_MESSAGE("Error resolving '" << address_ << ':' << port_);
@@ -185,6 +194,8 @@ struct async_server_base : server_storage_base, socket_options_base {
185194
<< port_);
186195
return;
187196
}
197+
address_ = acceptor.local_endpoint().address().to_string();
198+
port_ = std::to_string(acceptor.local_endpoint().port());
188199
acceptor.listen(boost::asio::socket_base::max_connections, error);
189200
if (error) {
190201
BOOST_NETWORK_MESSAGE("Error listening on socket: '"

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

Lines changed: 13 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_(boost::asio::ip::tcp::v6()),
3739
reuse_address_(false),
3840
report_aborted_(false),
3941
non_blocking_io_(true),
@@ -88,6 +90,12 @@ struct server_options {
8890
return *this;
8991
}
9092

93+
/// Set the protocol for address resolving and binding. Default is IPv6.
94+
server_options &protocol(boost::asio::ip::tcp const &v) {
95+
protocol_ = v;
96+
return *this;
97+
}
98+
9199
/// Set whether to reuse the address (SO_REUSE_ADDR). Default is false.
92100
server_options &reuse_address(bool v) {
93101
reuse_address_ = v;
@@ -159,6 +167,9 @@ struct server_options {
159167
/// Returns the port to listen on.
160168
string_type port() const { return port_; }
161169

170+
/// Returns the protocol used for address resolving and binding.
171+
boost::asio::ip::tcp protocol() const { return protocol_; }
172+
162173
/// Returns a reference to the provided handler.
163174
Handler &handler() const { return handler_; }
164175

@@ -215,6 +226,7 @@ struct server_options {
215226
swap(io_service_, other.io_service_);
216227
swap(address_, other.address_);
217228
swap(port_, other.port_);
229+
swap(protocol_, other.protocol_);
218230
swap(reuse_address_, other.reuse_address_);
219231
swap(report_aborted_, other.report_aborted_);
220232
swap(non_blocking_io_, other.non_blocking_io_);
@@ -233,6 +245,7 @@ struct server_options {
233245
Handler &handler_;
234246
string_type address_;
235247
string_type port_;
248+
boost::asio::ip::tcp protocol_;
236249
bool reuse_address_;
237250
bool report_aborted_;
238251
bool non_blocking_io_;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy