Split apart the server packets from the client packets
This commit is contained in:
1 parent
ca39e184ed
commit
392a33eb9e
5 files changed
+38
-12
No files matched your search
@@ -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
|
||||
if ( DataLength <= Data.Length ) {
|
||||
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
|
||||
var reader = new MessagePackReader(Data);
|
||||
|
||||
// Extract the object
|
||||
dynamic obj = MessagePackSerializer.Deserialize( resolvedType, ref reader, MessagePackSerializerOptions.Standard );
|
||||
dynamic obj = MessagePackSerializer.Deserialize( PacketType, ref reader, MessagePackSerializerOptions.Standard );
|
||||
|
||||
return obj;
|
||||
|
||||
@@ -69,13 +64,18 @@ namespace Mistox {
|
||||
|
||||
|
||||
// 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() {
|
||||
Type[] allClasses = Assembly.GetExecutingAssembly().GetTypes();
|
||||
foreach( Type cur in allClasses) {
|
||||
mPacket attribute = cur.GetCustomAttribute<mPacket>(false);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user