From c559e0a6239d2fdb6bf200828a0f9730c758b305 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sat, 4 May 2013 11:52:52 -0500 Subject: [PATCH] connection initialization errors are properly logged and propogated --- websocketpp/impl/endpoint_impl.hpp | 16 +++++++++------- websocketpp/transport/asio/connection.hpp | 9 +++++++-- websocketpp/transport/asio/endpoint.hpp | 14 +++++++++++--- websocketpp/transport/asio/security/none.hpp | 8 ++++++-- websocketpp/transport/asio/security/tls.hpp | 7 ++++++- websocketpp/transport/iostream/endpoint.hpp | 16 ++++++++++++++-- 6 files changed, 53 insertions(+), 17 deletions(-) diff --git a/websocketpp/impl/endpoint_impl.hpp b/websocketpp/impl/endpoint_impl.hpp index 9b3c4a68c6..29223e7908 100644 --- a/websocketpp/impl/endpoint_impl.hpp +++ b/websocketpp/impl/endpoint_impl.hpp @@ -46,7 +46,6 @@ endpoint::create_connection() { connection_weak_ptr w(con); - // Create a weak pointer on the heap using that shared_ptr. // Cast that weak pointer to void* and manage it using another shared_ptr // connection_hdl hdl(reinterpret_cast(new connection_weak_ptr(con))); @@ -75,15 +74,18 @@ endpoint::create_connection() { lib::placeholders::_1 ) ); - transport_type::init(con); - + + lib::error_code ec; + + ec = transport_type::init(con); + if (ec) { + m_elog.write(log::elevel::fatal,ec.message()); + return connection_ptr(); + } + scoped_lock_type lock(m_mutex); m_connections.insert(con); - - //m_alog->at(log::alevel::DEVEL) << "Connection created: count is now: " - // << m_connections.size() << log::endl; - return con; } diff --git a/websocketpp/transport/asio/connection.hpp b/websocketpp/transport/asio/connection.hpp index ea61faf7db..2274725b04 100644 --- a/websocketpp/transport/asio/connection.hpp +++ b/websocketpp/transport/asio/connection.hpp @@ -102,14 +102,19 @@ public: * TODO: this method is not protected because the endpoint needs to call it. * need to figure out if there is a way to friend the endpoint safely across * different compilers. + * + * @param io_service A pointer to the io_service to register with this + * connection + * + * @return Status code for the success or failure of the initialization */ - void init_asio (io_service_ptr io_service) { + lib::error_code init_asio (io_service_ptr io_service) { // do we need to store or use the io_service at this level? m_io_service = io_service; //m_strand.reset(new boost::asio::strand(*io_service)); - socket_con_type::init_asio(io_service, m_is_server); + return socket_con_type::init_asio(io_service, m_is_server); } void set_tcp_init_handler(tcp_init_handler h) { diff --git a/websocketpp/transport/asio/endpoint.hpp b/websocketpp/transport/asio/endpoint.hpp index 1425187e02..cc2e3829bd 100644 --- a/websocketpp/transport/asio/endpoint.hpp +++ b/websocketpp/transport/asio/endpoint.hpp @@ -462,16 +462,24 @@ protected: * constructor. * * @param tcon A pointer to the transport portion of the connection. + * + * @return A status code indicating the success or failure of the operation */ - void init(transport_con_ptr tcon) { + lib::error_code init(transport_con_ptr tcon) { m_alog->write(log::alevel::devel, "transport::asio::init"); // Initialize the connection socket component socket_type::init(lib::static_pointer_cast(tcon)); - - tcon->init_asio(m_io_service); + + lib::error_code ec; + + ec = tcon->init_asio(m_io_service); + if (ec) {return ec;} + tcon->set_tcp_init_handler(m_tcp_init_handler); + + return lib::error_code(); } private: enum state { diff --git a/websocketpp/transport/asio/security/none.hpp b/websocketpp/transport/asio/security/none.hpp index 5e160c5947..a70769c23f 100644 --- a/websocketpp/transport/asio/security/none.hpp +++ b/websocketpp/transport/asio/security/none.hpp @@ -242,15 +242,19 @@ public: void set_socket_init_handler(socket_init_handler h) { m_socket_init_handler = h; } - protected: /// Initialize a connection /** * Called by the transport after a new connection is created to initialize * the socket component of the connection. + * + * @param scon Pointer to the socket component of the connection + * + * @return Error code (empty on success) */ - void init(socket_con_ptr scon) { + lib::error_code init(socket_con_ptr scon) { scon->set_socket_init_handler(m_socket_init_handler); + return lib::error_code(); } private: socket_init_handler m_socket_init_handler; diff --git a/websocketpp/transport/asio/security/tls.hpp b/websocketpp/transport/asio/security/tls.hpp index e4951e42ed..97f900e665 100644 --- a/websocketpp/transport/asio/security/tls.hpp +++ b/websocketpp/transport/asio/security/tls.hpp @@ -352,10 +352,15 @@ protected: /** * Called by the transport after a new connection is created to initialize * the socket component of the connection. + * + * @param scon Pointer to the socket component of the connection + * + * @return Error code (empty on success) */ - void init(socket_con_ptr scon) { + lib::error_code init(socket_con_ptr scon) { scon->set_socket_init_handler(m_socket_init_handler); scon->set_tls_init_handler(m_tls_init_handler); + return lib::error_code(); } private: diff --git a/websocketpp/transport/iostream/endpoint.hpp b/websocketpp/transport/iostream/endpoint.hpp index 1adccca81e..dfaf82f20c 100644 --- a/websocketpp/transport/iostream/endpoint.hpp +++ b/websocketpp/transport/iostream/endpoint.hpp @@ -108,8 +108,20 @@ protected: cb(tcon->get_handle(),lib::error_code()); } - void init(transport_con_ptr tcon) { - tcon->register_ostream(output_stream); + /// Initialize a connection + /** + * Init is called by an endpoint once for each newly created connection. + * It's purpose is to give the transport policy the chance to perform any + * transport specific initialization that couldn't be done via the default + * constructor. + * + * @param tcon A pointer to the transport portion of the connection. + * + * @return A status code indicating the success or failure of the operation + */ + lib::error_code init(transport_con_ptr tcon) { + tcon->register_ostream(m_output_stream); + return lib::error_code(); } private: std::ostream* m_output_stream;