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:
parent
90fa2e09de
commit
ac562e67dc
1 changed files with 27 additions and 1 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue