Fix networking bug in Gateserver
This commit is contained in:
parent
9514e0898d
commit
14ccff36e9
1 changed files with 27 additions and 17 deletions
|
@ -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)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue