Split apart the server packets from the client packets

This commit is contained in:
derek committed 2026-07-03 16:53:13 -07:00
1 parent ca39e184ed
commit 392a33eb9e
5 files changed
+38 -12

No files matched your search

+6 -1
View File
@@ -42,8 +42,13 @@ namespace Mistox.Client {
// Read off the data // Read off the data
await ns.ReadExactlyAsync(payloadBuffer, 0, dataLength); await ns.ReadExactlyAsync(payloadBuffer, 0, dataLength);
// Get the type from the tag
if (!DataPacket.ServerToClientPackets.TryGetValue(packetbyte, out Type resolvedType)) {
throw new Exception($"Unknown packet type ID: {packetbyte}");
}
// Create the dataobject // Create the dataobject
dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) ); dynamic dataobject = DataPacket.Deserialize( resolvedType, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
// Return the object // Return the object
ClientInterface.ServerClientInstance.InvokeOnReceived(dataobject); ClientInterface.ServerClientInstance.InvokeOnReceived(dataobject);
+6 -1
View File
@@ -41,8 +41,13 @@ namespace Mistox.Client {
ushort DataLength = (ushort)((dataPacket[1] << 8) | dataPacket[2]); ushort DataLength = (ushort)((dataPacket[1] << 8) | dataPacket[2]);
ReadOnlyMemory<byte> data = dataMemory.Slice(DataPacket.HeaderLength, DataLength); ReadOnlyMemory<byte> data = dataMemory.Slice(DataPacket.HeaderLength, DataLength);
// Get the type from the tag
if (!DataPacket.ClientToClientPackets.TryGetValue(dataType, out Type resolvedType)) {
throw new Exception($"Unknown packet type ID: {dataType}");
}
// Extract the object // Extract the object
dynamic buildObj = DataPacket.Deserialize( dataType, DataLength, data ); dynamic buildObj = DataPacket.Deserialize( resolvedType, DataLength, data );
// Return the object // Return the object
ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj ); ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj );
+6 -1
View File
@@ -41,8 +41,13 @@ namespace Mistox.Server {
// Read off the data // Read off the data
await ns.ReadExactlyAsync(payloadBuffer, 0, dataLength); await ns.ReadExactlyAsync(payloadBuffer, 0, dataLength);
// Get the type from the tag
if (!DataPacket.ServerToClientPackets.TryGetValue(packetbyte, out Type resolvedType)) {
throw new Exception($"Unknown packet type ID: {packetbyte}");
}
// Create the dataobject // Create the dataobject
dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) ); dynamic dataobject = DataPacket.Deserialize( resolvedType, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
// Return the object // Return the object
ServerInterface.ServerInstance.InvokeDataReceived(dataobject); ServerInterface.ServerInstance.InvokeDataReceived(dataobject);
+9 -9
View File
@@ -43,23 +43,18 @@ namespace Mistox {
} }
public static object Deserialize( byte PacketType, ushort DataLength, ReadOnlyMemory<byte> Data ) { public static object Deserialize( Type PacketType, ushort DataLength, ReadOnlyMemory<byte> Data ) {
// Sanity check: Ensure the packet we received actually has the payload bytes it claims to have // Sanity check: Ensure the packet we received actually has the payload bytes it claims to have
if ( DataLength <= Data.Length ) { if ( DataLength <= Data.Length ) {
throw new Exception("Malformed packet: Payload size mismatch."); throw new Exception("Malformed packet: Payload size mismatch.");
} }
// Get the type from the tag
if (!NetworkPackets.TryGetValue(PacketType, out Type resolvedType)) {
throw new Exception($"Unknown packet type ID: {PacketType}");
}
// 4. Create the low-level reader struct directly over the memory slice // 4. Create the low-level reader struct directly over the memory slice
var reader = new MessagePackReader(Data); var reader = new MessagePackReader(Data);
// Extract the object // Extract the object
dynamic obj = MessagePackSerializer.Deserialize( resolvedType, ref reader, MessagePackSerializerOptions.Standard ); dynamic obj = MessagePackSerializer.Deserialize( PacketType, ref reader, MessagePackSerializerOptions.Standard );
return obj; return obj;
@@ -69,13 +64,18 @@ namespace Mistox {
// Fills out the NetworkPacket Lookup Dictionary // Fills out the NetworkPacket Lookup Dictionary
public static readonly Dictionary<byte, Type> NetworkPackets = new(); public static readonly Dictionary<byte, Type> ClientToClientPackets = new();
public static readonly Dictionary<byte, Type> ServerToClientPackets = new();
public static void BuildNetworkPacketList() { public static void BuildNetworkPacketList() {
Type[] allClasses = Assembly.GetExecutingAssembly().GetTypes(); Type[] allClasses = Assembly.GetExecutingAssembly().GetTypes();
foreach( Type cur in allClasses) { foreach( Type cur in allClasses) {
mPacket attribute = cur.GetCustomAttribute<mPacket>(false); mPacket attribute = cur.GetCustomAttribute<mPacket>(false);
if (attribute != null) { if (attribute != null) {
NetworkPackets[attribute.ID] = cur; if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToClient) ) {
ClientToClientPackets[attribute.ID] = cur;
} else if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToServer) ) {
ServerToClientPackets[attribute.ID] = cur;
}
} }
} }
} }
+11
View File
@@ -5,9 +5,20 @@ namespace Mistox.Attributes {
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class mPacket : Attribute { public sealed class mPacket : Attribute {
public byte ID { get; } public byte ID { get; }
public PacketUsage PacketUse { get; }
public mPacket(byte id) { public mPacket(byte id) {
ID = id; ID = id;
PacketUse = PacketUsage.ClientToClient | PacketUsage.ClientToServer;
}
public mPacket(byte id, PacketUsage packetUse) {
ID = id;
PacketUse = packetUse;
} }
} }
public enum PacketUsage {
ClientToClient = 0x01,
ClientToServer = 0x10,
}
} }