Supercell.GUT/Supercell.GUT.Server/Network/Connection/ClientConnectionManager.cs
BreadDEV ad23f95319 [v0.0.1] very early state server
only basic messages, wip.
2024-03-04 20:19:32 +07:00

43 lines
1.3 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Supercell.GUT.Server.Network.Connection;
internal class ClientConnectionManager : IGatewayEventListener
{
private readonly ILogger _logger;
private readonly IServiceScopeFactory _scopeFactory;
public ClientConnectionManager(ILogger<ClientConnectionManager> logger, IServiceScopeFactory scopeFactory)
{
_logger = logger;
_scopeFactory = scopeFactory;
}
public void OnConnect(IProtocolEntity entity)
{
_logger.LogInformation("New connection from {endPoint}", entity.RemoteEndPoint);
_ = RunSessionAsync(entity);
}
private async Task RunSessionAsync(IProtocolEntity entity)
{
using IServiceScope scope = _scopeFactory.CreateScope();
ClientConnection session = scope.ServiceProvider.GetRequiredService<ClientConnection>();
try
{
session.SetProtocolEntity(entity);
await session.RunAsync();
}
catch (OperationCanceledException) { /* Operation was canceled. */ }
catch (Exception exception)
{
_logger.LogError("Unhandled exception occurred while processing session, trace:\n{exception}", exception);
}
finally
{
entity.Dispose();
}
}
}