using System.Buffers.Binary; using RPG.Network.Proto; namespace RPG.Services.Core.Network.Command; public static class ServiceCommandEncoder { public static ServiceCommand DecodeCommand(ReadOnlyMemory buffer) { ReadOnlySpan span = buffer.Span; RPGServiceType senderType = (RPGServiceType)span[0]; ServiceCommandType commandType = (ServiceCommandType)BinaryPrimitives.ReadUInt16BigEndian(span[1..3]); ReadOnlyMemory body = buffer.Slice(7, BinaryPrimitives.ReadInt32BigEndian(span[3..7])); return new(senderType, commandType, body); } public static void EncodeCommand(ServiceCommand command, Memory buffer) { Span span = buffer.Span; span[0] = (byte)command.SenderType; BinaryPrimitives.WriteUInt16BigEndian(span[1..3], (ushort)command.CommandType); BinaryPrimitives.WriteInt32BigEndian(span[3..7], command.Body.Length); command.Body.CopyTo(buffer[7..]); } }