Skip to content

Commit b855c0a

Browse files
committed
Add http_listener configuration for the backlog, the maximum length of the queue of pending connections on the port.
1 parent 28919d0 commit b855c0a

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

Release/include/cpprest/http_listener.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class http_listener_config
4646
/// </summary>
4747
http_listener_config()
4848
: m_timeout(utility::seconds(120))
49+
, m_backlog(0)
4950
{}
5051

5152
/// <summary>
@@ -54,6 +55,7 @@ class http_listener_config
5455
/// <param name="other">http_listener_config to copy.</param>
5556
http_listener_config(const http_listener_config &other)
5657
: m_timeout(other.m_timeout)
58+
, m_backlog(other.m_backlog)
5759
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
5860
, m_ssl_context_callback(other.m_ssl_context_callback)
5961
#endif
@@ -65,6 +67,7 @@ class http_listener_config
6567
/// <param name="other">http_listener_config to move from.</param>
6668
http_listener_config(http_listener_config &&other)
6769
: m_timeout(std::move(other.m_timeout))
70+
, m_backlog(std::move(other.m_backlog))
6871
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
6972
, m_ssl_context_callback(std::move(other.m_ssl_context_callback))
7073
#endif
@@ -79,6 +82,7 @@ class http_listener_config
7982
if(this != &rhs)
8083
{
8184
m_timeout = rhs.m_timeout;
85+
m_backlog = rhs.m_backlog;
8286
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
8387
m_ssl_context_callback = rhs.m_ssl_context_callback;
8488
#endif
@@ -95,6 +99,7 @@ class http_listener_config
9599
if(this != &rhs)
96100
{
97101
m_timeout = std::move(rhs.m_timeout);
102+
m_backlog = std::move(rhs.m_backlog);
98103
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
99104
m_ssl_context_callback = std::move(rhs.m_ssl_context_callback);
100105
#endif
@@ -120,6 +125,26 @@ class http_listener_config
120125
m_timeout = std::move(timeout);
121126
}
122127

128+
/// <summary>
129+
/// Get the listen backlog
130+
/// </summary>
131+
/// <returns>The maximum length of the queue of pending connections, or zero for the implementation default.</returns>
132+
/// <remarks>The implementation may not honour this value.</remarks>
133+
int backlog() const
134+
{
135+
return m_backlog;
136+
}
137+
138+
/// <summary>
139+
/// Set the listen backlog
140+
/// </summary>
141+
/// <param name="backlog">The maximum length of the queue of pending connections, or zero for the implementation default.</param>
142+
/// <remarks>The implementation may not honour this value.</remarks>
143+
void set_backlog(int backlog)
144+
{
145+
m_backlog = backlog;
146+
}
147+
123148
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
124149
/// <summary>
125150
/// Get the callback of ssl context
@@ -143,6 +168,7 @@ class http_listener_config
143168
private:
144169

145170
utility::seconds m_timeout;
171+
int m_backlog;
146172
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
147173
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
148174
#endif

Release/src/http/listener/http_server_asio.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ namespace
122122
class hostport_listener
123123
{
124124
private:
125+
int m_backlog;
125126
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
126127
std::map<std::string, http_listener_impl* > m_listeners;
127128
pplx::extensibility::reader_writer_lock_t m_listeners_lock;
@@ -140,7 +141,8 @@ namespace
140141

141142
public:
142143
hostport_listener(http_linux_server* server, const std::string& hostport, bool is_https, const http_listener_config& config)
143-
: m_acceptor()
144+
: m_backlog(config.backlog())
145+
, m_acceptor()
144146
, m_listeners()
145147
, m_listeners_lock()
146148
, m_connections_lock()
@@ -482,8 +484,11 @@ void hostport_listener::start()
482484

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

485-
m_acceptor.reset(new tcp::acceptor(service, endpoint));
486-
m_acceptor->set_option(tcp::acceptor::reuse_address(true));
487+
m_acceptor.reset(new tcp::acceptor(service));
488+
m_acceptor->open(endpoint.protocol());
489+
m_acceptor->set_option(socket_base::reuse_address(true));
490+
m_acceptor->bind(endpoint);
491+
m_acceptor->listen(0 != m_backlog ? m_backlog : socket_base::max_connections);
487492

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

0 commit comments

Comments
 (0)