Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allocate on each new connection #435

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading