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);
    }
}
```
This commit is contained in:
Kamron Batman 2024-04-06 10:26:46 -07:00 committed by GitHub
parent 90fa2e09de
commit ac562e67dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<SocketConnectedEventArgs> 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;
}
}