From e5b0b7e9a78ece664b543ccbb9b8d319a1cdaaa6 Mon Sep 17 00:00:00 2001 From: Tom Ritchford Date: Wed, 4 Feb 2015 14:35:50 -0500 Subject: [PATCH] Expose a method and add a handler in websocketpp. * Expose websocketpp::transport::asio::connection::get_strand(). * Add new send_empty_handler to websocketpp::endpoint. --- src/websocketpp/websocketpp/connection.hpp | 18 ++++++++++++++++++ src/websocketpp/websocketpp/endpoint.hpp | 6 ++++++ .../websocketpp/impl/connection_impl.hpp | 6 ++++++ .../websocketpp/transport/asio/connection.hpp | 3 ++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/websocketpp/websocketpp/connection.hpp b/src/websocketpp/websocketpp/connection.hpp index 37880ce1c..d3ddf7156 100644 --- a/src/websocketpp/websocketpp/connection.hpp +++ b/src/websocketpp/websocketpp/connection.hpp @@ -150,6 +150,13 @@ typedef lib::function validate_handler; */ typedef lib::function http_handler; +/// The type and function signature of an send_empty handler +/** + * The send_empty handler is called each time the send queue becomes empty of + * messages. + */ +typedef lib::function send_empty_handler; + // typedef lib::function read_handler; typedef lib::function write_frame_handler; @@ -458,6 +465,16 @@ public: m_message_handler = h; } + /// Set send empty handler + /** + * The send empty handler is called when the message queue is empty. + * + * @param h The new message_handler + */ + void set_send_empty_handler(send_empty_handler h) { + m_send_empty_handler = h; + } + ////////////////////////////////////////// // Connection timeouts and other limits // ////////////////////////////////////////// @@ -1382,6 +1399,7 @@ private: http_handler m_http_handler; validate_handler m_validate_handler; message_handler m_message_handler; + send_empty_handler m_send_empty_handler; /// constant values long m_open_handshake_timeout_dur; diff --git a/src/websocketpp/websocketpp/endpoint.hpp b/src/websocketpp/websocketpp/endpoint.hpp index 3bbe9da18..773c4e134 100644 --- a/src/websocketpp/websocketpp/endpoint.hpp +++ b/src/websocketpp/websocketpp/endpoint.hpp @@ -271,6 +271,11 @@ public: scoped_lock_type guard(m_mutex); m_message_handler = h; } + void set_send_empty_handler(send_empty_handler h) { + m_alog.write(log::alevel::devel,"set_send_empty_handler"); + scoped_lock_type guard(m_mutex); + m_send_empty_handler = h; + } ////////////////////////////////////////// // Connection timeouts and other limits // @@ -560,6 +565,7 @@ private: http_handler m_http_handler; validate_handler m_validate_handler; message_handler m_message_handler; + send_empty_handler m_send_empty_handler; long m_open_handshake_timeout_dur; long m_close_handshake_timeout_dur; diff --git a/src/websocketpp/websocketpp/impl/connection_impl.hpp b/src/websocketpp/websocketpp/impl/connection_impl.hpp index 1f29f4eff..a43e69274 100644 --- a/src/websocketpp/websocketpp/impl/connection_impl.hpp +++ b/src/websocketpp/websocketpp/impl/connection_impl.hpp @@ -1575,7 +1575,9 @@ void connection::write_frame() { // pull off all the messages that are ready to write. // stop if we get a message marked terminal message_ptr next_message = write_pop(); + bool saw_message = false; while (next_message) { + saw_message = true; m_current_msgs.push_back(next_message); if (!next_message->get_terminal()) { next_message = write_pop(); @@ -1586,6 +1588,10 @@ void connection::write_frame() { if (m_current_msgs.empty()) { // there was nothing to send + // If we just made the transition to empty, send out + // "send_empty" callback. + if (saw_message) + m_send_empty_handler(m_connection_hdl); return; } else { // At this point we own the next messages to be sent and are diff --git a/src/websocketpp/websocketpp/transport/asio/connection.hpp b/src/websocketpp/websocketpp/transport/asio/connection.hpp index f4341019f..3b787d32d 100644 --- a/src/websocketpp/websocketpp/transport/asio/connection.hpp +++ b/src/websocketpp/websocketpp/transport/asio/connection.hpp @@ -339,12 +339,13 @@ public: callback(lib::error_code()); } } -protected: + /// Get a pointer to this connection's strand strand_ptr get_strand() { return m_strand; } +protected: /// Initialize transport for reading /** * init_asio is called once immediately after construction to initialize