Skip to content

Commit

Permalink
ignore & crash instead of disconnect clients after reaching the MaxPl…
Browse files Browse the repository at this point in the history
…ayers limit

Otherwise they'll enter an endless disconnect-reconnnect loop spamming the server with new TCP connections.
  • Loading branch information
Istador authored and Sanae6 committed Apr 27, 2024
1 parent 082e480 commit 4de654b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ async Task<bool> Read(Memory<byte> readMem, int readSize, int readOffset) {
client.Ignored = true;
client.Banned = true;
}
// is the server full?
else if (Clients.Count(x => x.Connected) >= Settings.Instance.Server.MaxPlayers) {
client.Logger.Error($"Ignoring player {client.Name} ({client.Id}/{remote}) as server reached max players of {Settings.Instance.Server.MaxPlayers}");
client.Ignored = true;
}

// send server init (required to crash ignored players later)
await client.Send(new InitPacket {
Expand All @@ -213,11 +218,12 @@ await client.Send(new InitPacket {

// add client to the set of connected players
lock (Clients) {
// is the server full?
if (Clients.Count(x => x.Connected) == Settings.Instance.Server.MaxPlayers) {
client.Logger.Error($"Turned away as server is at max clients");
// is the server full? (check again, to prevent race conditions)
if (Clients.Count(x => x.Connected) >= Settings.Instance.Server.MaxPlayers) {
client.Logger.Error($"Ignoring player {client.Name} ({client.Id}/{remote}) as server reached max players of {Settings.Instance.Server.MaxPlayers}");
client.Ignored = true;
memory.Dispose();
goto disconnect;
continue;
}

// detect and handle reconnections
Expand Down Expand Up @@ -354,7 +360,6 @@ await Parallel.ForEachAsync(otherConnectedPlayers, async (other, _) => {
}

// client disconnected
disconnect:
if (client.Name != "Unknown User" && client.Id != Guid.Parse("00000000-0000-0000-0000-000000000000")) {
Logger.Info($"Client {remote} ({client.Name}/{client.Id}) disconnected from the server");
}
Expand Down

0 comments on commit 4de654b

Please sign in to comment.