From 235b567b590ef7eff5014362b70bb9e1f0baeda8 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Mon, 7 Jan 2013 12:45:09 -0600 Subject: [PATCH] Updates on_close and on_fail to use new handler style --- websocketpp/connection.hpp | 53 ++++++++++++++++++++++++---- websocketpp/endpoint.hpp | 12 +++---- websocketpp/impl/connection_impl.hpp | 10 +++--- websocketpp/impl/endpoint_impl.hpp | 2 ++ 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index 26f561d78a..44f21403b2 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -145,12 +145,12 @@ public: // TODO: validate is server only. hide from client handlers? virtual bool validate(connection_ptr con) {return true;} - virtual void on_inturrupt(connection_ptr con) {} + //virtual void on_inturrupt(connection_ptr con) {} - virtual void on_open(connection_ptr con) {} - virtual void on_fail(connection_ptr con) {} + //virtual void on_open(connection_ptr con) {} + //virtual void on_fail(connection_ptr con) {} virtual void on_message(connection_ptr con, message_ptr msg) {} - virtual void on_close(connection_ptr con) {} + //virtual void on_close(connection_ptr con) {} virtual bool on_ping(connection_ptr con, const std::string &) { return true; @@ -222,10 +222,45 @@ public: return m_connection_hdl; } + /// Set open handler + /** + * The open handler is called after the WebSocket handshake is complete and + * the connection is considered OPEN. + * + * @param h The new open_handler + */ void set_open_handler(open_handler h) { m_open_handler = h; } + + /// Set close handler + /** + * The close handler is called immediately after the connection is closed. + * + * @param h The new close_handler + */ + void set_close_handler(close_handler h) { + m_close_handler = h; + } + /// Set fail handler + /** + * The fail handler is called whenever the connection fails while the + * handshake is bring processed. + * + * @param h The new fail_handler + */ + void set_fail_handler(fail_handler h) { + m_fail_handler = h; + } + + /// Set interrupt handler + /** + * The interrupt handler is called whenever the connection is manually + * interrupted by the application. + * + * @param h The new interrupt_handler + */ void set_interrupt_handler(interrupt_handler h) { m_interrupt_handler = h; } @@ -709,12 +744,18 @@ private: // static settings const std::string m_user_agent; - /// Pointer to the handler + /// Pointer to the connection handle connection_hdl m_connection_hdl; - handler_ptr m_handler; + + /// Handler objects open_handler m_open_handler; + close_handler m_close_handler; + fail_handler m_fail_handler; interrupt_handler m_interrupt_handler; + /// Legacy Handler + handler_ptr m_handler; + /// External connection state /** * Lock: m_connection_state_lock diff --git a/websocketpp/endpoint.hpp b/websocketpp/endpoint.hpp index e52b3412cd..9f9404a062 100644 --- a/websocketpp/endpoint.hpp +++ b/websocketpp/endpoint.hpp @@ -114,12 +114,10 @@ public: /* Set Handler functions */ /*************************/ - void set_open_handler(open_handler h) { - m_open_handler = h; - } - void set_interrupt_handler(interrupt_handler h) { - m_interrupt_handler = h; - } + void set_open_handler(open_handler h) {m_open_handler = h;} + void set_close_handler(close_handler h) {m_close_handler = h;} + void set_fail_handler(fail_handler h) {m_fail_handler = h;} + void set_interrupt_handler(interrupt_handler h) {m_interrupt_handler = h;} /*************************************/ /* Connection pass through functions */ @@ -172,6 +170,8 @@ private: std::string m_user_agent; open_handler m_open_handler; + close_handler m_close_handler; + fail_handler m_fail_handler; interrupt_handler m_interrupt_handler; // endpoint resources diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index da8ff73216..351fcac680 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -977,8 +977,6 @@ void connection::handle_send_http_response( "handle_send_http_response must be called from PROCESS_HTTP_REQUEST state" ); - m_handler->on_open(type::shared_from_this()); - if (m_open_handler) { m_open_handler(m_connection_hdl); } @@ -994,10 +992,14 @@ void connection::terminate() { if (m_state == session::state::CONNECTING) { m_state = session::state::CLOSED; - m_handler->on_fail(type::shared_from_this()); + if (m_fail_handler) { + m_fail_handler(m_connection_hdl); + } } else { m_state = session::state::CLOSED; - m_handler->on_close(type::shared_from_this()); + if (m_close_handler) { + m_close_handler(m_connection_hdl); + } } // call the termination handler if it exists diff --git a/websocketpp/impl/endpoint_impl.hpp b/websocketpp/impl/endpoint_impl.hpp index 7af041b9c4..c1f0db1495 100644 --- a/websocketpp/impl/endpoint_impl.hpp +++ b/websocketpp/impl/endpoint_impl.hpp @@ -59,6 +59,8 @@ endpoint::create_connection() { // Copy default handlers from the endpoint con->set_open_handler(m_open_handler); + con->set_close_handler(m_close_handler); + con->set_fail_handler(m_fail_handler); con->set_interrupt_handler(m_interrupt_handler); con->set_termination_handler(