Skip to content

Expose http_listener configuration for the backlog #624

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
Jan 24, 2018
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: 26 additions & 0 deletions Release/include/cpprest/http_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class http_listener_config
/// </summary>
http_listener_config()
: m_timeout(utility::seconds(120))
, m_backlog(0)
{}

/// <summary>
Expand All @@ -54,6 +55,7 @@ class http_listener_config
/// <param name="other">http_listener_config to copy.</param>
http_listener_config(const http_listener_config &other)
: m_timeout(other.m_timeout)
, m_backlog(other.m_backlog)
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
, m_ssl_context_callback(other.m_ssl_context_callback)
#endif
Expand All @@ -65,6 +67,7 @@ class http_listener_config
/// <param name="other">http_listener_config to move from.</param>
http_listener_config(http_listener_config &&other)
: m_timeout(std::move(other.m_timeout))
, m_backlog(std::move(other.m_backlog))
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
, m_ssl_context_callback(std::move(other.m_ssl_context_callback))
#endif
Expand All @@ -79,6 +82,7 @@ class http_listener_config
if(this != &rhs)
{
m_timeout = rhs.m_timeout;
m_backlog = rhs.m_backlog;
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
m_ssl_context_callback = rhs.m_ssl_context_callback;
#endif
Expand All @@ -95,6 +99,7 @@ class http_listener_config
if(this != &rhs)
{
m_timeout = std::move(rhs.m_timeout);
m_backlog = std::move(rhs.m_backlog);
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
m_ssl_context_callback = std::move(rhs.m_ssl_context_callback);
#endif
Expand All @@ -120,6 +125,26 @@ class http_listener_config
m_timeout = std::move(timeout);
}

/// <summary>
/// Get the listen backlog
/// </summary>
/// <returns>The maximum length of the queue of pending connections, or zero for the implementation default.</returns>
/// <remarks>The implementation may not honour this value.</remarks>
int backlog() const
{
return m_backlog;
}

/// <summary>
/// Set the listen backlog
/// </summary>
/// <param name="backlog">The maximum length of the queue of pending connections, or zero for the implementation default.</param>
/// <remarks>The implementation may not honour this value.</remarks>
void set_backlog(int backlog)
{
m_backlog = backlog;
}

#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
/// <summary>
/// Get the callback of ssl context
Expand All @@ -143,6 +168,7 @@ class http_listener_config
private:

utility::seconds m_timeout;
int m_backlog;
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
#endif
Expand Down
11 changes: 8 additions & 3 deletions Release/src/http/listener/http_server_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ namespace
class hostport_listener
{
private:
int m_backlog;
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
std::map<std::string, http_listener_impl* > m_listeners;
pplx::extensibility::reader_writer_lock_t m_listeners_lock;
Expand All @@ -140,7 +141,8 @@ namespace

public:
hostport_listener(http_linux_server* server, const std::string& hostport, bool is_https, const http_listener_config& config)
: m_acceptor()
: m_backlog(config.backlog())
, m_acceptor()
, m_listeners()
, m_listeners_lock()
, m_connections_lock()
Expand Down Expand Up @@ -482,8 +484,11 @@ void hostport_listener::start()

tcp::endpoint endpoint = *resolver.resolve(query);

m_acceptor.reset(new tcp::acceptor(service, endpoint));
m_acceptor->set_option(tcp::acceptor::reuse_address(true));
m_acceptor.reset(new tcp::acceptor(service));
m_acceptor->open(endpoint.protocol());
m_acceptor->set_option(socket_base::reuse_address(true));
m_acceptor->bind(endpoint);
m_acceptor->listen(0 != m_backlog ? m_backlog : socket_base::max_connections);

auto socket = new ip::tcp::socket(service);
m_acceptor->async_accept(*socket, [this, socket](const boost::system::error_code& ec)
Expand Down