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