Skip to content

Commit

Permalink
fix: Adds back socket connected event (#1688)
Browse files Browse the repository at this point in the history
### 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);
    }
}
```
  • Loading branch information
kamronbatman committed Apr 6, 2024
1 parent 90fa2e0 commit ac562e6
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Projects/Server/Network/TcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,15 @@ private static void ProcessConnection(Socket socket)
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);
Expand Down Expand Up @@ -292,4 +300,22 @@ private static void TraceDisconnect(string reason, IPAddress ip)
// 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;
}
}

0 comments on commit ac562e6

Please sign in to comment.