Fix not handling partial sends, which would very rarely occur and then cause the game to disconnect with invalid packet

This commit is contained in:
UnknownShadow200 2021-02-11 18:20:46 +11:00
parent 0f590755e8
commit 93c2c9a30a

View File

@ -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) {
s.sendInProgress = false;
// 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