From ef8898afef1c338e669ee897918eb0fe147bca5f Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Tue, 8 Jan 2013 06:33:02 -0600 Subject: [PATCH] adds support for message_handler --- examples/echo_server/echo_server.cpp | 13 ++++++++++++- websocketpp/connection.hpp | 14 ++++++++++++++ websocketpp/endpoint.hpp | 5 +++++ websocketpp/impl/connection_impl.hpp | 5 ++++- websocketpp/impl/endpoint_impl.hpp | 1 + 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/examples/echo_server/echo_server.cpp b/examples/echo_server/echo_server.cpp index 3bb900311d..7d568fd2e7 100644 --- a/examples/echo_server/echo_server.cpp +++ b/examples/echo_server/echo_server.cpp @@ -89,9 +89,20 @@ void on_tcp_init(websocketpp::connection_hdl hdl) { std::cout << "on_tcp_init called with hdl: " << hdl.lock().get() << std::endl; } + using websocketpp::lib::placeholders::_1; +using websocketpp::lib::placeholders::_2; using websocketpp::lib::bind; +// pull out the type of messages sent by our config +typedef websocketpp::config::asio::message_type::ptr message_ptr; + +void on_message(server* s,websocketpp::connection_hdl hdl,message_ptr msg) { + std::cout << "on_message called with hdl: " << hdl.lock().get() + << " and message: " << msg->get_payload() + << std::endl; + //s->get_con_from_hdl(hdl)->send(msg->get_payload(),msg->get_opcode()); +} int main() { server::handler::ptr h(new handler()); @@ -102,10 +113,10 @@ int main() { test_handler t(echo_server); - //echo_server.set_open_handler(websocketpp::lib::bind(&test_handler::on_open,t,websocketpp::lib::placeholders::_1)); echo_server.set_open_handler(bind(&on_open,&echo_server,::_1)); echo_server.set_interrupt_handler(bind(&on_interrupt,&echo_server,::_1)); echo_server.set_tcp_init_handler(&on_tcp_init); + echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2)); // Listen echo_server.listen(9002); diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index 5e8e8c9320..cd463dfefd 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -181,6 +181,9 @@ public: typedef processor::processor processor_type; typedef lib::shared_ptr processor_ptr; + // Message handler (needs to know message type) + typedef lib::function message_handler; + // Misc Convenience Types typedef session::internal_state::value istate_type; @@ -340,6 +343,16 @@ public: m_validate_handler = h; } + /// Set message handler + /** + * The message handler is called after a new message has been received. + * + * @param h The new message_handler + */ + void set_message_handler(message_handler h) { + m_message_handler = h; + } + /// Set new connection handler /** * Will invoke the old handler's on_unload callback followed by the @@ -832,6 +845,7 @@ private: interrupt_handler m_interrupt_handler; http_handler m_http_handler; validate_handler m_validate_handler; + message_handler m_message_handler; /// Legacy Handler handler_ptr m_handler; diff --git a/websocketpp/endpoint.hpp b/websocketpp/endpoint.hpp index 7340275890..c609532d51 100644 --- a/websocketpp/endpoint.hpp +++ b/websocketpp/endpoint.hpp @@ -65,6 +65,9 @@ public: /// that this endpoint creates. typedef typename transport_con_type::ptr transport_con_ptr; + /// Type of message_handler + typedef typename connection_type::message_handler message_handler; + // TODO: organize these typedef typename connection_type::handler handler_type; typedef typename handler_type::ptr handler_ptr; @@ -125,6 +128,7 @@ public: void set_interrupt_handler(interrupt_handler h) {m_interrupt_handler = h;} void set_http_handler(http_handler h) {m_http_handler = h;} void set_validate_handler(validate_handler h) {m_validate_handler = h;} + void set_message_handler(message_handler h) {m_message_handler = h;} /*************************************/ /* Connection pass through functions */ @@ -185,6 +189,7 @@ private: interrupt_handler m_interrupt_handler; http_handler m_http_handler; validate_handler m_validate_handler; + message_handler m_message_handler; // endpoint resources std::set m_connections; diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index 08f941dab3..0c3ac0d4fa 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -685,7 +685,10 @@ void connection::handle_read_frame(const lib::error_code& ec, std::cout << "null message from m_processor" << std::endl; } else if (!is_control(msg->get_opcode())) { // data message, dispatch to user - m_handler->on_message(type::shared_from_this(), msg); + //m_handler->on_message(type::shared_from_this(), msg); + if (m_message_handler) { + m_message_handler(m_connection_hdl, msg); + } } else { process_control_frame(msg); } diff --git a/websocketpp/impl/endpoint_impl.hpp b/websocketpp/impl/endpoint_impl.hpp index 882b300515..4bb7a408c8 100644 --- a/websocketpp/impl/endpoint_impl.hpp +++ b/websocketpp/impl/endpoint_impl.hpp @@ -67,6 +67,7 @@ endpoint::create_connection() { con->set_interrupt_handler(m_interrupt_handler); con->set_http_handler(m_http_handler); con->set_validate_handler(m_validate_handler); + con->set_message_handler(m_message_handler); con->set_termination_handler( lib::bind(