Shrink packet header size

This commit is contained in:
derek.holloway committed 2026-07-01 20:30:05 -07:00
1 parent c3906e7d60
commit 7b58ca0338
10 files changed
+140 -115

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using MistoxServer.Attributes;
using MsgPack.Serialization;
namespace MistoxServer {
public class DataPacket {
// The length of the packet header in bytes
public static readonly int HeaderLength = 3;
// Data Packet Properties
public byte PacketType { get; set; } = 0x00;
public ushort DataLength { get; set; } = 0;
public byte[] PacketData { get; set; } = [];
// 8b. 16b. ?
// Data Packet [ Type | DataLength | Data ]
// Helper classes
public static byte[] Serialize<T>( T Object ) {
mPacket attribute = typeof(T).GetCustomAttribute<mPacket>(false);
DataPacket packet = new DataPacket();
if (attribute != null) {
using ( MemoryStream stream = new MemoryStream() ) {
MessagePackSerializer serializer = MessagePackSerializer.Get<T>();
serializer.Pack( stream, Object );
packet.PacketData = stream.ToArray();
packet.DataLength = (ushort)packet.PacketData.Length;
return new byte[]{ packet.PacketType }.Join( BitConverter.GetBytes( packet.DataLength )).Join(packet.PacketData);
}
}
throw new Exception( "The datastructure " + typeof(T).Name + " is not an mPacket" );
}
public object Deserialize() {
Type type = Type.GetType( NetworkPackets[PacketType] );
MessagePackSerializer Serilizer = MessagePackSerializer.Get(type);
using( MemoryStream ms = new MemoryStream( PacketData ) ) {
return Serilizer.Unpack( ms );
}
}
// Fills out the NetworkPacket Lookup Dictionary
public static readonly Dictionary<byte, string> NetworkPackets = new();
public static void BuildNetworkPacketList() {
Type[] allClasses = Assembly.GetExecutingAssembly().GetTypes();
foreach( Type cur in allClasses) {
mPacket attribute = cur.GetCustomAttribute<mPacket>(false);
if (attribute != null) {
NetworkPackets[attribute.ID] = cur.FullName + "," + cur.Assembly.FullName;
}
}
}
}
}
+13
View File
@@ -0,0 +1,13 @@
using System;
namespace MistoxServer.Attributes {
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class mPacket : Attribute {
public byte ID { get; }
public mPacket(byte id) {
ID = id;
}
}
}