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

47 lines
1.5 KiB
C#

using Google.Protobuf;
using RPG.Network.Proto;
using RPG.Services.Core.Network;
using RPG.Services.Core.Network.Command;
namespace RPG.Services.Core.Session;
public abstract class RPGSession
{
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
});
}
}