WutheringWaves/GameServer/Network/PlayerSession.cs

52 lines
1.5 KiB
C#
Raw Normal View History

2024-02-09 22:15:05 +00:00
using GameServer.Network.Messages;
2024-02-09 09:44:42 +00:00
using GameServer.Network.Rpc;
using Google.Protobuf;
using Microsoft.Extensions.Logging;
using Protocol;
namespace GameServer.Network;
internal class PlayerSession
{
private readonly ILogger _logger;
private readonly MessageManager _messageManager;
public RpcManager Rpc { get; }
public ISessionActionListener? Listener { private get; set; }
public PlayerSession(ILogger<PlayerSession> logger, MessageManager messageManager, RpcManager rpcManager)
{
_logger = logger;
_messageManager = messageManager;
Rpc = rpcManager;
}
public async Task HandleMessageAsync(BaseMessage message)
{
switch (message)
{
case RequestMessage request:
2024-02-09 22:15:05 +00:00
await Rpc.Execute(request);
2024-02-09 09:44:42 +00:00
break;
case PushMessage push:
2024-02-09 22:15:05 +00:00
if (!await _messageManager.HandlePush(push.MessageId, push.Payload))
2024-02-09 09:44:42 +00:00
_logger.LogWarning("Push message ({id}) was not handled", push.MessageId);
break;
}
}
2024-02-09 22:15:05 +00:00
public Task Push<TProtoBuf>(MessageId id, TProtoBuf data) where TProtoBuf : IMessage<TProtoBuf>
2024-02-09 09:44:42 +00:00
{
return Listener?.OnServerMessageAvailable(new PushMessage
{
MessageId = id,
Payload = data.ToByteArray()
}) ?? Task.CompletedTask;
}
public Task SendRpcRsp(ResponseMessage message)
{
return Listener?.OnServerMessageAvailable(message) ?? Task.CompletedTask;
}
}