From ac562e67dc05cda019d61f87057d131b072011c0 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sat, 6 Apr 2024 10:26:46 -0700 Subject: [PATCH] fix: Adds back socket connected event (#1688) ### Summary Adds back the `SocketConnected` event. This event only fires asynchronously! (TcpServer thread). Example: ```cs public static void Configure() { TcpServer.EventSink.SocketConnected += OnSocketConnected; } // WARNING: Executed on the TcpServer thread! private static void OnSocketConnected(TcpServer.SocketConnectedEventArgs args) { if (... logic here...) { AdminFirewall.Add(((IPEndPoint)Socket.RemoteEndPoint)!.Address); } } ``` --- Projects/Server/Network/TcpServer.cs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Projects/Server/Network/TcpServer.cs b/Projects/Server/Network/TcpServer.cs index 892c4f589..27c30191f 100644 --- a/Projects/Server/Network/TcpServer.cs +++ b/Projects/Server/Network/TcpServer.cs @@ -256,7 +256,15 @@ public static class TcpServer return; } - if (Firewall.IsBlocked(remoteIP)) + var firewalled = Firewall.IsBlocked(remoteIP); + if (!firewalled) + { + var socketConnectedArgs = new SocketConnectedEventArgs(socket); + EventSink.InvokeSocketConnected(socketConnectedArgs); + firewalled = !socketConnectedArgs.ConnectionAllowed; + } + + if (firewalled) { TraceDisconnect("Firewalled", remoteIP); logger.Debug("{Address} Firewalled", remoteIP); @@ -292,4 +300,22 @@ public static class TcpServer // ignored } } + + public static class EventSink + { + // IMPORTANT: This is executed asynchronously! Do not run any game thread code on these delegates! + public static event Action SocketConnected; + + internal static void InvokeSocketConnected(SocketConnectedEventArgs context) => + SocketConnected?.Invoke(context); + } + + public class SocketConnectedEventArgs + { + public Socket Socket { get; } + + public bool ConnectionAllowed { get; set; } = true; + + internal SocketConnectedEventArgs(Socket socket) => Socket = socket; + } }