Skip to content

Expose OS-chosen address and port from async_server #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions boost/network/protocol/http/server/async_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ struct async_server_base : server_storage_base, socket_options_base {
/// Defines the type for the connection pointer.
typedef std::shared_ptr<connection> connection_ptr;

/// Defines the type for the options.
typedef server_options<Tag, Handler> options;

/// Constructs and initializes the asynchronous server core.
explicit async_server_base(server_options<Tag, Handler> const &options)
explicit async_server_base(options const &options)
: server_storage_base(options),
socket_options_base(options),
handler(options.handler()),
address_(options.address()),
port_(options.port()),
protocol_family(options.protocol_family()),
thread_pool(options.thread_pool()
? options.thread_pool()
: std::make_shared<utils::thread_pool>()),
Expand Down Expand Up @@ -108,11 +112,19 @@ struct async_server_base : server_storage_base, socket_options_base {
}
}

/// Returns the server socket address, either IPv4 or IPv6 depending on
/// options.protocol_family()
const string_type& address() const { return address_; }

/// Returns the server socket port
const string_type& port() const { return port_; }

private:
typedef std::unique_lock<std::mutex> scoped_mutex_lock;

Handler &handler;
string_type address_, port_;
typename options::protocol_family_t protocol_family;
std::shared_ptr<utils::thread_pool> thread_pool;
boost::asio::ip::tcp::acceptor acceptor;
bool stopping;
Expand Down Expand Up @@ -165,7 +177,15 @@ struct async_server_base : server_storage_base, socket_options_base {
// this allows repeated cycles of run -> stop -> run
service_.reset();
tcp::resolver resolver(service_);
tcp::resolver::query query(address_, port_);
tcp::resolver::query query( [&]{
switch(protocol_family) {
case options::ipv4:
return tcp::resolver::query(tcp::v4(), address_, port_);
case options::ipv6:
return tcp::resolver::query(tcp::v6(), address_, port_);
default:
return tcp::resolver::query(address_, port_);
}}());
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
if (error) {
BOOST_NETWORK_MESSAGE("Error resolving '" << address_ << ':' << port_);
Expand All @@ -185,6 +205,8 @@ struct async_server_base : server_storage_base, socket_options_base {
<< port_);
return;
}
address_ = acceptor.local_endpoint().address().to_string();
port_ = std::to_string(acceptor.local_endpoint().port());
acceptor.listen(boost::asio::socket_base::max_connections, error);
if (error) {
BOOST_NETWORK_MESSAGE("Error listening on socket: '"
Expand Down
14 changes: 14 additions & 0 deletions boost/network/protocol/http/server/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct server_options {
handler_(handler),
address_("localhost"),
port_("80"),
protocol_family_(undefined),
reuse_address_(false),
report_aborted_(false),
non_blocking_io_(true),
Expand Down Expand Up @@ -88,6 +89,14 @@ struct server_options {
return *this;
}

enum protocol_family_t { ipv4, ipv6, undefined };

/// Set the protocol family for address resolving. Default is AF_UNSPEC.
server_options &protocol_family(protocol_family_t v) {
protocol_family_ = v;
return *this;
}

/// Set whether to reuse the address (SO_REUSE_ADDR). Default is false.
server_options &reuse_address(bool v) {
reuse_address_ = v;
Expand Down Expand Up @@ -159,6 +168,9 @@ struct server_options {
/// Returns the port to listen on.
string_type port() const { return port_; }

/// Returns the protocol family used for address resolving.
protocol_family_t protocol_family() const { return protocol_family_; }

/// Returns a reference to the provided handler.
Handler &handler() const { return handler_; }

Expand Down Expand Up @@ -215,6 +227,7 @@ struct server_options {
swap(io_service_, other.io_service_);
swap(address_, other.address_);
swap(port_, other.port_);
swap(protocol_family_, other.protocol_family_);
swap(reuse_address_, other.reuse_address_);
swap(report_aborted_, other.report_aborted_);
swap(non_blocking_io_, other.non_blocking_io_);
Expand All @@ -233,6 +246,7 @@ struct server_options {
Handler &handler_;
string_type address_;
string_type port_;
protocol_family_t protocol_family_;
bool reuse_address_;
bool report_aborted_;
bool non_blocking_io_;
Expand Down