Snowflake/RPG.Services.Core/Session/RPGSession.cs

50 lines
1.5 KiB
C#
Raw Normal View History

2024-01-18 22:13:40 +00:00
using Google.Protobuf;
using RPG.Network.Proto;
using RPG.Services.Core.Network;
using RPG.Services.Core.Network.Command;
namespace RPG.Services.Core.Session;
2024-01-19 14:45:18 +00:00
public abstract class RPGSession : IDisposable
2024-01-18 22:13:40 +00:00
{
private readonly ServiceBox _serviceBox;
public ulong SessionId { get; }
public uint PlayerUid { get; set; }
public RPGSession(ulong sessionId, ServiceBox serviceBox)
{
SessionId = sessionId;
_serviceBox = serviceBox;
}
public void SendToService<TBody>(RPGServiceType target, ServiceCommandType commandType, TBody body) where TBody : IMessage<TBody>
{
ServiceCommand command = new(_serviceBox.CurrentType, commandType, body.ToByteArray());
byte[] commandBuffer = GC.AllocateUninitializedArray<byte>(7 + command.Body.Length);
ServiceCommandEncoder.EncodeCommand(command, commandBuffer);
_serviceBox.SendToService(target, commandBuffer);
}
public void BindService(RPGServiceType service)
{
SendToService(service, ServiceCommandType.BindContainer, new CmdBindContainer
{
SessionId = SessionId,
PlayerUid = PlayerUid
});
}
public void UnbindService(RPGServiceType service, CmdUnbindContainer.Types.UnbindContainerReason reason = CmdUnbindContainer.Types.UnbindContainerReason.Logout)
{
SendToService(service, ServiceCommandType.UnbindContainer, new CmdUnbindContainer
{
SessionId = SessionId,
Reason = reason
});
}
2024-01-19 14:45:18 +00:00
public abstract void Dispose();
2024-01-18 22:13:40 +00:00
}