diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index 44f21403b2..1fae7855a6 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -54,6 +54,8 @@ typedef lib::function ping_handler; typedef lib::function pong_handler; typedef lib::function pong_timeout_handler; +typedef lib::function validate_handler; +typedef lib::function http_handler; // constants related to the default WebSocket protocol versions available #ifdef _WEBSOCKETPP_INITIALIZER_LISTS_ // simplified C++11 version @@ -152,11 +154,11 @@ public: virtual void on_message(connection_ptr con, message_ptr msg) {} //virtual void on_close(connection_ptr con) {} - virtual bool on_ping(connection_ptr con, const std::string &) { - return true; - } - virtual void on_pong(connection_ptr con, const std::string &) {} - virtual void on_pong_timeout(connection_ptr con, const std::string &) {} + //virtual bool on_ping(connection_ptr con, const std::string &) { + // return true; + //} + //virtual void on_pong(connection_ptr con, const std::string &) {} + //virtual void on_pong_timeout(connection_ptr con, const std::string &) {} virtual void on_load(connection_ptr con, ptr old_handler) {} virtual void on_unload(connection_ptr con, ptr new_handler) {} @@ -254,6 +256,48 @@ public: m_fail_handler = h; } + /// Set ping handler + /** + * The ping handler is called whenever the connection receives a ping + * control frame. The ping payload is included. + * + * The ping handler's return time controls whether or not a pong is + * sent in response to this ping. Returning false will suppress the + * return pong. If no ping handler is set a pong will be sent. + * + * @param h The new ping_handler + */ + void set_ping_handler(ping_handler h) { + m_ping_handler = h; + } + + /// Set pong handler + /** + * The pong handler is called whenever the connection receives a pong + * control frame. The pong payload is included. + * + * @param h The new pong_handler + */ + void set_pong_handler(pong_handler h) { + m_pong_handler = h; + } + + /// Set pong timeout handler + /** + * If the transport component being used supports timers, the pong timeout + * handler is called whenever a pong control frame is not received with the + * configured timeout period after the application sends a ping. + * + * This can be used to probe the health of the remote endpoint's WebSocket + * implimentation. This does not guarantee that the remote application + * itself is still healthy but can be a useful diagnostic. + * + * @param h The new pong_timeout_handler + */ + void set_pong_timeout_handler(pong_timeout_handler h) { + m_pong_timeout_handler = h; + } + /// Set interrupt handler /** * The interrupt handler is called whenever the connection is manually @@ -751,6 +795,9 @@ private: open_handler m_open_handler; close_handler m_close_handler; fail_handler m_fail_handler; + ping_handler m_ping_handler; + pong_handler m_pong_handler; + pong_timeout_handler m_pong_timeout_handler; interrupt_handler m_interrupt_handler; /// Legacy Handler diff --git a/websocketpp/endpoint.hpp b/websocketpp/endpoint.hpp index 9f9404a062..612c3da355 100644 --- a/websocketpp/endpoint.hpp +++ b/websocketpp/endpoint.hpp @@ -117,6 +117,11 @@ public: 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_ping_handler(ping_handler h) {m_ping_handler = h;} + void set_pong_handler(pong_handler h) {m_pong_handler = h;} + void set_pong_timeout_handler(pong_timeout_handler h) { + m_pong_timeout_handler = h; + } void set_interrupt_handler(interrupt_handler h) {m_interrupt_handler = h;} /*************************************/ @@ -172,6 +177,9 @@ private: open_handler m_open_handler; close_handler m_close_handler; fail_handler m_fail_handler; + ping_handler m_ping_handler; + pong_handler m_pong_handler; + pong_timeout_handler m_pong_timeout_handler; interrupt_handler m_interrupt_handler; // endpoint resources diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index 351fcac680..3744b1792e 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -1148,8 +1148,13 @@ void connection::process_control_frame(typename lib::error_code ec; if (op == frame::opcode::PING) { - if (m_handler->on_ping(type::shared_from_this(),msg->get_payload())) { - // send pong + bool pong = true; + + if (m_ping_handler) { + pong = m_ping_handler(m_connection_hdl, msg->get_payload()); + } + + if (pong) { this->pong(msg->get_payload(),ec); if (ec) { std::cout << "Failed to send response pong: " @@ -1157,7 +1162,9 @@ void connection::process_control_frame(typename } } } else if (op == frame::opcode::PONG) { - m_handler->on_pong(type::shared_from_this(), msg->get_payload()); + if (m_pong_handler) { + m_pong_handler(m_connection_hdl, msg->get_payload()); + } } else if (op == frame::opcode::CLOSE) { std::cout << "Got close frame" << std::endl; // record close code and reason somewhere diff --git a/websocketpp/impl/endpoint_impl.hpp b/websocketpp/impl/endpoint_impl.hpp index c1f0db1495..41fe7abfb7 100644 --- a/websocketpp/impl/endpoint_impl.hpp +++ b/websocketpp/impl/endpoint_impl.hpp @@ -61,6 +61,9 @@ endpoint::create_connection() { con->set_open_handler(m_open_handler); con->set_close_handler(m_close_handler); con->set_fail_handler(m_fail_handler); + con->set_ping_handler(m_ping_handler); + con->set_pong_handler(m_pong_handler); + con->set_pong_timeout_handler(m_pong_timeout_handler); con->set_interrupt_handler(m_interrupt_handler); con->set_termination_handler( diff --git a/websocketpp/roles/server_endpoint.hpp b/websocketpp/roles/server_endpoint.hpp index 8c6d72abc8..b3fc7f8165 100644 --- a/websocketpp/roles/server_endpoint.hpp +++ b/websocketpp/roles/server_endpoint.hpp @@ -34,6 +34,8 @@ namespace websocketpp { +typedef lib::function validate_handler; + /// Server endpoint role based on the given config /** *