Shrink packet header size
This commit is contained in:
1 parent
c3906e7d60
commit
7b58ca0338
10 files changed
+131
-106
No files matched your search
@@ -57,8 +57,8 @@ namespace MistoxServer.Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send<Packet>(Packet packet) {
|
public async Task Send<T>(T packet) {
|
||||||
byte[] data = mSerialize.PacketSerialize( packet );
|
byte[] data = DataPacket.Serialize( packet );
|
||||||
await Server.GetStream().WriteAsync( data, 0, data.Length );
|
await Server.GetStream().WriteAsync( data, 0, data.Length );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,20 +10,18 @@ namespace MistoxServer.Client {
|
|||||||
public class mUDPClient : IDisposable {
|
public class mUDPClient : IDisposable {
|
||||||
|
|
||||||
public event EventHandler onReceived;
|
public event EventHandler onReceived;
|
||||||
ServerMode Mode;
|
|
||||||
|
|
||||||
Socket udpClient;
|
Socket udpClient;
|
||||||
bool Alive;
|
bool Alive;
|
||||||
int Port;
|
int Port;
|
||||||
|
|
||||||
public mUDPClient( IPEndPoint ServerAddress, ServerMode mode ) {
|
public mUDPClient( IPEndPoint ServerAddress ) {
|
||||||
udpClient = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
|
udpClient = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
|
||||||
udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true );
|
udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true );
|
||||||
udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 128 );
|
udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 128 );
|
||||||
udpClient.Bind( new IPEndPoint( IPAddress.Any, ServerAddress.Port ) );
|
udpClient.Bind( new IPEndPoint( IPAddress.Any, ServerAddress.Port ) );
|
||||||
Alive = true;
|
Alive = true;
|
||||||
Port = ServerAddress.Port;
|
Port = ServerAddress.Port;
|
||||||
Mode = mode;
|
|
||||||
Thread Client = new Thread(async() => { await ReceiveThread(); });
|
Thread Client = new Thread(async() => { await ReceiveThread(); });
|
||||||
Client.Start();
|
Client.Start();
|
||||||
}
|
}
|
||||||
@@ -33,28 +31,19 @@ namespace MistoxServer.Client {
|
|||||||
try {
|
try {
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int bytesRead = await udpClient.ReceiveAsync( buffer );
|
int bytesRead = await udpClient.ReceiveAsync( buffer );
|
||||||
if (Mode == ServerMode.Passive) {
|
|
||||||
onReceived?.Invoke( buffer.Sub( 0, bytesRead ), new EventArgs() );
|
|
||||||
} else if (Mode == ServerMode.Authoritative) {
|
|
||||||
dynamic data = mSerialize.uReceive( buffer.Sub(0, bytesRead) );
|
dynamic data = mSerialize.uReceive( buffer.Sub(0, bytesRead) );
|
||||||
if( data != null ) {
|
if( data != null ) {
|
||||||
onReceived?.Invoke( data, new EventArgs() );
|
onReceived?.Invoke( data, new EventArgs() );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch( Exception ) {
|
} catch( Exception ) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send<Packet>( Packet Data, IPEndPoint remoteHost ) {
|
public async Task Send<T>( T Data, IPEndPoint remoteHost ) {
|
||||||
if (Mode == ServerMode.Authoritative) {
|
byte[] byteData = DataPacket.Serialize( Data );
|
||||||
byte[] byteData = mSerialize.PacketSerialize( Data );
|
|
||||||
await udpClient.SendToAsync( byteData, SocketFlags.None, new IPEndPoint( remoteHost.Address, Port ) );
|
await udpClient.SendToAsync( byteData, SocketFlags.None, new IPEndPoint( remoteHost.Address, Port ) );
|
||||||
} else if (Mode == ServerMode.Passive) {
|
|
||||||
byte[] byteData = Data as byte[];
|
|
||||||
await udpClient.SendToAsync( byteData, SocketFlags.None, new IPEndPoint( remoteHost.Address, Port ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ namespace MistoxServer {
|
|||||||
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + mPEndPoint.Address );
|
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + mPEndPoint.Address );
|
||||||
// Make a UDP connection to the server
|
// Make a UDP connection to the server
|
||||||
try {
|
try {
|
||||||
FastUpdate = new mUDPClient( mPEndPoint, ServerMode.Authoritative );
|
FastUpdate = new mUDPClient( mPEndPoint );
|
||||||
FastUpdate.onReceived += (object o, EventArgs e) => { onFastReceive?.Invoke( o, e ); };
|
FastUpdate.onReceived += (object o, EventArgs e) => { onFastReceive?.Invoke( o, e ); };
|
||||||
} catch( Exception e ) {
|
} catch( Exception e ) {
|
||||||
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
|
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ using System.Net;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MistoxServer {
|
namespace MistoxServer {
|
||||||
public partial class mServer {
|
public class mServer {
|
||||||
public static IMistoxServer newServer(int port, ServerMode mode) {
|
|
||||||
return new ServerInterface(port, mode);
|
public static IMistoxServer newServer(int port) {
|
||||||
|
DataPacket.BuildNetworkPacketList();
|
||||||
|
return new ServerInterface(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IMistoxServer newClient(string ServerIPOrHostName, int Port) {
|
public static IMistoxServer newClient(string ServerIPOrHostName, int Port) {
|
||||||
|
DataPacket.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++ ) {
|
||||||
@@ -24,17 +27,16 @@ namespace MistoxServer {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IMistoxServer {
|
public interface IMistoxServer {
|
||||||
|
|
||||||
public event EventHandler onConnected;
|
public event EventHandler onConnected;
|
||||||
public event EventHandler onSlowReceive;
|
public event EventHandler onSlowReceive;
|
||||||
public event EventHandler onFastReceive;
|
public event EventHandler onFastReceive;
|
||||||
public event EventHandler onDisconnected;
|
public event EventHandler onDisconnected;
|
||||||
|
|
||||||
public Task Send<Packet>(Packet data, SendType speed);
|
public Task Send<Packet>(Packet data, SendType speed);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum SendType {
|
public enum SendType {
|
||||||
@@ -42,9 +44,4 @@ namespace MistoxServer {
|
|||||||
FastUpdate
|
FastUpdate
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum ServerMode {
|
|
||||||
Passive,
|
|
||||||
Authoritative
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -17,14 +17,16 @@ namespace MistoxServer {
|
|||||||
public event EventHandler onFastReceive;
|
public event EventHandler onFastReceive;
|
||||||
public event EventHandler onDisconnected;
|
public event EventHandler onDisconnected;
|
||||||
|
|
||||||
public ServerInterface( int port, ServerMode mode ) {
|
public ServerInterface( int port ) {
|
||||||
FastUpdateServer = new mUDPClient( new IPEndPoint( IPAddress.IPv6Any, port ), mode );
|
|
||||||
SlowUpdateServer = new mTCPListener( port, mode );
|
|
||||||
|
|
||||||
|
// Initilize the server listening connections
|
||||||
|
SlowUpdateServer = new mTCPListener( port );
|
||||||
SlowUpdateServer.onConnected += OnConnected;
|
SlowUpdateServer.onConnected += OnConnected;
|
||||||
FastUpdateServer.onReceived += ( object o, EventArgs e ) => { onFastReceive?.Invoke( o, e ); };
|
|
||||||
SlowUpdateServer.onDisconnected += OnDisconnected;
|
SlowUpdateServer.onDisconnected += OnDisconnected;
|
||||||
|
|
||||||
|
FastUpdateServer = new mUDPClient( new IPEndPoint( IPAddress.Any, port ) );
|
||||||
|
FastUpdateServer.onReceived += ( object o, EventArgs e ) => { onFastReceive?.Invoke( o, e ); };
|
||||||
|
|
||||||
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + port );
|
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + port );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,12 @@ namespace MistoxServer.Server {
|
|||||||
public event EventHandler onReceive;
|
public event EventHandler onReceive;
|
||||||
|
|
||||||
TcpListener Listener;
|
TcpListener Listener;
|
||||||
ServerMode ServerMode;
|
|
||||||
bool Alive;
|
bool Alive;
|
||||||
int port;
|
int port;
|
||||||
|
|
||||||
public mTCPListener( int Port, ServerMode mode ) {
|
public mTCPListener( int Port ) {
|
||||||
port = Port;
|
port = Port;
|
||||||
Alive = true;
|
Alive = true;
|
||||||
ServerMode = mode;
|
|
||||||
Thread ConnectionThread = new Thread(async () => await ListenerThread() );
|
Thread ConnectionThread = new Thread(async () => await ListenerThread() );
|
||||||
ConnectionThread.Start();
|
ConnectionThread.Start();
|
||||||
}
|
}
|
||||||
@@ -33,7 +31,7 @@ namespace MistoxServer.Server {
|
|||||||
TcpClient client = await Listener.AcceptTcpClientAsync();
|
TcpClient client = await Listener.AcceptTcpClientAsync();
|
||||||
|
|
||||||
Connection user = new Connection(){
|
Connection user = new Connection(){
|
||||||
slowClient = new mTCPServer(client, ServerMode),
|
slowClient = new mTCPServer( client ),
|
||||||
fastClient = new IPEndPoint( ((IPEndPoint)client.Client.RemoteEndPoint).Address, port )
|
fastClient = new IPEndPoint( ((IPEndPoint)client.Client.RemoteEndPoint).Address, port )
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,15 +5,13 @@ using System.Threading.Tasks;
|
|||||||
namespace MistoxServer.Server {
|
namespace MistoxServer.Server {
|
||||||
public class mTCPServer : IDisposable {
|
public class mTCPServer : IDisposable {
|
||||||
public TcpClient slowClient;
|
public TcpClient slowClient;
|
||||||
ServerMode Mode;
|
|
||||||
|
|
||||||
public event EventHandler onReceived;
|
public event EventHandler onReceived;
|
||||||
public event EventHandler onDisconnected;
|
public event EventHandler onDisconnected;
|
||||||
|
|
||||||
public mTCPServer( TcpClient client, ServerMode mode ) {
|
public mTCPServer( TcpClient client ) {
|
||||||
slowClient = client;
|
slowClient = client;
|
||||||
slowClient.Client.NoDelay = true;
|
slowClient.Client.NoDelay = true;
|
||||||
Mode = mode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Alive = true;
|
bool Alive = true;
|
||||||
@@ -24,15 +22,11 @@ namespace MistoxServer.Server {
|
|||||||
byte[] StreamData = new byte[1024];
|
byte[] StreamData = new byte[1024];
|
||||||
int bytesRead = await slowClient.GetStream().ReadAsync(StreamData, 0, StreamData.Length);
|
int bytesRead = await slowClient.GetStream().ReadAsync(StreamData, 0, StreamData.Length);
|
||||||
if( bytesRead > 0 ) {
|
if( bytesRead > 0 ) {
|
||||||
if ( Mode == ServerMode.Passive) {
|
|
||||||
onReceived?.Invoke( StreamData.Sub( 0, bytesRead ), new EventArgs() );
|
|
||||||
} else if ( Mode == ServerMode.Authoritative) {
|
|
||||||
dynamic data = mSerialize.tReceive( StreamData.Sub( 0, bytesRead ) );
|
dynamic data = mSerialize.tReceive( StreamData.Sub( 0, bytesRead ) );
|
||||||
if( data != null ) {
|
if( data != null ) {
|
||||||
onReceived?.Invoke( data, new EventArgs() );
|
onReceived?.Invoke( data, new EventArgs() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch( Exception ) {
|
} catch( Exception ) {
|
||||||
Console.WriteLine( "User disconnected" );
|
Console.WriteLine( "User disconnected" );
|
||||||
connected = false;
|
connected = false;
|
||||||
@@ -42,13 +36,8 @@ namespace MistoxServer.Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send<T>( T packet ) {
|
public async Task Send<T>( T packet ) {
|
||||||
if( Mode == ServerMode.Authoritative ) {
|
byte[] byteData = DataPacket.Serialize( packet );
|
||||||
byte[] byteData = mSerialize.PacketSerialize( packet );
|
|
||||||
await slowClient.GetStream().WriteAsync( byteData, 0, byteData.Length );
|
await slowClient.GetStream().WriteAsync( byteData, 0, byteData.Length );
|
||||||
} else if( Mode == ServerMode.Passive ) {
|
|
||||||
byte[] byteData = packet as byte[];
|
|
||||||
await slowClient.GetStream().WriteAsync( byteData, 0, byteData.Length );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
|
|||||||
+30
-58
@@ -1,7 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
|
||||||
using MsgPack.Serialization;
|
|
||||||
|
|
||||||
namespace MistoxServer {
|
namespace MistoxServer {
|
||||||
|
|
||||||
@@ -18,74 +15,49 @@ namespace MistoxServer {
|
|||||||
Array.Copy( data, index, result, 0, length );
|
Array.Copy( data, index, result, 0, length );
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class mSerialize {
|
public class mSerialize {
|
||||||
public static byte[] PacketSerialize<T>( T Packet ) {
|
|
||||||
MessagePackSerializer serializer = MessagePackSerializer.Get<T>();
|
|
||||||
using( MemoryStream stream = new MemoryStream() ) {
|
|
||||||
serializer.Pack( stream, Packet );
|
|
||||||
byte[] typename = Encoding.UTF8.GetBytes(typeof(T).FullName + "," + typeof(T).Assembly.FullName);
|
|
||||||
byte[] typelength = BitConverter.GetBytes(typename.Length);
|
|
||||||
byte[] packetdata = stream.ToArray();
|
|
||||||
byte[] paketlength = BitConverter.GetBytes(packetdata.Length);
|
|
||||||
return typelength.Join( typename ).Join( paketlength ).Join( packetdata );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static object PacketDeserialize( string typeData, byte[] Data ) {
|
static byte[] TBufferedData = [];
|
||||||
Type type = Type.GetType( typeData );
|
|
||||||
MessagePackSerializer Serilizer = MessagePackSerializer.Get(type);
|
|
||||||
|
|
||||||
using( MemoryStream ms = new MemoryStream( Data ) ) {
|
|
||||||
return (object)Serilizer.Unpack( ms );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static byte[] TBufferedData = new byte[0];
|
|
||||||
public static dynamic tReceive( byte [] BytesRead ) {
|
public static dynamic tReceive( byte [] BytesRead ) {
|
||||||
|
// Append the new data onto the old data
|
||||||
TBufferedData = TBufferedData.Join( BytesRead );
|
TBufferedData = TBufferedData.Join( BytesRead );
|
||||||
if( TBufferedData.Length > 4 ) {
|
|
||||||
int typeLength = BitConverter.ToInt32( TBufferedData.Sub(0, 4) );
|
// If the total of the data is bigger than the header
|
||||||
if( TBufferedData.Length > (8 + typeLength) ) {
|
if( TBufferedData.Length > DataPacket.HeaderLength ) {
|
||||||
int dataLength = BitConverter.ToInt32( TBufferedData.Sub( typeLength + 4, 4 ) );
|
DataPacket packet = new DataPacket() {
|
||||||
int TotalLength = 8 + typeLength + dataLength;
|
PacketType = BytesRead[0],
|
||||||
if ( TBufferedData.Length >= TotalLength ) {
|
DataLength = BitConverter.ToUInt16( BytesRead.Sub(1,2) )
|
||||||
string typeData = Encoding.UTF8.GetString( TBufferedData.Sub(4, typeLength) );
|
};
|
||||||
if ( typeData.Substring(0, 13) != "System.Object" ) {
|
|
||||||
byte[] dataBytes = TBufferedData.Sub( (typeLength + 8), dataLength );
|
// If the total of the data is bigger then the whole packet
|
||||||
dynamic data = mSerialize.PacketDeserialize( typeData, dataBytes );
|
if (TBufferedData.Length > DataPacket.HeaderLength + packet.DataLength) {
|
||||||
TBufferedData = TBufferedData.Sub( TotalLength, TBufferedData.Length - TotalLength );
|
packet.PacketData = TBufferedData.Sub( DataPacket.HeaderLength, packet.DataLength );
|
||||||
return data;
|
TBufferedData = TBufferedData.Sub( DataPacket.HeaderLength, TBufferedData.Length - ( DataPacket.HeaderLength + packet.DataLength ) );
|
||||||
} else {
|
return packet.Deserialize();
|
||||||
TBufferedData = TBufferedData.Sub( TotalLength, TBufferedData.Length - TotalLength );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static byte[] UBufferedData = new byte[0];
|
static byte[] UBufferedData = [];
|
||||||
public static dynamic uReceive( byte [] BytesRead ) {
|
public static dynamic uReceive( byte [] BytesRead ) {
|
||||||
|
// Append the new data onto the old data
|
||||||
UBufferedData = UBufferedData.Join( BytesRead );
|
UBufferedData = UBufferedData.Join( BytesRead );
|
||||||
if( UBufferedData.Length > 4 ) {
|
|
||||||
int typeLength = BitConverter.ToInt32( UBufferedData.Sub(0, 4) );
|
// If the total of the data is bigger than the header
|
||||||
if( UBufferedData.Length > (8 + typeLength) ) {
|
if( UBufferedData.Length > DataPacket.HeaderLength ) {
|
||||||
int dataLength = BitConverter.ToInt32( UBufferedData.Sub( typeLength + 4, 4 ) );
|
DataPacket packet = new DataPacket() {
|
||||||
int TotalLength = 8 + typeLength + dataLength;
|
PacketType = BytesRead[0],
|
||||||
if( UBufferedData.Length >= TotalLength ) {
|
DataLength = BitConverter.ToUInt16( BytesRead.Sub(1,2) )
|
||||||
string typeData = Encoding.UTF8.GetString( UBufferedData.Sub(4, typeLength) );
|
};
|
||||||
if( typeData.Substring( 0, 13 ) != "System.Object" ) {
|
|
||||||
byte[] dataBytes = UBufferedData.Sub( (typeLength + 8), dataLength );
|
// If the total of the data is bigger then the whole packet
|
||||||
dynamic data = mSerialize.PacketDeserialize( typeData, dataBytes );
|
if (UBufferedData.Length > DataPacket.HeaderLength + packet.DataLength) {
|
||||||
UBufferedData = UBufferedData.Sub( TotalLength, UBufferedData.Length - TotalLength );
|
packet.PacketData = UBufferedData.Sub( DataPacket.HeaderLength, packet.DataLength );
|
||||||
return data;
|
UBufferedData = UBufferedData.Sub( DataPacket.HeaderLength, UBufferedData.Length - ( DataPacket.HeaderLength + packet.DataLength ) );
|
||||||
} else {
|
return packet.Deserialize();
|
||||||
UBufferedData = UBufferedData.Sub( TotalLength, UBufferedData.Length - TotalLength );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user