Fix transmit queue synchronization bug

This commit is contained in:
Frank A. Krueger 2017-12-09 17:49:11 -08:00
parent a96c3977f6
commit 9b09f6992e
2 changed files with 16 additions and 10 deletions

View File

@ -1,5 +1,5 @@
var debug = false; var debug = true;
const nodes = {}; const nodes = {};

View File

@ -671,6 +671,7 @@ namespace Ooui
// //
// Add it to the queue // Add it to the queue
// //
//Console.WriteLine ($"QM {message.MessageType} {message.TargetId} {message.Key} {message.Value}");
queuedMessages.Add (message); queuedMessages.Add (message);
} }
@ -689,19 +690,24 @@ namespace Ooui
// Dequeue as many messages as we can // Dequeue as many messages as we can
// //
var messagesToSend = new List<Message> (); var messagesToSend = new List<Message> ();
System.Runtime.CompilerServices.ConfiguredTaskAwaitable task;
lock (queuedMessages) { lock (queuedMessages) {
messagesToSend.AddRange (queuedMessages); messagesToSend.AddRange (queuedMessages);
queuedMessages.Clear (); queuedMessages.Clear ();
}
if (messagesToSend.Count == 0)
return;
// if (messagesToSend.Count == 0)
// Now actually send this message return;
//
var json = Newtonsoft.Json.JsonConvert.SerializeObject (messagesToSend); //
var outputBuffer = new ArraySegment<byte> (Encoding.UTF8.GetBytes (json)); // Now actually send this message
await webSocket.SendAsync (outputBuffer, WebSocketMessageType.Text, true, token).ConfigureAwait (false); // Do this while locked to make sure SendAsync is called in the right order
//
var json = Newtonsoft.Json.JsonConvert.SerializeObject (messagesToSend);
var outputBuffer = new ArraySegment<byte> (Encoding.UTF8.GetBytes (json));
//Console.WriteLine ("TRANSMIT " + json);
task = webSocket.SendAsync (outputBuffer, WebSocketMessageType.Text, true, token).ConfigureAwait (false);
}
await task;
} }
catch (Exception ex) { catch (Exception ex) {
Error ("Failed to send queued messages, aborting session", ex); Error ("Failed to send queued messages, aborting session", ex);