Updates on_ping, on_pong, and on_pong_timeout handlers to new interface

This commit is contained in:
Peter Thorson
2013-01-07 16:53:14 -06:00
parent 235b567b59
commit e7b87e5d54
5 changed files with 75 additions and 8 deletions

View File

@@ -54,6 +54,8 @@ typedef lib::function<bool(connection_hdl,std::string)> ping_handler;
typedef lib::function<void(connection_hdl,std::string)> pong_handler;
typedef lib::function<void(connection_hdl,std::string)> pong_timeout_handler;
typedef lib::function<bool(connection_hdl)> validate_handler;
typedef lib::function<void(connection_hdl)> 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

View File

@@ -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

View File

@@ -1148,8 +1148,13 @@ void connection<config>::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<config>::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

View File

@@ -61,6 +61,9 @@ endpoint<connection,config>::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(

View File

@@ -34,6 +34,8 @@
namespace websocketpp {
typedef lib::function<bool(connection_hdl)> validate_handler;
/// Server endpoint role based on the given config
/**
*