From 5a1ae577d7d3cdbee00b3976744b7fef21dcb4f8 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Mon, 27 May 2019 11:41:20 +0200 Subject: [PATCH 01/15] add ping and pong to message handler, and optional pong timeout --- Release/include/cpprest/ws_client.h | 29 +++++++++- Release/include/cpprest/ws_msg.h | 13 +++++ .../src/websockets/client/ws_client_wspp.cpp | 55 ++++++++++++++++++- 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index 9c202d7f73..6a67c8fe91 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -79,7 +79,7 @@ class websocket_client_config /// /// Creates a websocket client configuration with default settings. /// - websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true) {} + websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true), m_pong_timeout(0) {} /// /// Get the web proxy object @@ -135,6 +135,18 @@ class websocket_client_config /// The User Agent to use, as a string. _ASYNCRTIMP void set_user_agent(const utf8string& user_agent); + /// + /// Sets the pong timeout to be used for the connection + /// + /// The timeout to use, as an unsigned integer. Default is the one of Websockettpp + void set_pong_timeout(const uint32_t timeout) { m_pong_timeout = timeout; } + + /// + /// Gets the pong timeout to be used for the connection + /// + /// Pong timeout value. + uint32_t pong_timeout() { return m_pong_timeout; } + /// /// Gets the headers of the HTTP request message used in the WebSocket protocol handshake. /// @@ -204,6 +216,7 @@ class websocket_client_config web::credentials m_credentials; web::http::http_headers m_headers; bool m_sni_enabled; + uint32_t m_pong_timeout; utf8string m_sni_hostname; bool m_validate_certificates; #if !defined(_WIN32) || !defined(__cplusplus_winrt) @@ -328,6 +341,8 @@ class websocket_client_callback_impl virtual void set_message_handler(const std::function& handler) = 0; + virtual void set_pong_timeout_handler(const std::function& handler) = 0; + virtual pplx::task close() = 0; virtual pplx::task close(websocket_close_status close_status, @@ -551,6 +566,18 @@ class websocket_callback_client m_client->set_message_handler(handler); } + /// + /// Set the pong timeout handler for notification of client websocket messages. + /// + /// A function representing the incoming websocket pong timeout handler. It's parameters are: + /// msg: a std::string value indicating the missed pong message + /// + /// If this handler is not set before connecting incoming messages will be missed. + void set_pong_timeout_handler(const std::function& handler) + { + m_client->set_pong_timeout_handler(handler); + } + /// /// Closes a websocket client connection, sends a close frame to the server and waits for a close message from the /// server. diff --git a/Release/include/cpprest/ws_msg.h b/Release/include/cpprest/ws_msg.h index 64cee7ae52..90927c6c88 100644 --- a/Release/include/cpprest/ws_msg.h +++ b/Release/include/cpprest/ws_msg.h @@ -64,6 +64,12 @@ class websocket_outgoing_message /// This is useful when the client side wants to check whether the server is alive. /// void set_pong_message() { this->set_message_pong(); } + + /// + /// Sets a the outgoing message to be a ping message. + /// This is useful when the client side wants to check whether the server is alive. + /// + void set_ping_message() { this->set_message_ping(); } #endif /// @@ -147,6 +153,13 @@ class websocket_outgoing_message m_length = static_cast(buffer.size()); m_body = buffer; } + void set_message_ping() + { + concurrency::streams::container_buffer buffer(""); + m_msg_type = websocket_message_type::ping; + m_length = static_cast(buffer.size()); + m_body = buffer; + } #endif void set_message(const concurrency::streams::container_buffer& buffer) diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index 326557fee5..2740c552fb 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -325,11 +325,56 @@ class wspp_callback_client : public websocket_client_callback_impl, } }); + client.set_ping_handler( + [this](websocketpp::connection_hdl, const std::string& msg) { + if (m_external_message_handler) + { + _ASSERTE(m_state >= CONNECTED && m_state < CLOSED); + websocket_incoming_message incoming_msg; + + incoming_msg.m_msg_type = websocket_message_type::ping; + incoming_msg.m_body = concurrency::streams::container_buffer(msg); + + m_external_message_handler(incoming_msg); + } + return true; + }); + + client.set_pong_handler( + [this](websocketpp::connection_hdl, const std::string& msg) { + if (m_external_message_handler) + { + _ASSERTE(m_state >= CONNECTED && m_state < CLOSED); + websocket_incoming_message incoming_msg; + + incoming_msg.m_msg_type = websocket_message_type::pong; + incoming_msg.m_body = concurrency::streams::container_buffer(msg); + + m_external_message_handler(incoming_msg); + } + }); + + client.set_pong_timeout_handler( + [this](websocketpp::connection_hdl, const std::string& msg) { + if (m_external_pong_timeout_handler) + { + _ASSERTE(m_state >= CONNECTED && m_state < CLOSED); + + m_external_pong_timeout_handler(msg); + } + }); + client.set_close_handler([this](websocketpp::connection_hdl con_hdl) { _ASSERTE(m_state != CLOSED); shutdown_wspp_impl(con_hdl, false); }); + // Set the pong timeout if set + if (m_config.pong_timeout() > 0) + { + client.set_pong_timeout(m_config.pong_timeout()); + } + // Set User Agent specified by the user. This needs to happen before any connection is created const auto& headers = m_config.headers(); @@ -434,12 +479,13 @@ class wspp_callback_client : public websocket_client_callback_impl, { case websocket_message_type::text_message: case websocket_message_type::binary_message: + case websocket_message_type::ping: case websocket_message_type::pong: break; default: return pplx::task_from_exception(websocket_exception("Message Type not supported.")); } const auto length = msg.m_length; - if (length == 0 && msg.m_msg_type != websocket_message_type::pong) + if (length == 0 && msg.m_msg_type != websocket_message_type::ping && msg.m_msg_type != websocket_message_type::pong) { return pplx::task_from_exception(websocket_exception("Cannot send empty message.")); } @@ -694,6 +740,7 @@ class wspp_callback_client : public websocket_client_callback_impl, case websocket_message_type::binary_message: client.send(this_client->m_con, sp_allocated.get(), length, websocketpp::frame::opcode::binary, ec); break; + case websocket_message_type::ping: client.ping(this_client->m_con, "", ec); break; case websocket_message_type::pong: client.pong(this_client->m_con, "", ec); break; default: // This case should have already been filtered above. @@ -716,6 +763,11 @@ class wspp_callback_client : public websocket_client_callback_impl, m_external_message_handler = handler; } + void set_pong_timeout_handler(const std::function& handler) + { + m_external_pong_timeout_handler = handler; + } + void set_close_handler( const std::function& handler) { @@ -777,6 +829,7 @@ class wspp_callback_client : public websocket_client_callback_impl, std::function m_external_message_handler; std::function m_external_close_handler; + std::function m_external_pong_timeout_handler; // Used to track if any of the OpenSSL server certificate verifications // failed. This can safely be tracked at the client level since connections From 7621bf72bcb4de5c58289cb721830753fe3211ca Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Mon, 27 May 2019 14:03:17 +0200 Subject: [PATCH 02/15] add pragma for winrt --- Release/include/cpprest/ws_client.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index 6a67c8fe91..778b620086 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -135,6 +135,7 @@ class websocket_client_config /// The User Agent to use, as a string. _ASYNCRTIMP void set_user_agent(const utf8string& user_agent); +#if !defined(__cplusplus_winrt) /// /// Sets the pong timeout to be used for the connection /// @@ -146,6 +147,7 @@ class websocket_client_config /// /// Pong timeout value. uint32_t pong_timeout() { return m_pong_timeout; } +#endif /// /// Gets the headers of the HTTP request message used in the WebSocket protocol handshake. @@ -216,9 +218,11 @@ class websocket_client_config web::credentials m_credentials; web::http::http_headers m_headers; bool m_sni_enabled; - uint32_t m_pong_timeout; utf8string m_sni_hostname; bool m_validate_certificates; +#if !defined(__cplusplus_winrt) + uint32_t m_pong_timeout; +#endif #if !defined(_WIN32) || !defined(__cplusplus_winrt) std::function m_ssl_context_callback; #endif @@ -341,8 +345,10 @@ class websocket_client_callback_impl virtual void set_message_handler(const std::function& handler) = 0; +#if !defined(__cplusplus_winrt) virtual void set_pong_timeout_handler(const std::function& handler) = 0; - +#endif + virtual pplx::task close() = 0; virtual pplx::task close(websocket_close_status close_status, From a0caf8b9c424acf5b0ea262b14683d7d3f5f0735 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Mon, 27 May 2019 14:41:33 +0200 Subject: [PATCH 03/15] remove pong timeout --- Release/include/cpprest/ws_client.h | 33 ------------------- .../src/websockets/client/ws_client_wspp.cpp | 21 ------------ 2 files changed, 54 deletions(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index 778b620086..a3e73486c1 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -135,20 +135,6 @@ class websocket_client_config /// The User Agent to use, as a string. _ASYNCRTIMP void set_user_agent(const utf8string& user_agent); -#if !defined(__cplusplus_winrt) - /// - /// Sets the pong timeout to be used for the connection - /// - /// The timeout to use, as an unsigned integer. Default is the one of Websockettpp - void set_pong_timeout(const uint32_t timeout) { m_pong_timeout = timeout; } - - /// - /// Gets the pong timeout to be used for the connection - /// - /// Pong timeout value. - uint32_t pong_timeout() { return m_pong_timeout; } -#endif - /// /// Gets the headers of the HTTP request message used in the WebSocket protocol handshake. /// @@ -220,9 +206,6 @@ class websocket_client_config bool m_sni_enabled; utf8string m_sni_hostname; bool m_validate_certificates; -#if !defined(__cplusplus_winrt) - uint32_t m_pong_timeout; -#endif #if !defined(_WIN32) || !defined(__cplusplus_winrt) std::function m_ssl_context_callback; #endif @@ -344,10 +327,6 @@ class websocket_client_callback_impl virtual pplx::task send(websocket_outgoing_message& msg) = 0; virtual void set_message_handler(const std::function& handler) = 0; - -#if !defined(__cplusplus_winrt) - virtual void set_pong_timeout_handler(const std::function& handler) = 0; -#endif virtual pplx::task close() = 0; @@ -572,18 +551,6 @@ class websocket_callback_client m_client->set_message_handler(handler); } - /// - /// Set the pong timeout handler for notification of client websocket messages. - /// - /// A function representing the incoming websocket pong timeout handler. It's parameters are: - /// msg: a std::string value indicating the missed pong message - /// - /// If this handler is not set before connecting incoming messages will be missed. - void set_pong_timeout_handler(const std::function& handler) - { - m_client->set_pong_timeout_handler(handler); - } - /// /// Closes a websocket client connection, sends a close frame to the server and waits for a close message from the /// server. diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index 2740c552fb..7f16b6133a 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -354,27 +354,11 @@ class wspp_callback_client : public websocket_client_callback_impl, } }); - client.set_pong_timeout_handler( - [this](websocketpp::connection_hdl, const std::string& msg) { - if (m_external_pong_timeout_handler) - { - _ASSERTE(m_state >= CONNECTED && m_state < CLOSED); - - m_external_pong_timeout_handler(msg); - } - }); - client.set_close_handler([this](websocketpp::connection_hdl con_hdl) { _ASSERTE(m_state != CLOSED); shutdown_wspp_impl(con_hdl, false); }); - // Set the pong timeout if set - if (m_config.pong_timeout() > 0) - { - client.set_pong_timeout(m_config.pong_timeout()); - } - // Set User Agent specified by the user. This needs to happen before any connection is created const auto& headers = m_config.headers(); @@ -763,11 +747,6 @@ class wspp_callback_client : public websocket_client_callback_impl, m_external_message_handler = handler; } - void set_pong_timeout_handler(const std::function& handler) - { - m_external_pong_timeout_handler = handler; - } - void set_close_handler( const std::function& handler) { From 7a810596d84fbda59fa8a5e334158cc3fca6569e Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Mon, 27 May 2019 14:52:49 +0200 Subject: [PATCH 04/15] clean pong timeout --- Release/src/websockets/client/ws_client_wspp.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index 7f16b6133a..f88ea9ee0b 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -808,7 +808,6 @@ class wspp_callback_client : public websocket_client_callback_impl, std::function m_external_message_handler; std::function m_external_close_handler; - std::function m_external_pong_timeout_handler; // Used to track if any of the OpenSSL server certificate verifications // failed. This can safely be tracked at the client level since connections From 9afadbacec3ea84d3ad448483dc0a6589d19b9f8 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Mon, 27 May 2019 14:54:16 +0200 Subject: [PATCH 05/15] clean pong timeout --- Release/include/cpprest/ws_client.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index a3e73486c1..73ea546ac4 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -79,7 +79,7 @@ class websocket_client_config /// /// Creates a websocket client configuration with default settings. /// - websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true), m_pong_timeout(0) {} + websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true) {} /// /// Get the web proxy object From 66e1806ad9db6458fe295a7f0d1a63909da8f42c Mon Sep 17 00:00:00 2001 From: Kumamon38 Date: Mon, 27 May 2019 15:23:24 +0200 Subject: [PATCH 06/15] typo --- Release/include/cpprest/ws_client.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index 73ea546ac4..9c202d7f73 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -327,7 +327,7 @@ class websocket_client_callback_impl virtual pplx::task send(websocket_outgoing_message& msg) = 0; virtual void set_message_handler(const std::function& handler) = 0; - + virtual pplx::task close() = 0; virtual pplx::task close(websocket_close_status close_status, From 0e6cca0ae35e29980dd9d730b260f65670bd9337 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Tue, 28 May 2019 09:53:37 +0200 Subject: [PATCH 07/15] add std::move --- Release/src/websockets/client/ws_client_wspp.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index f88ea9ee0b..f19158463c 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -333,7 +333,8 @@ class wspp_callback_client : public websocket_client_callback_impl, websocket_incoming_message incoming_msg; incoming_msg.m_msg_type = websocket_message_type::ping; - incoming_msg.m_body = concurrency::streams::container_buffer(msg); + // 'move' the payload into a container buffer to avoid any copies. + incoming_msg.m_body = concurrency::streams::container_buffer(std::move(msg)); m_external_message_handler(incoming_msg); } @@ -348,7 +349,8 @@ class wspp_callback_client : public websocket_client_callback_impl, websocket_incoming_message incoming_msg; incoming_msg.m_msg_type = websocket_message_type::pong; - incoming_msg.m_body = concurrency::streams::container_buffer(msg); + // 'move' the payload into a container buffer to avoid any copies. + incoming_msg.m_body = concurrency::streams::container_buffer(std::move(msg)); m_external_message_handler(incoming_msg); } From 2f4034dd98db9ba3f063a672809073c3e53567a4 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Tue, 28 May 2019 10:49:44 +0200 Subject: [PATCH 08/15] remove std::move --- Release/src/websockets/client/ws_client_wspp.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index f19158463c..f88ea9ee0b 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -333,8 +333,7 @@ class wspp_callback_client : public websocket_client_callback_impl, websocket_incoming_message incoming_msg; incoming_msg.m_msg_type = websocket_message_type::ping; - // 'move' the payload into a container buffer to avoid any copies. - incoming_msg.m_body = concurrency::streams::container_buffer(std::move(msg)); + incoming_msg.m_body = concurrency::streams::container_buffer(msg); m_external_message_handler(incoming_msg); } @@ -349,8 +348,7 @@ class wspp_callback_client : public websocket_client_callback_impl, websocket_incoming_message incoming_msg; incoming_msg.m_msg_type = websocket_message_type::pong; - // 'move' the payload into a container buffer to avoid any copies. - incoming_msg.m_body = concurrency::streams::container_buffer(std::move(msg)); + incoming_msg.m_body = concurrency::streams::container_buffer(msg); m_external_message_handler(incoming_msg); } From 5914029df3b635a40e8fc41e34e22fa4ff985ad6 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Thu, 6 Jun 2019 11:27:05 +0200 Subject: [PATCH 09/15] required modifications --- Release/include/cpprest/ws_msg.h | 27 +++++++++++-------- .../src/websockets/client/ws_client_wspp.cpp | 4 +-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Release/include/cpprest/ws_msg.h b/Release/include/cpprest/ws_msg.h index 90927c6c88..e6135362f3 100644 --- a/Release/include/cpprest/ws_msg.h +++ b/Release/include/cpprest/ws_msg.h @@ -60,16 +60,23 @@ class websocket_outgoing_message public: #if !defined(__cplusplus_winrt) /// - /// Sets a the outgoing message to be an unsolicited pong message. - /// This is useful when the client side wants to check whether the server is alive. + /// Sets the outgoing message to be a ping message. /// - void set_pong_message() { this->set_message_pong(); } + /// UTF-8 String containing the optional ping message. + void set_ping_message(const std::string& data = "") + { + this->set_message_ping(concurrency::streams::container_buffer(data)); + } /// - /// Sets a the outgoing message to be a ping message. + /// Sets the outgoing message to be an unsolicited pong message. /// This is useful when the client side wants to check whether the server is alive. /// - void set_ping_message() { this->set_message_ping(); } + /// UTF-8 String containing the optional pong message. + void set_pong_message(const std::string& data = "") + { + this->set_message_pong(concurrency::streams::container_buffer(data)); + } #endif /// @@ -146,17 +153,15 @@ class websocket_outgoing_message const pplx::task_completion_event& body_sent() const { return m_body_sent; } #if !defined(__cplusplus_winrt) - void set_message_pong() + void set_message_ping(const concurrency::streams::container_buffer& buffer) { - concurrency::streams::container_buffer buffer(""); - m_msg_type = websocket_message_type::pong; + m_msg_type = websocket_message_type::ping; m_length = static_cast(buffer.size()); m_body = buffer; } - void set_message_ping() + void set_message_pong(const concurrency::streams::container_buffer& buffer) { - concurrency::streams::container_buffer buffer(""); - m_msg_type = websocket_message_type::ping; + m_msg_type = websocket_message_type::pong; m_length = static_cast(buffer.size()); m_body = buffer; } diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index f88ea9ee0b..8bcdb3c6a2 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -724,8 +724,8 @@ class wspp_callback_client : public websocket_client_callback_impl, case websocket_message_type::binary_message: client.send(this_client->m_con, sp_allocated.get(), length, websocketpp::frame::opcode::binary, ec); break; - case websocket_message_type::ping: client.ping(this_client->m_con, "", ec); break; - case websocket_message_type::pong: client.pong(this_client->m_con, "", ec); break; + case websocket_message_type::ping: client.ping(this_client->m_con, *(std::string*)sp_allocated.get(), ec); break; + case websocket_message_type::pong: client.pong(this_client->m_con, *(std::string*)sp_allocated.get(), ec); break; default: // This case should have already been filtered above. std::abort(); From 61c5ee2ee57881a02a7e9128ce87641c71178ce8 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Thu, 6 Jun 2019 11:55:05 +0200 Subject: [PATCH 10/15] add configuration to auto enable pong answers --- Release/include/cpprest/ws_client.h | 14 +++++++++++++- Release/src/websockets/client/ws_client_wspp.cpp | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index 9c202d7f73..c9a463c10b 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -79,7 +79,7 @@ class websocket_client_config /// /// Creates a websocket client configuration with default settings. /// - websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true) {} + websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true), m_auto_pong_enabled(false) {} /// /// Get the web proxy object @@ -105,6 +105,17 @@ class websocket_client_config /// The client credentials. void set_credentials(const web::credentials& cred) { m_credentials = cred; } + /// + /// Turn on automatic pong answer to incoming ping request + /// + void set_auto_pong_enabled() { m_auto_pong_enabled = true; } + + /// + /// Determines if the automatic Pong answer to Ping is enabled. + /// + /// True if enabled, false otherwise. + bool is_auto_pong_enabled() const { return m_auto_pong_enabled; } + /// /// Disables Server Name Indication (SNI). Default is on. /// @@ -206,6 +217,7 @@ class websocket_client_config bool m_sni_enabled; utf8string m_sni_hostname; bool m_validate_certificates; + bool m_auto_pong_enabled; #if !defined(_WIN32) || !defined(__cplusplus_winrt) std::function m_ssl_context_callback; #endif diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index 8bcdb3c6a2..645b6bb5a0 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -337,7 +337,7 @@ class wspp_callback_client : public websocket_client_callback_impl, m_external_message_handler(incoming_msg); } - return true; + return m_config.is_auto_pong_enabled(); }); client.set_pong_handler( From 65a3d362bde2866000ea98dae4feed5bdd6c4ffd Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Thu, 6 Jun 2019 12:00:27 +0200 Subject: [PATCH 11/15] rename --- Release/include/cpprest/ws_client.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index c9a463c10b..e88e798ab3 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -108,7 +108,7 @@ class websocket_client_config /// /// Turn on automatic pong answer to incoming ping request /// - void set_auto_pong_enabled() { m_auto_pong_enabled = true; } + void enable_auto_pong() { m_auto_pong_enabled = true; } /// /// Determines if the automatic Pong answer to Ping is enabled. From ce53a155caa82d9ba42507fa03e697026d31b8bf Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Thu, 6 Jun 2019 14:07:26 +0200 Subject: [PATCH 12/15] fixes, clean, and std::string for ping/pong --- Release/include/cpprest/ws_client.h | 14 +------------- Release/include/cpprest/ws_msg.h | 2 +- Release/src/websockets/client/ws_client_wspp.cpp | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index e88e798ab3..9c202d7f73 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -79,7 +79,7 @@ class websocket_client_config /// /// Creates a websocket client configuration with default settings. /// - websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true), m_auto_pong_enabled(false) {} + websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true) {} /// /// Get the web proxy object @@ -105,17 +105,6 @@ class websocket_client_config /// The client credentials. void set_credentials(const web::credentials& cred) { m_credentials = cred; } - /// - /// Turn on automatic pong answer to incoming ping request - /// - void enable_auto_pong() { m_auto_pong_enabled = true; } - - /// - /// Determines if the automatic Pong answer to Ping is enabled. - /// - /// True if enabled, false otherwise. - bool is_auto_pong_enabled() const { return m_auto_pong_enabled; } - /// /// Disables Server Name Indication (SNI). Default is on. /// @@ -217,7 +206,6 @@ class websocket_client_config bool m_sni_enabled; utf8string m_sni_hostname; bool m_validate_certificates; - bool m_auto_pong_enabled; #if !defined(_WIN32) || !defined(__cplusplus_winrt) std::function m_ssl_context_callback; #endif diff --git a/Release/include/cpprest/ws_msg.h b/Release/include/cpprest/ws_msg.h index e6135362f3..c9d3731a24 100644 --- a/Release/include/cpprest/ws_msg.h +++ b/Release/include/cpprest/ws_msg.h @@ -61,6 +61,7 @@ class websocket_outgoing_message #if !defined(__cplusplus_winrt) /// /// Sets the outgoing message to be a ping message. + /// This is useful when the client side wants to check whether the server is alive. /// /// UTF-8 String containing the optional ping message. void set_ping_message(const std::string& data = "") @@ -70,7 +71,6 @@ class websocket_outgoing_message /// /// Sets the outgoing message to be an unsolicited pong message. - /// This is useful when the client side wants to check whether the server is alive. /// /// UTF-8 String containing the optional pong message. void set_pong_message(const std::string& data = "") diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index 645b6bb5a0..5ef74aaf1e 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -337,7 +337,7 @@ class wspp_callback_client : public websocket_client_callback_impl, m_external_message_handler(incoming_msg); } - return m_config.is_auto_pong_enabled(); + return true; }); client.set_pong_handler( @@ -724,8 +724,18 @@ class wspp_callback_client : public websocket_client_callback_impl, case websocket_message_type::binary_message: client.send(this_client->m_con, sp_allocated.get(), length, websocketpp::frame::opcode::binary, ec); break; - case websocket_message_type::ping: client.ping(this_client->m_con, *(std::string*)sp_allocated.get(), ec); break; - case websocket_message_type::pong: client.pong(this_client->m_con, *(std::string*)sp_allocated.get(), ec); break; + case websocket_message_type::ping: + { + std::string s(reinterpret_cast(sp_allocated.get()), length); + client.ping(this_client->m_con, s, ec); + break; + } + case websocket_message_type::pong: + { + std::string s(reinterpret_cast(sp_allocated.get()), length); + client.pong(this_client->m_con, s, ec); + break; + } default: // This case should have already been filtered above. std::abort(); From ec70dc7c1f73f773ccbbdabbf42154ecde624585 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Thu, 6 Jun 2019 15:23:02 +0200 Subject: [PATCH 13/15] tests from garethsb-sony --- .../websockets/client/send_msg_tests.cpp | 51 +++++++++++++++++-- .../utilities/test_websocket_server.cpp | 14 +++++ .../utilities/test_websocket_server.h | 1 + 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/Release/tests/functional/websockets/client/send_msg_tests.cpp b/Release/tests/functional/websockets/client/send_msg_tests.cpp index e99bccd03c..eed1dedf94 100644 --- a/Release/tests/functional/websockets/client/send_msg_tests.cpp +++ b/Release/tests/functional/websockets/client/send_msg_tests.cpp @@ -104,16 +104,32 @@ SUITE(send_msg_tests) } template - pplx::task send_pong_msg_helper(SocketClientClass & client, web::uri uri, test_websocket_server & server) + pplx::task send_ping_msg_helper(SocketClientClass & client, web::uri uri, test_websocket_server & server, + const std::string& body = "") { server.next_message( - [](test_websocket_msg msg) // Handler to verify the message sent by the client. - { websocket_asserts::assert_message_equals(msg, "", test_websocket_message_type::WEB_SOCKET_PONG_TYPE); }); + [body](test_websocket_msg msg) // Handler to verify the message sent by the client. + { websocket_asserts::assert_message_equals(msg, body, test_websocket_message_type::WEB_SOCKET_PING_TYPE); }); client.connect(uri).wait(); websocket_outgoing_message msg; - msg.set_pong_message(); + msg.set_ping_message(body); + return client.send(msg); + } + + template + pplx::task send_pong_msg_helper(SocketClientClass & client, web::uri uri, test_websocket_server & server, + const std::string& body = "") + { + server.next_message( + [body](test_websocket_msg msg) // Handler to verify the message sent by the client. + { websocket_asserts::assert_message_equals(msg, body, test_websocket_message_type::WEB_SOCKET_PONG_TYPE); }); + + client.connect(uri).wait(); + + websocket_outgoing_message msg; + msg.set_pong_message(body); return client.send(msg); } @@ -493,6 +509,24 @@ SUITE(send_msg_tests) } #if !defined(__cplusplus_winrt) + // Send a ping message to the server + TEST_FIXTURE(uri_address, send_ping_msg) + { + test_websocket_server server; + websocket_client client; + send_ping_msg_helper(client, m_uri, server).wait(); + client.close().wait(); + } + + // Send a ping message to the server with a body + TEST_FIXTURE(uri_address, send_ping_msg_body) + { + test_websocket_server server; + websocket_client client; + send_ping_msg_helper(client, m_uri, server, "abcdefghijklmnopqrstuvwxyz").wait(); + client.close().wait(); + } + // Send an unsolicited pong message to the server TEST_FIXTURE(uri_address, send_pong_msg) { @@ -502,6 +536,15 @@ SUITE(send_msg_tests) client.close().wait(); } + // Send an unsolicited pong message to the server with a body + TEST_FIXTURE(uri_address, send_pong_msg_body) + { + test_websocket_server server; + websocket_client client; + send_pong_msg_helper(client, m_uri, server, "abcdefghijklmnopqrstuvwxyz").wait(); + client.close().wait(); + } + // Send an unsolicited pong message to the server with websocket_callback_client TEST_FIXTURE(uri_address, send_pong_msg_callback_client) { diff --git a/Release/tests/functional/websockets/utilities/test_websocket_server.cpp b/Release/tests/functional/websockets/utilities/test_websocket_server.cpp index 151d03bca0..863376f22a 100644 --- a/Release/tests/functional/websockets/utilities/test_websocket_server.cpp +++ b/Release/tests/functional/websockets/utilities/test_websocket_server.cpp @@ -122,6 +122,20 @@ class _test_websocket_server m_server_connected.set_exception(std::runtime_error("Connection attempt failed.")); }); + m_srv.set_ping_handler([this](websocketpp::connection_hdl hdl, std::string input) { + auto fn = m_test_srv->get_next_message_handler(); + assert(fn); + + test_websocket_msg wsmsg; + + wsmsg.set_data(std::vector(input.begin(), input.end())); + + wsmsg.set_msg_type(WEB_SOCKET_PING_TYPE); + fn(wsmsg); + + return true; + }); + m_srv.set_pong_handler([this](websocketpp::connection_hdl hdl, std::string input) { auto fn = m_test_srv->get_next_message_handler(); assert(fn); diff --git a/Release/tests/functional/websockets/utilities/test_websocket_server.h b/Release/tests/functional/websockets/utilities/test_websocket_server.h index 50489e1a9d..23fbea12e2 100644 --- a/Release/tests/functional/websockets/utilities/test_websocket_server.h +++ b/Release/tests/functional/websockets/utilities/test_websocket_server.h @@ -46,6 +46,7 @@ enum test_websocket_message_type WEB_SOCKET_UTF8_MESSAGE_TYPE, WEB_SOCKET_UTF8_FRAGMENT_TYPE, WEB_SOCKET_CLOSE_TYPE, + WEB_SOCKET_PING_TYPE, WEB_SOCKET_PONG_TYPE }; From 01c436ea0611a5b0e93998b306a402bf3bcecfd1 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Tue, 11 Jun 2019 08:59:41 +0200 Subject: [PATCH 14/15] change default value to std::string() --- Release/include/cpprest/ws_msg.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Release/include/cpprest/ws_msg.h b/Release/include/cpprest/ws_msg.h index c9d3731a24..d0620eea1b 100644 --- a/Release/include/cpprest/ws_msg.h +++ b/Release/include/cpprest/ws_msg.h @@ -64,7 +64,7 @@ class websocket_outgoing_message /// This is useful when the client side wants to check whether the server is alive. /// /// UTF-8 String containing the optional ping message. - void set_ping_message(const std::string& data = "") + void set_ping_message(const std::string& data = std::string()) { this->set_message_ping(concurrency::streams::container_buffer(data)); } @@ -73,7 +73,7 @@ class websocket_outgoing_message /// Sets the outgoing message to be an unsolicited pong message. /// /// UTF-8 String containing the optional pong message. - void set_pong_message(const std::string& data = "") + void set_pong_message(const std::string& data = std::string()) { this->set_message_pong(concurrency::streams::container_buffer(data)); } From e70913de55b3a409fed000b93fac4fe5f46a3313 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Tue, 11 Jun 2019 10:29:49 +0200 Subject: [PATCH 15/15] change default value to {} --- Release/include/cpprest/ws_msg.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Release/include/cpprest/ws_msg.h b/Release/include/cpprest/ws_msg.h index d0620eea1b..73e968a001 100644 --- a/Release/include/cpprest/ws_msg.h +++ b/Release/include/cpprest/ws_msg.h @@ -64,7 +64,7 @@ class websocket_outgoing_message /// This is useful when the client side wants to check whether the server is alive. /// /// UTF-8 String containing the optional ping message. - void set_ping_message(const std::string& data = std::string()) + void set_ping_message(const std::string& data = {}) { this->set_message_ping(concurrency::streams::container_buffer(data)); } @@ -73,7 +73,7 @@ class websocket_outgoing_message /// Sets the outgoing message to be an unsolicited pong message. /// /// UTF-8 String containing the optional pong message. - void set_pong_message(const std::string& data = std::string()) + void set_pong_message(const std::string& data = {}) { this->set_message_pong(concurrency::streams::container_buffer(data)); }