diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index c8f1bc6061..c3ce1bf000 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -149,6 +149,10 @@ typedef lib::function validate_handler; */ typedef lib::function http_handler; +// +typedef lib::function read_handler; +typedef lib::function write_frame_handler; + // constants related to the default WebSocket protocol versions available #ifdef _WEBSOCKETPP_INITIALIZER_LISTS_ // simplified C++11 version /// Container that stores the list of protocol versions supported @@ -278,6 +282,17 @@ public: explicit connection(bool is_server, std::string const & ua, alog_type& alog, elog_type& elog, rng_type & rng) : transport_con_type(is_server,alog,elog) + , m_handle_read_frame(lib::bind( + &type::handle_read_frame, + this, + lib::placeholders::_1, + lib::placeholders::_2 + )) + , m_write_frame_handler(lib::bind( + &type::handle_write_frame, + this, + lib::placeholders::_1 + )) , m_user_agent(ua) , m_state(session::state::connecting) , m_internal_state(session::internal_state::USER_INIT) @@ -1034,7 +1049,7 @@ public: * @param ec A status code from the transport layer, zero on success, * non-zero otherwise. */ - void handle_write_frame(bool terminate, lib::error_code const & ec); + void handle_write_frame(lib::error_code const & ec); protected: void handle_transport_init(lib::error_code const & ec); @@ -1190,6 +1205,10 @@ private: */ void log_fail_result(); + // internal handler functions + read_handler m_handle_read_frame; + write_frame_handler m_write_frame_handler; + // static settings const std::string m_user_agent; diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index 9932ca6469..dfebae4692 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -942,12 +942,13 @@ void connection::handle_read_frame(const lib::error_code& ec, 1, m_buf, config::connection_read_buffer_size, - lib::bind( + /*lib::bind( &type::handle_read_frame, type::get_shared(), lib::placeholders::_1, lib::placeholders::_2 - ) + )*/ + m_handle_read_frame ); } @@ -1540,8 +1541,8 @@ void connection::write_frame() { m_write_flag = true; } - const std::string& header = m_current_msg->get_header(); - const std::string& payload = m_current_msg->get_payload(); + std::string const & header = m_current_msg->get_header(); + std::string const & payload = m_current_msg->get_payload(); m_send_buffer.push_back(transport::buffer(header.c_str(),header.size())); m_send_buffer.push_back(transport::buffer(payload.c_str(),payload.size())); @@ -1563,25 +1564,28 @@ void connection::write_frame() { } } + transport_con_type::async_write( m_send_buffer, - lib::bind( + /*lib::bind( &type::handle_write_frame, type::get_shared(), m_current_msg->get_terminal(), lib::placeholders::_1 - ) + )*/ + m_write_frame_handler ); } template -void connection::handle_write_frame(bool terminate, - const lib::error_code& ec) +void connection::handle_write_frame(lib::error_code const & ec) { if (m_alog.static_test(log::alevel::devel)) { m_alog.write(log::alevel::devel,"connection handle_write_frame"); } + bool terminate = m_current_msg->get_terminal(); + m_send_buffer.clear(); m_current_msg.reset();