mirror of
https://github.com/thebreaddev/Supercell.GUT.git
synced 2024-11-10 07:44:37 +00:00
ad23f95319
only basic messages, wip.
43 lines
1.3 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|