From 8993b34b3dc637e9a66588bb59aeec691d40872e Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sat, 2 Nov 2013 19:32:38 -0500 Subject: [PATCH] Allows changing the listen backlog queue length --- changelog.md | 1 + websocketpp/transport/asio/endpoint.hpp | 36 ++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 1c30c83b7c..085a6cb3f5 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,5 @@ HEAD +- Allows changing the listen backlog queue length. - Fix handler allocation crash with multithreaded io_service. - Split tcp init into pre and post init. - Adds URI method to extract query string from URI. Thank you Banaan for code. diff --git a/websocketpp/transport/asio/endpoint.hpp b/websocketpp/transport/asio/endpoint.hpp index 59c420b76a..8f4577a4fc 100644 --- a/websocketpp/transport/asio/endpoint.hpp +++ b/websocketpp/transport/asio/endpoint.hpp @@ -88,6 +88,7 @@ public: // generate and manage our own io_service explicit endpoint() : m_external_io_service(false) + , m_listen_backlog(0) , m_state(UNINITIALIZED) { //std::cout << "transport::asio::endpoint constructor" << std::endl; @@ -119,6 +120,7 @@ public: : m_io_service(src.m_io_service) , m_external_io_service(src.m_external_io_service) , m_acceptor(src.m_acceptor) + , m_listen_backlog(0) , m_state(src.m_state) { src.m_io_service = NULL; @@ -132,11 +134,13 @@ public: m_io_service = rhs.m_io_service; m_external_io_service = rhs.m_external_io_service; m_acceptor = rhs.m_acceptor; + m_listen_backlog = rhs.m_listen_backlog m_state = rhs.m_state; rhs.m_io_service = NULL; rhs.m_external_io_service = false; rhs.m_acceptor = NULL; + rhs.m_listen_backlog = 0; rhs.m_state = UNINITIALIZED; } return *this; @@ -216,6 +220,29 @@ public: m_external_io_service = false; } + /// Sets the maximum length of the queue of pending connections. + /** + * Sets the maximum length of the queue of pending connections. Increasing + * this will allow WebSocket++ to queue additional incoming connections. + * Setting it higher may prevent failed connections at high connection rates + * but may cause additional latency. + * + * For this value to take effect you may need to adjust operating system + * settings. + * + * New values affect future calls to listen only. + * + * A value of zero will use the operating system default. This is the + * default value. + * + * @since 0.4.0-alpha1 + * + * @param backlog The maximum length of the queue of pending connections + */ + void set_listen_backlog(int backlog) { + m_listen_backlog = backlog; + } + /// Retrieve a reference to the endpoint's io_service /** * The io_service may be an internal or external one. This may be used to @@ -268,7 +295,11 @@ public: m_acceptor->open(ep.protocol()); m_acceptor->set_option(boost::asio::socket_base::reuse_address(true)); m_acceptor->bind(ep); - m_acceptor->listen(); + if (m_listen_backlog == 0) { + m_acceptor->listen(); + } else { + m_acceptor->listen(m_listen_backlog); + } m_state = LISTENING; ec = lib::error_code(); } @@ -927,6 +958,9 @@ private: acceptor_ptr m_acceptor; resolver_ptr m_resolver; + // Network constants + int m_listen_backlog; + elog_type* m_elog; alog_type* m_alog;