WutheringWaves/GameServer/Network/SessionManager.cs

42 lines
1.2 KiB
C#
Raw Permalink Normal View History

2024-02-09 09:44:42 +00:00
using GameServer.Network.Kcp;
2024-02-09 22:15:05 +00:00
using GameServer.Network.Messages;
2024-02-09 09:44:42 +00:00
using KcpSharp;
2024-02-07 21:41:39 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace GameServer.Network;
internal class SessionManager
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ILogger _logger;
public SessionManager(IServiceScopeFactory scopeFactory, ILogger<SessionManager> logger)
{
_scopeFactory = scopeFactory;
_logger = logger;
}
public async Task RunSessionAsync(KcpConversation kcpConv)
{
using IServiceScope scope = _scopeFactory.CreateScope();
2024-02-09 09:44:42 +00:00
PlayerSession session = scope.ServiceProvider.GetRequiredService<PlayerSession>();
KcpConnection connection = new(kcpConv);
session.Listener = connection;
2024-02-07 21:41:39 +00:00
try
{
2024-02-09 09:44:42 +00:00
while (connection.Active)
{
BaseMessage? message = await connection.ReceiveMessageAsync();
if (message == null) break;
await session.HandleMessageAsync(message);
}
2024-02-07 21:41:39 +00:00
}
catch (Exception exception)
{
_logger.LogError("Exception occurred in session processing: {trace}", exception);
}
}
}