Skip to content

Commit

Permalink
Do not allocate on each new connection (#435)
Browse files Browse the repository at this point in the history
* Avoid an allocation on each new connection

* Remove line

* Catch exceptions
  • Loading branch information
TheVeryStarlk committed Mar 6, 2024
1 parent 31544a3 commit 75ff77d
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions Obsidian/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,39 @@ private async Task AcceptClientsAsync()
var client = new Client(connection, Math.Max(0, _clients.Count + this.DefaultWorld.GetTotalLoadedEntities()), this.loggerFactory, this.userCache, this);

_clients.Add(client);
_ = ExecuteAsync(client);
}

client.Disconnected += client =>
_logger.LogInformation("No longer accepting new clients");
await _tcpListener.UnbindAsync();
return;

async Task ExecuteAsync(Client client)
{
await Task.Yield();

try
{
await client.StartConnectionAsync();
}
catch (OperationCanceledException)
{
// Ignore.
}
catch (Exception exception)
{
_logger.LogError("Unexpected exception from client {Identifier}: {Message}", client.id, exception.Message);
}
finally
{
_clients.TryRemove(client);

if (client.Player is not null)
_ = OnlinePlayers.TryRemove(client.Player.Uuid, out _);
};

_ = Task.Run(client.StartConnectionAsync);
client.Dispose();
}
}

_logger.LogInformation("No longer accepting new clients");
await _tcpListener.UnbindAsync();
}

public IBossBar CreateBossBar(ChatMessage title, float health, BossBarColor color, BossBarDivisionType divisionType, BossBarFlags flags) => new BossBar(this)
Expand Down

0 comments on commit 75ff77d

Please sign in to comment.