Skip to content

Commit

Permalink
Avoid messages from multiple threads rarely interleaving with each other
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed May 27, 2020
1 parent d034a04 commit bcfbd16
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions MCGalaxy/Network/Player.Networking.cs
Expand Up @@ -163,13 +163,16 @@ public partial class Player : IDisposable, INetProtocol {
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<string> 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);
}
Expand Down Expand Up @@ -245,9 +248,9 @@ public partial class Player : IDisposable, INetProtocol {
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);
}
}
Expand Down

0 comments on commit bcfbd16

Please sign in to comment.