SmoOnlineServer/Shared/Packet/Packets/ConnectPacket.cs

32 lines
1.0 KiB
C#
Raw Normal View History

2022-02-04 09:45:38 +00:00
using System.Runtime.InteropServices;
using System.Text;
2022-02-04 09:45:38 +00:00
2022-02-10 01:44:50 +00:00
namespace Shared.Packet.Packets;
2022-02-04 09:45:38 +00:00
[Packet(PacketType.Connect)]
public struct ConnectPacket : IPacket {
public ConnectionTypes ConnectionType = ConnectionTypes.FirstConnection;
2022-04-27 05:24:40 +00:00
public ushort MaxPlayers = 0;
2022-02-14 22:09:55 +00:00
public string ClientName = "?????";
2022-02-10 01:44:50 +00:00
public ConnectPacket() { }
2022-04-27 05:24:40 +00:00
public short Size => 6 + Constants.CostumeNameSize;
2022-02-04 09:45:38 +00:00
public void Serialize(Span<byte> data) {
MemoryMarshal.Write(data, ref ConnectionType);
2022-04-27 05:24:40 +00:00
MemoryMarshal.Write(data[4..], ref MaxPlayers);
Encoding.UTF8.GetBytes(ClientName).CopyTo(data[6..(6 + Constants.CostumeNameSize)]);
2022-02-04 09:45:38 +00:00
}
public void Deserialize(ReadOnlySpan<byte> data) {
2022-02-04 09:45:38 +00:00
ConnectionType = MemoryMarshal.Read<ConnectionTypes>(data);
2022-04-27 05:26:35 +00:00
MaxPlayers = MemoryMarshal.Read<ushort>(data[4..]);
2022-04-27 05:24:40 +00:00
ClientName = Encoding.UTF8.GetString(data[6..(6 + Constants.CostumeNameSize)]).TrimNullTerm();
2022-02-04 09:45:38 +00:00
}
2022-03-13 11:14:01 +00:00
public enum ConnectionTypes {
FirstConnection,
Reconnecting
}
2021-11-29 04:04:34 +00:00
}