From bcfbd1624d1b3c6991abd22affcb8ab5a722162b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 27 May 2020 18:26:43 +1000 Subject: [PATCH] Avoid messages from multiple threads rarely interleaving with each other --- MCGalaxy/Network/Player.Networking.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/MCGalaxy/Network/Player.Networking.cs b/MCGalaxy/Network/Player.Networking.cs index ebfa4da83..5460854ee 100644 --- a/MCGalaxy/Network/Player.Networking.cs +++ b/MCGalaxy/Network/Player.Networking.cs @@ -163,13 +163,16 @@ namespace MCGalaxy { if (cancelmessage) { cancelmessage = false; return; } try { - foreach (string raw in LineWrapper.Wordwrap(message)) { - string line = raw; - if (!Supports(CpeExt.EmoteFix) && LineEndsInEmote(line)) - line += '\''; - - Send(Packet.Message(line, (CpeMessageType)id, hasCP437)); + List lines = LineWrapper.Wordwrap(message); + byte[] packet = new byte[lines.Count * 66]; + + for (int i = 0; i < lines.Count; i++) { + string line = lines[i]; + if (!Supports(CpeExt.EmoteFix) && LineEndsInEmote(line)) line += '\''; + Packet.WriteMessage(line, id, hasCP437, packet, i * 66); } + // So multi-line messages from multiple threads don't interleave + Send(packet); } catch (Exception e) { Logger.LogError(e); } @@ -245,9 +248,9 @@ namespace MCGalaxy { using (LevelChunkStream dst = new LevelChunkStream(this)) using (Stream stream = LevelChunkStream.CompressMapHeader(this, volume, dst)) { - if (level.MightHaveCustomBlocks()) { + if (level.MightHaveCustomBlocks()) { LevelChunkStream.CompressMap(this, stream, dst); - } else { + } else { LevelChunkStream.CompressMapSimple(this, stream, dst); } }