From 93c2c9a30a87c8057c84f28ceba5cd5e9bc189ba Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 11 Feb 2021 18:20:46 +1100 Subject: [PATCH] Fix not handling partial sends, which would very rarely occur and then cause the game to disconnect with invalid packet --- MCGalaxy/Network/Sockets.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/MCGalaxy/Network/Sockets.cs b/MCGalaxy/Network/Sockets.cs index eb1642d82..fe776623b 100644 --- a/MCGalaxy/Network/Sockets.cs +++ b/MCGalaxy/Network/Sockets.cs @@ -219,11 +219,20 @@ namespace MCGalaxy.Network { 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