From b805689f9caf261531a83e93c3881b6a42fd39c1 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Tue, 4 Mar 2014 06:45:00 -0600 Subject: [PATCH] whitespace and don't accept if we know the transport isn't listening references #334 --- websocketpp/roles/server_endpoint.hpp | 21 +++++++++++++-------- websocketpp/transport/asio/endpoint.hpp | 12 ++++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/websocketpp/roles/server_endpoint.hpp b/websocketpp/roles/server_endpoint.hpp index 9ebb109897..911c61cd4c 100644 --- a/websocketpp/roles/server_endpoint.hpp +++ b/websocketpp/roles/server_endpoint.hpp @@ -82,6 +82,12 @@ public: // Starts the server's async connection acceptance loop. void start_accept() { + if (!transport_type::is_listening()) { + endpoint_type::m_elog.write(log::elevel::info, + "Stopping acceptance of new connections because the underlying transport is no longer listening."); + return; + } + connection_ptr con = get_connection(); lib::error_code ec; @@ -104,13 +110,12 @@ public: endpoint_type::m_elog.write(log::elevel::rerror, "start_accept error: "+ec.message()); } - - if (ec) { - // Terminate the connection to prevent memory leaks. - lib::error_code con_ec; - con->terminate(con_ec); - } - } + + if (ec) { + // Terminate the connection to prevent memory leaks. + con->terminate(lib::error_code()); + } + } void handle_accept(connection_ptr con, lib::error_code const & ec) { if (ec) { @@ -127,7 +132,7 @@ public: con->start(); } - // TODO: are there cases where we should terminate this loop? + start_accept(); } private: diff --git a/websocketpp/transport/asio/endpoint.hpp b/websocketpp/transport/asio/endpoint.hpp index 7d007dd8f0..21bb610e57 100644 --- a/websocketpp/transport/asio/endpoint.hpp +++ b/websocketpp/transport/asio/endpoint.hpp @@ -539,6 +539,14 @@ public: } } + /// Check if the endpoint is listening + /** + * @return Whether or not the endpoint is listening. + */ + bool is_listening() const { + return (m_state == LISTENING); + } + /// wraps the run method of the internal io_service object std::size_t run() { return m_io_service->run(); @@ -985,10 +993,6 @@ protected: callback(lib::error_code()); } - bool is_listening() const { - return (m_state == LISTENING); - } - /// Initialize a connection /** * init is called by an endpoint once for each newly created connection.