mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-24 12:15:17 +00:00
Stop using MemoryMarshal for header serde
This commit is contained in:
parent
1bcb15eb54
commit
bddce48f9d
4 changed files with 15 additions and 11 deletions
|
@ -52,7 +52,8 @@ public class Client : IDisposable {
|
|||
}
|
||||
|
||||
public async Task Send(Memory<byte> data, Client? sender) {
|
||||
PacketHeader header = MemoryMarshal.Read<PacketHeader>(data.Span);
|
||||
PacketHeader header = new PacketHeader();
|
||||
header.Deserialize(data.Span);
|
||||
if (!Connected && header.Type is not PacketType.Connect) {
|
||||
Server.Logger.Error($"Didn't send {header.Type} to {Id} because they weren't connected yet");
|
||||
return;
|
||||
|
|
|
@ -60,7 +60,7 @@ public class Server {
|
|||
public static void FillPacket<T>(PacketHeader header, T packet, Memory<byte> memory) where T : struct, IPacket {
|
||||
Span<byte> data = memory.Span;
|
||||
|
||||
MemoryMarshal.Write(data, ref header);
|
||||
header.Serialize(data[..Constants.HeaderSize]);
|
||||
packet.Serialize(data[Constants.HeaderSize..]);
|
||||
}
|
||||
|
||||
|
@ -234,7 +234,7 @@ public class Server {
|
|||
Type = PacketType.Connect,
|
||||
PacketSize = connect.Size
|
||||
};
|
||||
MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader);
|
||||
connectHeader.Serialize(tempBuffer.Memory.Span[..Constants.HeaderSize]);
|
||||
ConnectPacket connectPacket = new ConnectPacket {
|
||||
ConnectionType = ConnectPacket.ConnectionTypes.FirstConnection, // doesn't matter what it is
|
||||
MaxPlayers = Settings.Instance.Server.MaxPlayers,
|
||||
|
@ -245,7 +245,7 @@ public class Server {
|
|||
if (other.CurrentCostume.HasValue) {
|
||||
connectHeader.Type = PacketType.Costume;
|
||||
connectHeader.PacketSize = other.CurrentCostume.Value.Size;
|
||||
MemoryMarshal.Write(tempBuffer.Memory.Span, ref connectHeader);
|
||||
connectHeader.Serialize(tempBuffer.Memory.Span[..Constants.HeaderSize]);
|
||||
other.CurrentCostume.Value.Serialize(tempBuffer.Memory.Span[Constants.HeaderSize..(Constants.HeaderSize + connectHeader.PacketSize)]);
|
||||
await client.Send(tempBuffer.Memory[..(Constants.HeaderSize + connectHeader.PacketSize)], null);
|
||||
}
|
||||
|
@ -309,6 +309,8 @@ public class Server {
|
|||
|
||||
private static PacketHeader GetHeader(Span<byte> data) {
|
||||
//no need to error check, the client will disconnect when the packet is invalid :)
|
||||
return MemoryMarshal.Read<PacketHeader>(data);
|
||||
PacketHeader header = new PacketHeader();
|
||||
header.Deserialize(data[..Constants.HeaderSize]);
|
||||
return header;
|
||||
}
|
||||
}
|
|
@ -12,7 +12,7 @@ public static class Constants {
|
|||
public static readonly Dictionary<Type, PacketAttribute> PacketMap = Assembly
|
||||
.GetExecutingAssembly()
|
||||
.GetTypes()
|
||||
.Where(type => type.IsAssignableTo(typeof(IPacket)))
|
||||
.Where(type => type.IsAssignableTo(typeof(IPacket)) && type.GetCustomAttribute<PacketAttribute>() != null)
|
||||
.ToDictionary(type => type, type => type.GetCustomAttribute<PacketAttribute>()!);
|
||||
public static readonly Dictionary<PacketType, Type> PacketIdMap = Assembly
|
||||
.GetExecutingAssembly()
|
||||
|
@ -20,7 +20,7 @@ public static class Constants {
|
|||
.Where(type => type.IsAssignableTo(typeof(IPacket)) && type.GetCustomAttribute<PacketAttribute>() != null)
|
||||
.ToDictionary(type => type.GetCustomAttribute<PacketAttribute>()!.Type, type => type);
|
||||
|
||||
public static int HeaderSize { get; } = Marshal.SizeOf<PacketHeader>();
|
||||
public static int HeaderSize { get; } = PacketHeader.StaticSize;
|
||||
|
||||
public static readonly Dictionary<string, string> MapNames = new Dictionary<string, string>() {
|
||||
{"cap", "CapWorldHomeStage"},
|
||||
|
|
|
@ -10,16 +10,17 @@ public struct PacketHeader : IPacket {
|
|||
public PacketType Type;
|
||||
public short PacketSize;
|
||||
|
||||
public short Size => 20;
|
||||
public static short StaticSize => 20;
|
||||
public short Size => StaticSize;
|
||||
|
||||
public void Serialize(Span<byte> data) {
|
||||
MemoryMarshal.Write(data, ref Id);
|
||||
MemoryMarshal.Write(data[..16], ref Id);
|
||||
MemoryMarshal.Write(data[16..], ref Type);
|
||||
MemoryMarshal.Write(data[18..], ref PacketSize);
|
||||
}
|
||||
|
||||
public void Deserialize(ReadOnlySpan<byte> data) {
|
||||
Id = MemoryMarshal.Read<Guid>(data);
|
||||
Id = MemoryMarshal.Read<Guid>(data[..16]);
|
||||
Type = MemoryMarshal.Read<PacketType>(data[16..]);
|
||||
PacketSize = MemoryMarshal.Read<short>(data[18..]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue