Skip to content

Commit 8b878f7

Browse files
committed
2 parents aa2d08f + 5e5ecaa commit 8b878f7

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

Release/src/websockets/client/ws_client_wspp.cpp

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
117117
public:
118118
wspp_callback_client(websocket_client_config config) :
119119
websocket_client_callback_impl(std::move(config)),
120-
m_state(CREATED),
121-
m_num_sends(0)
120+
m_state(CREATED)
122121
#if defined(__APPLE__) || (defined(ANDROID) || defined(__ANDROID__)) || defined(_WIN32)
123122
, m_openssl_failed(false)
124123
#endif
@@ -418,18 +417,22 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
418417
return pplx::task_from_exception<void>(websocket_exception("Message size too large. Ensure message length is less than UINT_MAX."));
419418
}
420419

420+
bool msg_pending = false;
421421
{
422-
if (++m_num_sends == 1) // No sends in progress
423-
{
424-
// Start sending the message
425-
send_msg(msg);
426-
}
427-
else
428-
{
429-
// Only actually have to take the lock if touching the queue.
430-
std::lock_guard<std::mutex> lock(m_send_lock);
431-
m_outgoing_msg_queue.push(msg);
432-
}
422+
std::lock_guard<std::mutex> lock(m_send_lock);
423+
if (m_outgoing_msg_queue.size() > 0)
424+
{
425+
msg_pending = true;
426+
}
427+
428+
m_outgoing_msg_queue.push(msg);
429+
}
430+
431+
// No sends in progress
432+
if (msg_pending == false)
433+
{
434+
// Start sending the message
435+
send_msg(msg);
433436
}
434437

435438
return pplx::create_task(msg.body_sent());
@@ -565,16 +568,25 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
565568
msg.signal_body_sent();
566569
}
567570

568-
if (--this_client->m_num_sends > 0)
571+
bool msg_pending = false;
572+
websocket_outgoing_message next_msg;
569573
{
570-
// Only hold the lock when actually touching the queue.
571-
websocket_outgoing_message next_msg;
572-
{
573-
std::lock_guard<std::mutex> lock(this_client->m_send_lock);
574-
next_msg = this_client->m_outgoing_msg_queue.front();
575-
this_client->m_outgoing_msg_queue.pop();
576-
}
577-
this_client->send_msg(next_msg);
574+
// Only hold the lock when actually touching the queue.
575+
std::lock_guard<std::mutex> lock(this_client->m_send_lock);
576+
577+
// First message in queue has been sent
578+
this_client->m_outgoing_msg_queue.pop();
579+
580+
if (this_client->m_outgoing_msg_queue.size() > 0)
581+
{
582+
next_msg = this_client->m_outgoing_msg_queue.front();
583+
msg_pending = true;
584+
}
585+
}
586+
587+
if (msg_pending)
588+
{
589+
this_client->send_msg(next_msg);
578590
}
579591
});
580592
}
@@ -766,12 +778,9 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
766778
// Guards access to m_outgoing_msg_queue
767779
std::mutex m_send_lock;
768780

769-
// Queue to order the sends
781+
// Queue to track pending sends
770782
std::queue<websocket_outgoing_message> m_outgoing_msg_queue;
771783

772-
// Number of sends in progress and queued up.
773-
std::atomic<int> m_num_sends;
774-
775784
// External callback for handling received and close event
776785
std::function<void(websocket_incoming_message)> m_external_message_handler;
777786
std::function<void(websocket_close_status, const utility::string_t&, const std::error_code&)> m_external_close_handler;

0 commit comments

Comments
 (0)