diff --git a/MistoxServer/Client/TCPClient.cs b/MistoxServer/Client/TCPClient.cs index 69a50fb..264732e 100644 --- a/MistoxServer/Client/TCPClient.cs +++ b/MistoxServer/Client/TCPClient.cs @@ -42,13 +42,8 @@ namespace Mistox.Client { // Read off the data 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 - dynamic dataobject = DataPacket.Deserialize( resolvedType, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) ); + dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) ); // Return the object ClientInterface.ServerClientInstance.InvokeOnReceived(dataobject); diff --git a/MistoxServer/Client/UDPClient.cs b/MistoxServer/Client/UDPClient.cs index a8d2756..ab3179b 100644 --- a/MistoxServer/Client/UDPClient.cs +++ b/MistoxServer/Client/UDPClient.cs @@ -41,13 +41,8 @@ namespace Mistox.Client { ushort DataLength = (ushort)((dataPacket[1] << 8) | dataPacket[2]); ReadOnlyMemory 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 - dynamic buildObj = DataPacket.Deserialize( resolvedType, DataLength, data ); + dynamic buildObj = DataPacket.Deserialize( dataType, DataLength, data ); // Return the object ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj ); diff --git a/MistoxServer/Interface/Interface.cs b/MistoxServer/Interface/Interface.cs index 4d783dc..40e6194 100644 --- a/MistoxServer/Interface/Interface.cs +++ b/MistoxServer/Interface/Interface.cs @@ -1,16 +1,17 @@ using System; using System.Net; +using Mistox.Attributes; namespace Mistox { public class mServer { public static ServerInterface newServer( string serverName, int port ) { - DataPacket.BuildNetworkPacketList(); + NetworkPacketDictionary.BuildNetworkPacketList(); return new ServerInterface(port, serverName); } public static ClientInterface newClient( string ServerIPOrHostName, int Port ) { - DataPacket.BuildNetworkPacketList(); + NetworkPacketDictionary.BuildNetworkPacketList(); int index = 0; IPAddress[] remoteAddress = Dns.GetHostAddresses(ServerIPOrHostName); for( int i=0; i< remoteAddress.Length; i++ ) { diff --git a/MistoxServer/Server/TCPConnection.cs b/MistoxServer/Server/TCPConnection.cs index bb2fbef..72cd8d3 100644 --- a/MistoxServer/Server/TCPConnection.cs +++ b/MistoxServer/Server/TCPConnection.cs @@ -2,6 +2,7 @@ using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; +using Mistox.Attributes; namespace Mistox.Server { public class TCPConnection : IDisposable { @@ -41,13 +42,8 @@ namespace Mistox.Server { // Read off the data 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 - dynamic dataobject = DataPacket.Deserialize( resolvedType, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) ); + dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) ); // Return the object ServerInterface.ServerInstance.InvokeDataReceived(dataobject); diff --git a/MistoxServer/mPacket/DataPacket.cs b/MistoxServer/mPacket/DataPacket.cs index 7f4a57c..18f99d2 100644 --- a/MistoxServer/mPacket/DataPacket.cs +++ b/MistoxServer/mPacket/DataPacket.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Reflection; using MessagePack; using Mistox.Attributes; @@ -43,42 +42,30 @@ namespace Mistox { } - public static object Deserialize( Type PacketType, ushort DataLength, ReadOnlyMemory Data ) { + public static object Deserialize( byte PacketByte, ushort DataLength, ReadOnlyMemory Data ) { + // Get the type from the tag + if (!NetworkPacketDictionary.ServerToClientPackets.TryGetValue(PacketByte, out Type resolvedType)) { + throw new Exception($"Unknown packet type ID: {PacketByte}"); + } + // 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."); } - // 4. Create the low-level reader struct directly over the memory slice - var reader = new MessagePackReader(Data); + // Make sure the data is exactly the size requested + ReadOnlyMemory payloadSlice = Data.Slice(0, DataLength); + + // Create the low-level reader struct directly over the memory slice + var reader = new MessagePackReader(payloadSlice); // Extract the object - dynamic obj = MessagePackSerializer.Deserialize( PacketType, ref reader, MessagePackSerializerOptions.Standard ); + dynamic obj = MessagePackSerializer.Deserialize( resolvedType, ref reader, MessagePackSerializerOptions.Standard ); return obj; } - - - - // Fills out the NetworkPacket Lookup Dictionary - public static readonly Dictionary ClientToClientPackets = new(); - public static readonly Dictionary ServerToClientPackets = new(); - public static void BuildNetworkPacketList() { - Type[] allClasses = Assembly.GetExecutingAssembly().GetTypes(); - foreach( Type cur in allClasses) { - mPacket attribute = cur.GetCustomAttribute(false); - if (attribute != null) { - if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToClient) ) { - ClientToClientPackets[attribute.ID] = cur; - } else if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToServer) ) { - ServerToClientPackets[attribute.ID] = cur; - } - } - } - } - } } \ No newline at end of file diff --git a/MistoxServer/mPacket/NetworkPacketDictionary.cs b/MistoxServer/mPacket/NetworkPacketDictionary.cs new file mode 100644 index 0000000..b7c8efe --- /dev/null +++ b/MistoxServer/mPacket/NetworkPacketDictionary.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace Mistox.Attributes { + + public class NetworkPacketDictionary { + + // Fills out the NetworkPacket Lookup Dictionary + public static readonly Dictionary ClientToClientPackets = new(); + public static readonly Dictionary ServerToClientPackets = new(); + public static void BuildNetworkPacketList() { + Type[] allClasses = Assembly.GetExecutingAssembly().GetTypes(); + foreach( Type cur in allClasses) { + mPacket attribute = cur.GetCustomAttribute(false); + if (attribute != null) { + if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToClient) ) { + ClientToClientPackets[attribute.ID] = cur; + } else if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToServer) ) { + ServerToClientPackets[attribute.ID] = cur; + } + } + } + } + + } + +} \ No newline at end of file