From 6c82daecb77787926294cbba936e456ada9c9fe3 Mon Sep 17 00:00:00 2001 From: "starlkytminecraft@gmail.com" Date: Sun, 3 Mar 2024 22:47:13 +0300 Subject: [PATCH 1/3] Avoid an allocation on each new connection --- Obsidian/Server.cs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/Obsidian/Server.cs b/Obsidian/Server.cs index d4f67ef6..8c9ac3aa 100644 --- a/Obsidian/Server.cs +++ b/Obsidian/Server.cs @@ -315,6 +315,7 @@ private async Task HandleServerShutdown() private async Task AcceptClientsAsync() { + _tcpListener = await SocketFactory.CreateListenerAsync(new IPEndPoint(IPAddress.Any, Port), token: _cancelTokenSource.Token); while (!_cancelTokenSource.Token.IsCancellationRequested) @@ -375,20 +376,33 @@ 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); + } + + _logger.LogInformation("No longer accepting new clients"); + await _tcpListener.UnbindAsync(); + return; - client.Disconnected += client => + async Task ExecuteAsync(Client client) + { + await Task.Yield(); + + try { - _clients.TryRemove(client); + await client.StartConnectionAsync(); + } + catch (OperationCanceledException) + { + // Ignore. + } - if (client.Player is not null) - _ = OnlinePlayers.TryRemove(client.Player.Uuid, out _); - }; + _clients.TryRemove(client); - _ = Task.Run(client.StartConnectionAsync); - } + if (client.Player is not null) + _ = OnlinePlayers.TryRemove(client.Player.Uuid, out _); - _logger.LogInformation("No longer accepting new clients"); - await _tcpListener.UnbindAsync(); + client.Dispose(); + } } public IBossBar CreateBossBar(ChatMessage title, float health, BossBarColor color, BossBarDivisionType divisionType, BossBarFlags flags) => new BossBar(this) From d3a2867f903a004c244a70f820618b9aa5097809 Mon Sep 17 00:00:00 2001 From: "starlkytminecraft@gmail.com" Date: Sun, 3 Mar 2024 23:01:53 +0300 Subject: [PATCH 2/3] Remove line --- Obsidian/Server.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Obsidian/Server.cs b/Obsidian/Server.cs index 8c9ac3aa..bb9f1aa1 100644 --- a/Obsidian/Server.cs +++ b/Obsidian/Server.cs @@ -315,7 +315,6 @@ private async Task HandleServerShutdown() private async Task AcceptClientsAsync() { - _tcpListener = await SocketFactory.CreateListenerAsync(new IPEndPoint(IPAddress.Any, Port), token: _cancelTokenSource.Token); while (!_cancelTokenSource.Token.IsCancellationRequested) From 9ff760f3179a2fe02d0ef68a9244ca8e596dce32 Mon Sep 17 00:00:00 2001 From: "starlkytminecraft@gmail.com" Date: Sun, 3 Mar 2024 23:04:14 +0300 Subject: [PATCH 3/3] Catch exceptions --- Obsidian/Server.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Obsidian/Server.cs b/Obsidian/Server.cs index bb9f1aa1..40930878 100644 --- a/Obsidian/Server.cs +++ b/Obsidian/Server.cs @@ -394,13 +394,19 @@ async Task ExecuteAsync(Client client) { // Ignore. } + catch (Exception exception) + { + _logger.LogError("Unexpected exception from client {Identifier}: {Message}", client.id, exception.Message); + } + finally + { + _clients.TryRemove(client); - _clients.TryRemove(client); - - if (client.Player is not null) - _ = OnlinePlayers.TryRemove(client.Player.Uuid, out _); + if (client.Player is not null) + _ = OnlinePlayers.TryRemove(client.Player.Uuid, out _); - client.Dispose(); + client.Dispose(); + } } }