2024-02-07 21:41:39 +00:00
|
|
|
|
using System.Buffers.Binary;
|
|
|
|
|
|
2024-02-09 22:15:05 +00:00
|
|
|
|
namespace GameServer.Network.Messages;
|
2024-02-07 21:41:39 +00:00
|
|
|
|
internal abstract class BaseMessage
|
|
|
|
|
{
|
|
|
|
|
public const int LengthFieldSize = 3;
|
|
|
|
|
|
|
|
|
|
public abstract MessageType Type { get; }
|
|
|
|
|
public abstract int HeaderSize { get; }
|
|
|
|
|
|
|
|
|
|
public uint SeqNo { get; set; }
|
|
|
|
|
public ReadOnlyMemory<byte> Payload { get; set; }
|
|
|
|
|
|
|
|
|
|
public virtual void Encode(Memory<byte> buffer)
|
|
|
|
|
{
|
|
|
|
|
BinaryPrimitives.WriteUInt32LittleEndian(buffer.Span[1..], SeqNo);
|
|
|
|
|
Payload.CopyTo(buffer[HeaderSize..]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Decode(ReadOnlyMemory<byte> buffer)
|
|
|
|
|
{
|
|
|
|
|
SeqNo = BinaryPrimitives.ReadUInt32LittleEndian(buffer.Span[1..]);
|
|
|
|
|
Payload = buffer[HeaderSize..];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int NetworkSize => HeaderSize + Payload.Length + LengthFieldSize;
|
|
|
|
|
}
|