Skip to content

Commit

Permalink
Fix Topic calls dropping partial packets (OpenDreamProject#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberboss authored Dec 14, 2023
1 parent 22f7796 commit 936eac3
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions OpenDreamRuntime/DreamManager.Connections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,18 @@ private async Task ConsumeAndHandleWorldTopicSocket(Socket remote, CancellationT
var length = BitConverter.ToUInt16(buffer);

buffer = new byte[length];
var read = await from.ReceiveAsync(buffer, cancellationToken);
if (read != buffer.Length) {
_sawmill.Warning("failed to parse byond topic due to insufficient data read");
return null;
}
var totalRead = 0;
do {
var read = await from.ReceiveAsync(
new Memory<byte>(buffer, totalRead, length - totalRead),
cancellationToken);
if(read == 0 && totalRead != length) {
_sawmill.Warning("failed to parse byond topic due to insufficient data read");
return null;
}

totalRead += read;
} while (totalRead < length);

return Encoding.ASCII.GetString(buffer[6..^1]);
}
Expand Down Expand Up @@ -178,7 +185,7 @@ private async Task ConsumeAndHandleWorldTopicSocket(Socket remote, CancellationT
await remote.DisconnectAsync(false, cancellationToken);
}
} catch (Exception ex) {
_sawmill.Warning("Error processing topic: {0}", ex);
_sawmill.Warning("Error processing topic #{0}: {1}", topicId, ex);
} finally {
_sawmill.Debug("Finished world topic #{0}", topicId);
}
Expand Down

0 comments on commit 936eac3

Please sign in to comment.