Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9f1ee5

Browse files
committed
Expose OS-chosen address and port from async_server
1 parent2b910a6 commitc9f1ee5

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-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-
explicitasync_server_base(server_options<Tag, Handler>const &options)
42+
explicitasync_server_base(optionsconst &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::resolverresolver(service_);
168-
tcp::resolver::queryquery(address_, port_);
180+
tcp::resolver::queryquery( [&]{
181+
switch(protocol_family) {
182+
case options::ipv4:
183+
returntcp::resolver::query(tcp::v4(), address_, port_);
184+
case options::ipv6:
185+
returntcp::resolver::query(tcp::v6(), address_, port_);
186+
default:
187+
returntcp::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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct server_options {
3434
handler_(handler),
3535
address_("localhost"),
3636
port_("80"),
37+
protocol_family_(undefined),
3738
reuse_address_(false),
3839
report_aborted_(false),
3940
non_blocking_io_(true),
@@ -88,6 +89,14 @@ struct server_options {
8889
return *this;
8990
}
9091

92+
enumprotocol_family_t { ipv4, ipv6, undefined };
93+
94+
/// Set the protocol family for address resolving. Default is AF_UNSPEC.
95+
server_options &protocol_family(protocol_family_t v) {
96+
protocol_family_ = v;
97+
return *this;
98+
}
99+
91100
/// Set whether to reuse the address (SO_REUSE_ADDR). Default is false.
92101
server_options &reuse_address(bool v) {
93102
reuse_address_ = v;
@@ -159,6 +168,9 @@ struct server_options {
159168
/// Returns the port to listen on.
160169
string_typeport()const {return port_; }
161170

171+
/// Returns the protocol family used for address resolving.
172+
protocol_family_tprotocol_family()const {return protocol_family_; }
173+
162174
/// Returns a reference to the provided handler.
163175
Handler &handler()const {return handler_; }
164176

@@ -215,6 +227,7 @@ struct server_options {
215227
swap(io_service_, other.io_service_);
216228
swap(address_, other.address_);
217229
swap(port_, other.port_);
230+
swap(protocol_family_, other.protocol_family_);
218231
swap(reuse_address_, other.reuse_address_);
219232
swap(report_aborted_, other.report_aborted_);
220233
swap(non_blocking_io_, other.non_blocking_io_);
@@ -233,6 +246,7 @@ struct server_options {
233246
Handler &handler_;
234247
string_type address_;
235248
string_type port_;
249+
protocol_family_t protocol_family_;
236250
bool reuse_address_;
237251
bool report_aborted_;
238252
bool non_blocking_io_;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp