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

Expose OS-chosen address and port from async_server#734

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
deanberris merged 1 commit intocpp-netlib:masterfromtribal-tec:master
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes fromall 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
Expose OS-chosen address and port from async_server
  • Loading branch information
@tribal-tec
tribal-tec committedFeb 28, 2017
commitc9f1ee5d654540ecee282cbea6cbc7fd7d1a2a6d
26 changes: 24 additions & 2 deletionsboost/network/protocol/http/server/async_server.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionsboost/network/protocol/http/server/options.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp