fix: Fixes TcpServer edge cases where packets are split on login (#2160)

This commit is contained in:
Kamron Batman 2025-04-15 19:29:17 -07:00 committed by GitHub
parent 4aa272d429
commit e54782441e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 14 deletions

View file

@ -25,7 +25,6 @@ using Server.Mobiles;
using Server.Network;
using Server.Prompts;
using Server.Targeting;
using Server.Text;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

View file

@ -14,7 +14,6 @@
*************************************************************************/
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Net;
@ -157,27 +156,33 @@ public static class TcpServer
}
}
[ThreadStatic]
private static byte[] _firstBytes;
private static async ValueTask ProcessSocketConnection(Socket socket)
{
byte[] firstBytes = ArrayPool<byte>.Shared.Rent(83);
_firstBytes ??= GC.AllocateUninitializedArray<byte>(128);
var cts = CancellationTokenSource.CreateLinkedTokenSource(Core.ClosingTokenSource.Token);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Core.ClosingTokenSource.Token);
cts.CancelAfter(TimeSpan.FromMilliseconds(500));
try
{
var bytesRead = await socket.ReceiveAsync(firstBytes, SocketFlags.Peek, cts.Token);
var bytesRead = await socket.ReceiveAsync(_firstBytes, SocketFlags.Peek, cts.Token);
var isValid =
// Sometimes when newer clients are connecting to the game server the first 4 bytes are sent separately
bytesRead == 4 ||
// Older clients only send the 4 byte seed first then 0x80
(UOClient.MinRequired == null || UOClient.MinRequired < ClientVersion.Version6050) && (
bytesRead == 4 || bytesRead >= 66 && firstBytes[4] == 0x80) ||
(UOClient.MinRequired == null || UOClient.MinRequired < ClientVersion.Version6050) &&
bytesRead >= 66 && _firstBytes[4] == 0x80 ||
// Newer clients
(UOClient.MaxRequired == null || UOClient.MaxRequired >= ClientVersion.Version6050) && (
// Account Login - 0xEF + 0x80 (83 bytes)
bytesRead >= 83 && firstBytes[0] == 0xEF && firstBytes[21] == 0x80 ||
bytesRead >= 83 && _firstBytes[0] == 0xEF && _firstBytes[21] == 0x80 ||
bytesRead == 21 && _firstBytes[0] == 0xEF ||
// Game Login - 4 bytes + 0x91 (69 bytes)
bytesRead >= 69 && firstBytes[4] == 0x91
bytesRead >= 69 && _firstBytes[4] == 0x91
);
// TODO: Validate client version is v4 -> v7 for 0xEF packet
@ -212,11 +217,6 @@ public static class TcpServer
{
ForceCloseSocket(socket);
}
finally
{
ArrayPool<byte>.Shared.Return(firstBytes);
cts.Dispose();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]