Fix networking bug in Gateserver

This commit is contained in:
xeon 2024-01-30 19:37:13 +03:00
parent 9514e0898d
commit 14ccff36e9

View file

@ -37,23 +37,33 @@ internal class NetworkSession : RPGSession
recvBufferIdx += readAmount; recvBufferIdx += readAmount;
int consumed = ConsumeData(recvBufferMem, recvBufferIdx);
if (consumed > 0)
Buffer.BlockCopy(_recvBuffer, consumed, _recvBuffer, 0, recvBufferIdx -= consumed);
}
}
private int ConsumeData(ReadOnlyMemory<byte> recvBufferMem, int recvBufferIdx)
{
int consumedBytes = 0;
do do
{ {
switch (NetPacket.TryDeserialize(recvBufferMem[..recvBufferIdx], out NetPacket? packet, out int bytesRead)) switch (NetPacket.TryDeserialize(recvBufferMem[consumedBytes..recvBufferIdx], out NetPacket? packet, out int bytesRead))
{ {
case NetPacket.DeserializationResult.Success: case NetPacket.DeserializationResult.Success:
HandleSessionPacketAsync(packet!); HandlePacket(packet!);
Buffer.BlockCopy(_recvBuffer, bytesRead, _recvBuffer, 0, recvBufferIdx -= bytesRead); consumedBytes += bytesRead;
break; break;
case NetPacket.DeserializationResult.BufferExceeded: case NetPacket.DeserializationResult.BufferExceeded:
recvBufferIdx = -1; return consumedBytes;
break;
case NetPacket.DeserializationResult.Corrupted: case NetPacket.DeserializationResult.Corrupted:
throw new InvalidDataException("The network stream was corrupted"); throw new InvalidDataException("The network stream was corrupted");
} }
} }
while (recvBufferIdx >= NetPacket.Overhead); while (recvBufferIdx - consumedBytes >= NetPacket.Overhead);
}
return consumedBytes;
} }
public void ServiceBound(RPGServiceType serviceType) public void ServiceBound(RPGServiceType serviceType)
@ -74,7 +84,7 @@ internal class NetworkSession : RPGSession
await Socket!.SendAsync(buffer); await Socket!.SendAsync(buffer);
} }
private void HandleSessionPacketAsync(NetPacket packet) private void HandlePacket(NetPacket packet)
{ {
switch ((CmdType)packet.CmdType) switch ((CmdType)packet.CmdType)
{ {