Reduce the latency of sending messages

This commit is contained in:
BruceChen 2022-08-31 22:52:05 +08:00
parent 98dd645fb5
commit a13af47b3e
2 changed files with 29 additions and 12 deletions

View file

@ -281,6 +281,20 @@ namespace MinecraftClient
} }
} }
/// <summary>
/// Retrieve messages from the queue and send.
/// Note: requires external locking.
/// </summary>
private void TrySendMessageToServer()
{
while (chatQueue.Count > 0 && nextMessageSendTime < DateTime.Now)
{
string text = chatQueue.Dequeue();
handler.SendChatMessage(text, playerKeyPair);
nextMessageSendTime = DateTime.Now + Settings.messageCooldown;
}
}
/// <summary> /// <summary>
/// Called ~10 times per second by the protocol handler /// Called ~10 times per second by the protocol handler
/// </summary> /// </summary>
@ -295,22 +309,16 @@ namespace MinecraftClient
} }
catch (Exception e) catch (Exception e)
{ {
if (!(e is ThreadAbortException)) if (e is not ThreadAbortException)
{
Log.Warn("Update: Got error from " + bot.ToString() + ": " + e.ToString()); Log.Warn("Update: Got error from " + bot.ToString() + ": " + e.ToString());
} else
else throw; //ThreadAbortException should not be caught throw; //ThreadAbortException should not be caught
} }
} }
lock (chatQueue) lock (chatQueue)
{ {
if (chatQueue.Count > 0 && nextMessageSendTime < DateTime.Now) TrySendMessageToServer();
{
string text = chatQueue.Dequeue();
handler.SendChatMessage(text, playerKeyPair);
nextMessageSendTime = DateTime.Now + Settings.messageCooldown;
}
} }
if (terrainAndMovementsEnabled && locationReceived) if (terrainAndMovementsEnabled && locationReceived)
@ -539,7 +547,7 @@ namespace MinecraftClient
if (ConsoleIO.BasicIO && text.Length > 0 && text[0] == (char)0x00) if (ConsoleIO.BasicIO && text.Length > 0 && text[0] == (char)0x00)
{ {
//Process a request from the GUI //Process a request from the GUI
string[] command = text.Substring(1).Split((char)0x00); string[] command = text[1..].Split((char)0x00);
switch (command[0].ToLower()) switch (command[0].ToLower())
{ {
case "autocomplete": case "autocomplete":
@ -1148,6 +1156,7 @@ namespace MinecraftClient
} }
} }
else chatQueue.Enqueue(text); else chatQueue.Enqueue(text);
TrySendMessageToServer();
} }
} }

View file

@ -229,14 +229,22 @@ namespace MinecraftClient.Protocol.Handlers
{ {
try try
{ {
while (socketWrapper.HasDataAvailable() && !cancelToken.IsCancellationRequested) while (socketWrapper.HasDataAvailable())
{
packetQueue.Add(ReadNextPacket()); packetQueue.Add(ReadNextPacket());
if (cancelToken.IsCancellationRequested)
break;
}
} }
catch (System.IO.IOException) { break; } catch (System.IO.IOException) { break; }
catch (SocketException) { break; } catch (SocketException) { break; }
catch (NullReferenceException) { break; } catch (NullReferenceException) { break; }
catch (Ionic.Zlib.ZlibException) { break; } catch (Ionic.Zlib.ZlibException) { break; }
if (cancelToken.IsCancellationRequested)
break;
Thread.Sleep(10); Thread.Sleep(10);
} }
packetQueue.CompleteAdding(); packetQueue.CompleteAdding();