Updates on_close and on_fail to use new handler style

This commit is contained in:
Peter Thorson
2013-01-07 12:45:09 -06:00
parent ebfb8b8e2f
commit 235b567b59
4 changed files with 61 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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