From 94dff1fe0a40f03e68cec1ca6a856d170eb1b240 Mon Sep 17 00:00:00 2001 From: Tuomo Date: Mon, 4 Mar 2024 14:01:11 +0200 Subject: [PATCH] A2S response failure debugging --- A2SServer/A2SServer.cs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/A2SServer/A2SServer.cs b/A2SServer/A2SServer.cs index f47319c..950f949 100644 --- a/A2SServer/A2SServer.cs +++ b/A2SServer/A2SServer.cs @@ -32,8 +32,9 @@ public class Info public class A2SServer : UdpServer { - private readonly byte[] _prefix = { 0xff, 0xff, 0xff, 0xff }; + private readonly byte[] _prefix = [0xff, 0xff, 0xff, 0xff]; private const string QueryStr = "Source Engine Query\0"; + private readonly object netLock = new object(); public Info Info { get; set; } = new(); public Dictionary Rules { get; set; } = new() { ["\0"] = "\0" }; @@ -53,19 +54,24 @@ protected override void OnStarted() protected override void OnReceived(EndPoint endpoint, byte[] buffer, long offset, long size) { + // TODO: shit workaround, migrate to SuperSocket. + Monitor.Enter(netLock); + Console.WriteLine($"OnReceived: {endpoint}: {size}"); // A2S requests should never exceed 29 bytes. if (size is < 5 or > 30) { - // Console.WriteLine("bad size"); + Console.WriteLine($"bad size on request from {endpoint}"); + OnSent(endpoint, 0); return; } var prefix = buffer.Take(4); if (!prefix.SequenceEqual(_prefix)) { - // Console.WriteLine("bad prefix"); + Console.WriteLine($"bad prefix on request from {endpoint}"); + OnSent(endpoint, 0); return; } @@ -95,7 +101,10 @@ protected override void OnReceived(EndPoint endpoint, byte[] buffer, long offset if (ok) { Console.WriteLine($"responding to 0x{header:X} request from {endpoint}: {sendSize}"); - SendAsync(endpoint, sendBuffer, 0, sendSize); + if (!SendAsync(endpoint, sendBuffer, 0, sendSize)) + { + Console.WriteLine($"failed to respond to 0x{header:x} request from {endpoint}"); + } } else { @@ -232,7 +241,7 @@ private bool HandleRulesRequest(ref byte[] buffer, long bufferSize, out byte[] s var rulesLen = (short)Rules.Count; - List rulesData = new(); + List rulesData = []; foreach (var kv in Rules) { rulesData.AddRange(Encoding.ASCII.GetBytes(kv.Key + '\0')); @@ -254,16 +263,18 @@ private bool HandleRulesRequest(ref byte[] buffer, long bufferSize, out byte[] s protected override void OnSent(EndPoint endpoint, long sent) { + Monitor.Exit(netLock); ReceiveAsync(); } protected override void OnError(SocketError error) { + Monitor.Exit(netLock); Console.WriteLine($"A2SServer OnError: {error}"); ReceiveAsync(); } - private IEnumerable MakeChallengePacket() + private byte[] MakeChallengePacket() { var data = new byte[] {