Seperate out the NetworkPacketDictonary
This commit is contained in:
1 parent
531fd2572f
commit
3992c3debc
6 files changed
+48
-46
No files matched your search
@@ -42,13 +42,8 @@ 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( resolvedType, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
|
dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
|
||||||
|
|
||||||
// Return the object
|
// Return the object
|
||||||
ClientInterface.ServerClientInstance.InvokeOnReceived(dataobject);
|
ClientInterface.ServerClientInstance.InvokeOnReceived(dataobject);
|
||||||
|
|||||||
@@ -41,13 +41,8 @@ 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( resolvedType, DataLength, data );
|
dynamic buildObj = DataPacket.Deserialize( dataType, DataLength, data );
|
||||||
|
|
||||||
// Return the object
|
// Return the object
|
||||||
ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj );
|
ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj );
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using Mistox.Attributes;
|
||||||
|
|
||||||
namespace Mistox {
|
namespace Mistox {
|
||||||
public class mServer {
|
public class mServer {
|
||||||
|
|
||||||
public static ServerInterface newServer( string serverName, int port ) {
|
public static ServerInterface newServer( string serverName, int port ) {
|
||||||
DataPacket.BuildNetworkPacketList();
|
NetworkPacketDictionary.BuildNetworkPacketList();
|
||||||
return new ServerInterface(port, serverName);
|
return new ServerInterface(port, serverName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ClientInterface newClient( string ServerIPOrHostName, int Port ) {
|
public static ClientInterface newClient( string ServerIPOrHostName, int Port ) {
|
||||||
DataPacket.BuildNetworkPacketList();
|
NetworkPacketDictionary.BuildNetworkPacketList();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
IPAddress[] remoteAddress = Dns.GetHostAddresses(ServerIPOrHostName);
|
IPAddress[] remoteAddress = Dns.GetHostAddresses(ServerIPOrHostName);
|
||||||
for( int i=0; i< remoteAddress.Length; i++ ) {
|
for( int i=0; i< remoteAddress.Length; i++ ) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Mistox.Attributes;
|
||||||
|
|
||||||
namespace Mistox.Server {
|
namespace Mistox.Server {
|
||||||
public class TCPConnection : IDisposable {
|
public class TCPConnection : IDisposable {
|
||||||
@@ -41,13 +42,8 @@ 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( resolvedType, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
|
dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) );
|
||||||
|
|
||||||
// Return the object
|
// Return the object
|
||||||
ServerInterface.ServerInstance.InvokeDataReceived(dataobject);
|
ServerInterface.ServerInstance.InvokeDataReceived(dataobject);
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using MessagePack;
|
using MessagePack;
|
||||||
using Mistox.Attributes;
|
using Mistox.Attributes;
|
||||||
@@ -43,42 +42,30 @@ namespace Mistox {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static object Deserialize( Type PacketType, ushort DataLength, ReadOnlyMemory<byte> Data ) {
|
public static object Deserialize( byte PacketByte, ushort DataLength, ReadOnlyMemory<byte> 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
|
// 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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Create the low-level reader struct directly over the memory slice
|
// Make sure the data is exactly the size requested
|
||||||
var reader = new MessagePackReader(Data);
|
ReadOnlyMemory<byte> payloadSlice = Data.Slice(0, DataLength);
|
||||||
|
|
||||||
|
// Create the low-level reader struct directly over the memory slice
|
||||||
|
var reader = new MessagePackReader(payloadSlice);
|
||||||
|
|
||||||
// Extract the object
|
// Extract the object
|
||||||
dynamic obj = MessagePackSerializer.Deserialize( PacketType, ref reader, MessagePackSerializerOptions.Standard );
|
dynamic obj = MessagePackSerializer.Deserialize( resolvedType, ref reader, MessagePackSerializerOptions.Standard );
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Fills out the NetworkPacket Lookup Dictionary
|
|
||||||
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) {
|
|
||||||
if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToClient) ) {
|
|
||||||
ClientToClientPackets[attribute.ID] = cur;
|
|
||||||
} else if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToServer) ) {
|
|
||||||
ServerToClientPackets[attribute.ID] = cur;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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<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) {
|
||||||
|
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