diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index ba41dd3134..2d7a15354a 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -114,8 +114,7 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: public: wspp_callback_client(websocket_client_config config) : websocket_client_callback_impl(std::move(config)), - m_state(CREATED), - m_num_sends(0) + m_state(CREATED) #if defined(__APPLE__) || (defined(ANDROID) || defined(__ANDROID__)) || defined(_WIN32) , m_openssl_failed(false) #endif @@ -415,18 +414,22 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: return pplx::task_from_exception(websocket_exception("Message size too large. Ensure message length is less than UINT_MAX.")); } + bool msg_pending = false; { - if (++m_num_sends == 1) // No sends in progress - { - // Start sending the message - send_msg(msg); - } - else - { - // Only actually have to take the lock if touching the queue. - std::lock_guard lock(m_send_lock); - m_outgoing_msg_queue.push(msg); - } + std::lock_guard lock(m_send_lock); + if (m_outgoing_msg_queue.size() > 0) + { + msg_pending = true; + } + + m_outgoing_msg_queue.push(msg); + } + + // No sends in progress + if (msg_pending == false) + { + // Start sending the message + send_msg(msg); } return pplx::create_task(msg.body_sent()); @@ -562,16 +565,25 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: msg.signal_body_sent(); } - if (--this_client->m_num_sends > 0) + bool msg_pending = false; + websocket_outgoing_message next_msg; { - // Only hold the lock when actually touching the queue. - websocket_outgoing_message next_msg; - { - std::lock_guard lock(this_client->m_send_lock); - next_msg = this_client->m_outgoing_msg_queue.front(); - this_client->m_outgoing_msg_queue.pop(); - } - this_client->send_msg(next_msg); + // Only hold the lock when actually touching the queue. + std::lock_guard lock(this_client->m_send_lock); + + // First message in queue has been sent + this_client->m_outgoing_msg_queue.pop(); + + if (this_client->m_outgoing_msg_queue.size() > 0) + { + next_msg = this_client->m_outgoing_msg_queue.front(); + msg_pending = true; + } + } + + if (msg_pending) + { + this_client->send_msg(next_msg); } }); } @@ -763,12 +775,9 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: // Guards access to m_outgoing_msg_queue std::mutex m_send_lock; - // Queue to order the sends + // Queue to track pending sends std::queue m_outgoing_msg_queue; - // Number of sends in progress and queued up. - std::atomic m_num_sends; - // External callback for handling received and close event std::function m_external_message_handler; std::function m_external_close_handler;