@@ -85,8 +85,7 @@ class winrt_callback_client : public websocket_client_callback_impl, public std:
85
85
public:
86
86
winrt_callback_client (websocket_client_config config) :
87
87
websocket_client_callback_impl (std::move(config)),
88
- m_connected (false ),
89
- m_num_sends (0 )
88
+ m_connected (false )
90
89
{
91
90
m_msg_websocket = ref new MessageWebSocket ();
92
91
@@ -243,17 +242,24 @@ class winrt_callback_client : public websocket_client_callback_impl, public std:
243
242
return pplx::task_from_exception<void >(websocket_exception (" Message size too large. Ensure message length is less than UINT_MAX." ));
244
243
}
245
244
246
- if (++m_num_sends == 1 ) // No sends in progress
245
+ bool msg_pending = false ;
247
246
{
248
- // Start sending the message
249
- send_msg (msg);
250
- }
251
- else
252
- {
253
- // Only actually have to take the lock if touching the queue.
254
247
std::lock_guard<std::mutex> lock (m_send_lock);
248
+ if (m_outgoing_msg_queue.size () > 0 )
249
+ {
250
+ msg_pending = true ;
251
+ }
252
+
255
253
m_outgoing_msg_queue.push (msg);
256
254
}
255
+
256
+ // No sends in progress
257
+ if (msg_pending == false )
258
+ {
259
+ // Start sending the message
260
+ send_msg (msg);
261
+ }
262
+
257
263
return pplx::create_task (msg.body_sent ());
258
264
}
259
265
@@ -385,15 +391,24 @@ class winrt_callback_client : public websocket_client_callback_impl, public std:
385
391
msg.signal_body_sent ();
386
392
}
387
393
388
- if (--this_client->m_num_sends > 0 )
394
+ bool msg_pending = false ;
395
+ websocket_outgoing_message next_msg;
389
396
{
390
397
// Only hold the lock when actually touching the queue.
391
- websocket_outgoing_message next_msg;
398
+ std::lock_guard<std::mutex> lock (this_client->m_send_lock );
399
+
400
+ // First message in queue has been sent
401
+ this_client->m_outgoing_msg_queue .pop ();
402
+
403
+ if (this_client->m_outgoing_msg_queue .size () > 0 )
392
404
{
393
- std::lock_guard<std::mutex> lock (this_client->m_send_lock );
394
405
next_msg = this_client->m_outgoing_msg_queue .front ();
395
- this_client-> m_outgoing_msg_queue . pop () ;
406
+ msg_pending = true ;
396
407
}
408
+ }
409
+
410
+ if (msg_pending)
411
+ {
397
412
this_client->send_msg (next_msg);
398
413
}
399
414
});
@@ -443,11 +458,8 @@ class winrt_callback_client : public websocket_client_callback_impl, public std:
443
458
// The implementation has to ensure ordering of send requests
444
459
std::mutex m_send_lock;
445
460
446
- // Queue to order the sends
461
+ // Queue to track pending sends
447
462
std::queue<websocket_outgoing_message> m_outgoing_msg_queue;
448
-
449
- // Number of sends in progress and queued up.
450
- std::atomic<int > m_num_sends;
451
463
};
452
464
453
465
void ReceiveContext::OnReceive (MessageWebSocket^ sender, MessageWebSocketMessageReceivedEventArgs^ args)
0 commit comments