Skip to content

Commit

Permalink
Fix not handling partial sends, which would very rarely occur and the…
Browse files Browse the repository at this point in the history
…n cause the game to disconnect with invalid packet
  • Loading branch information
UnknownShadow200 committed Feb 11, 2021
1 parent 0f59075 commit 93c2c9a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions MCGalaxy/Network/Sockets.cs
Expand Up @@ -219,11 +219,20 @@ public sealed class TcpSocket : INetSocket {
static void SendCallback(object sender, SocketAsyncEventArgs e) {
TcpSocket s = (TcpSocket)e.UserToken;
try {
// TODO: Need to check if all data was sent or not?
int sent = e.BytesTransferred;
lock (s.sendLock) {
lock (s.sendLock) {
// check if last packet was only partially sent? try to resend it
for (;;) {
int sent = e.BytesTransferred;
int count = e.Count;
if (sent >= count || sent < 0) break;

// last packet was only partially sent - resend rest of packet
s.sendArgs.SetBuffer(e.Offset + sent, e.Count - sent);
s.sendInProgress = s.socket.SendAsync(s.sendArgs);
if (s.sendInProgress) return;
}

s.sendInProgress = false;

while (s.sendQueue.Count > 0) {
// DoSendAsync returns false if SendAsync completed sync
// If that happens, SendCallback isn't called so we need to send data here instead
Expand Down

0 comments on commit 93c2c9a

Please sign in to comment.