0
0
Fork 0
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:
Sanae 2022-04-27 00:24:11 -06:00
parent 1bcb15eb54
commit bddce48f9d
4 changed files with 15 additions and 11 deletions

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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"},

View file

@ -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..]);
}