fixed the big client packet issue hopefully (yes i only tested once)

This commit is contained in:
rfi 2023-12-11 20:13:37 +07:00
parent 259c744f23
commit 80c5e1cc95
2 changed files with 18 additions and 13 deletions

View File

@ -1,5 +1,6 @@
using AscNet.Common.Database; using AscNet.Common.Database;
using AscNet.Common.MsgPack; using AscNet.Common.MsgPack;
using static AscNet.Common.MsgPack.NotifyCharacterDataList;
using AscNet.Common.Util; using AscNet.Common.Util;
using AscNet.Common; using AscNet.Common;
using AscNet.Table.V2.share.fuben; using AscNet.Table.V2.share.fuben;
@ -8,7 +9,6 @@ using AscNet.Table.V2.share.reward;
using MessagePack; using MessagePack;
using AscNet.GameServer.Handlers.Drops; using AscNet.GameServer.Handlers.Drops;
using AscNet.Table.V2.share.robot; using AscNet.Table.V2.share.robot;
using static AscNet.Common.MsgPack.NotifyCharacterDataList;
using AscNet.Table.V2.share.character.skill; using AscNet.Table.V2.share.character.skill;
namespace AscNet.GameServer.Handlers namespace AscNet.GameServer.Handlers

View File

@ -1,5 +1,6 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Net.Sockets; using System.Net.Sockets;
using System.Reflection.Emit;
using AscNet.Common; using AscNet.Common;
using AscNet.Common.Database; using AscNet.Common.Database;
using AscNet.Common.Util; using AscNet.Common.Util;
@ -45,34 +46,37 @@ namespace AscNet.GameServer
{ {
try try
{ {
if (prevBuf == 0) int len = 0;
Array.Clear(msg, 0, msg.Length);
int len = stream.Read(msg, prevBuf + 0, msg.Length - prevBuf);
len += prevBuf;
if (len > 0) read:
while (stream.DataAvailable)
{
len = stream.Read(msg, prevBuf, msg.Length - prevBuf);
prevBuf += len;
}
if (prevBuf > 0)
{ {
lastPacketTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); lastPacketTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
List<Packet> packets = new(); List<Packet> packets = new();
int readbytes = 0; int readbytes = 0;
while (readbytes < len) while (readbytes < prevBuf)
{ {
int packetLen = BinaryPrimitives.ReadInt32LittleEndian(msg.AsSpan()[readbytes..]); int packetLen = BinaryPrimitives.ReadInt32LittleEndian(msg.AsSpan()[readbytes..]);
if (len > 0) if (prevBuf > 0)
readbytes += 4; readbytes += 4;
if (packetLen < 1) if (packetLen < 1)
{ {
prevBuf = 0;
break; break;
} }
else if (packetLen > len && len > 0) if (packetLen > prevBuf)
{ {
prevBuf += len; goto read;
break;
} }
else else
{ {
prevBuf = 0;
byte[] packet = GC.AllocateUninitializedArray<byte>(packetLen); byte[] packet = GC.AllocateUninitializedArray<byte>(packetLen);
Array.Copy(msg, readbytes, packet, 0, packetLen); Array.Copy(msg, readbytes, packet, 0, packetLen);
readbytes += packetLen; readbytes += packetLen;
@ -85,11 +89,12 @@ namespace AscNet.GameServer
catch (Exception) catch (Exception)
{ {
log.Debug(BitConverter.ToString(msg).Replace("-", "")); log.Debug(BitConverter.ToString(msg).Replace("-", ""));
log.Debug($"PacketLen = {packetLen}, ReadLen = {len}"); log.Debug($"PacketLen = {packetLen}, ReadLen = {prevBuf}");
log.Error("Failed to deserialize packet: " + BitConverter.ToString(packet).Replace("-", "")); log.Error("Failed to deserialize packet: " + BitConverter.ToString(packet).Replace("-", ""));
} }
} }
} }
prevBuf = 0;
foreach (var packet in packets) foreach (var packet in packets)
{ {