Files
DanhengServer-OpenSource/GameServer/Server/Packet/BasePacket.cs
Somebody 87d228eb79 Feature:Asynchronous Operation & Formatting Code
- Now the async operation is enabled!
- Code formatted by Resharper plugin <3
2024-07-22 17:12:03 +08:00

40 lines
921 B
C#

using EggLink.DanhengServer.Util;
using Google.Protobuf;
namespace EggLink.DanhengServer.Server.Packet;
public class BasePacket(ushort cmdId)
{
private const uint HEADER_CONST = 0x9d74c714;
private const uint TAIL_CONST = 0xd7a152c8;
public ushort CmdId { get; set; } = cmdId;
public byte[] Data { get; set; } = [];
public void SetData(byte[] data)
{
Data = data;
}
public void SetData(IMessage message)
{
Data = message.ToByteArray();
}
public byte[] BuildPacket()
{
using MemoryStream? ms = new();
using BinaryWriter? bw = new(ms);
bw.WriteUInt32BE(HEADER_CONST);
bw.WriteUInt16BE(CmdId);
bw.WriteUInt16BE(0);
bw.WriteUInt32BE((uint)Data.Length);
if (Data.Length > 0) bw.Write(Data);
bw.WriteUInt32BE(TAIL_CONST);
var packet = ms.ToArray();
return packet;
}
}