Shrink packet header size
This commit is contained in:
1 parent
c3906e7d60
commit
7b58ca0338
10 files changed
+140
-115
No files matched your search
+30
-58
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using MsgPack.Serialization;
|
||||
|
||||
namespace MistoxServer {
|
||||
|
||||
@@ -18,74 +15,49 @@ namespace MistoxServer {
|
||||
Array.Copy( data, index, result, 0, length );
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 ) {
|
||||
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];
|
||||
static byte[] TBufferedData = [];
|
||||
public static dynamic tReceive( byte [] BytesRead ) {
|
||||
// Append the new data onto the old data
|
||||
TBufferedData = TBufferedData.Join( BytesRead );
|
||||
if( TBufferedData.Length > 4 ) {
|
||||
int typeLength = BitConverter.ToInt32( TBufferedData.Sub(0, 4) );
|
||||
if( TBufferedData.Length > (8 + typeLength) ) {
|
||||
int dataLength = BitConverter.ToInt32( TBufferedData.Sub( typeLength + 4, 4 ) );
|
||||
int TotalLength = 8 + typeLength + dataLength;
|
||||
if ( TBufferedData.Length >= TotalLength ) {
|
||||
string typeData = Encoding.UTF8.GetString( TBufferedData.Sub(4, typeLength) );
|
||||
if ( typeData.Substring(0, 13) != "System.Object" ) {
|
||||
byte[] dataBytes = TBufferedData.Sub( (typeLength + 8), dataLength );
|
||||
dynamic data = mSerialize.PacketDeserialize( typeData, dataBytes );
|
||||
TBufferedData = TBufferedData.Sub( TotalLength, TBufferedData.Length - TotalLength );
|
||||
return data;
|
||||
} else {
|
||||
TBufferedData = TBufferedData.Sub( TotalLength, TBufferedData.Length - TotalLength );
|
||||
}
|
||||
}
|
||||
|
||||
// If the total of the data is bigger than the header
|
||||
if( TBufferedData.Length > DataPacket.HeaderLength ) {
|
||||
DataPacket packet = new DataPacket() {
|
||||
PacketType = BytesRead[0],
|
||||
DataLength = BitConverter.ToUInt16( BytesRead.Sub(1,2) )
|
||||
};
|
||||
|
||||
// If the total of the data is bigger then the whole packet
|
||||
if (TBufferedData.Length > DataPacket.HeaderLength + packet.DataLength) {
|
||||
packet.PacketData = TBufferedData.Sub( DataPacket.HeaderLength, packet.DataLength );
|
||||
TBufferedData = TBufferedData.Sub( DataPacket.HeaderLength, TBufferedData.Length - ( DataPacket.HeaderLength + packet.DataLength ) );
|
||||
return packet.Deserialize();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static byte[] UBufferedData = new byte[0];
|
||||
static byte[] UBufferedData = [];
|
||||
public static dynamic uReceive( byte [] BytesRead ) {
|
||||
// Append the new data onto the old data
|
||||
UBufferedData = UBufferedData.Join( BytesRead );
|
||||
if( UBufferedData.Length > 4 ) {
|
||||
int typeLength = BitConverter.ToInt32( UBufferedData.Sub(0, 4) );
|
||||
if( UBufferedData.Length > (8 + typeLength) ) {
|
||||
int dataLength = BitConverter.ToInt32( UBufferedData.Sub( typeLength + 4, 4 ) );
|
||||
int TotalLength = 8 + typeLength + dataLength;
|
||||
if( UBufferedData.Length >= TotalLength ) {
|
||||
string typeData = Encoding.UTF8.GetString( UBufferedData.Sub(4, typeLength) );
|
||||
if( typeData.Substring( 0, 13 ) != "System.Object" ) {
|
||||
byte[] dataBytes = UBufferedData.Sub( (typeLength + 8), dataLength );
|
||||
dynamic data = mSerialize.PacketDeserialize( typeData, dataBytes );
|
||||
UBufferedData = UBufferedData.Sub( TotalLength, UBufferedData.Length - TotalLength );
|
||||
return data;
|
||||
} else {
|
||||
UBufferedData = UBufferedData.Sub( TotalLength, UBufferedData.Length - TotalLength );
|
||||
}
|
||||
}
|
||||
|
||||
// If the total of the data is bigger than the header
|
||||
if( UBufferedData.Length > DataPacket.HeaderLength ) {
|
||||
DataPacket packet = new DataPacket() {
|
||||
PacketType = BytesRead[0],
|
||||
DataLength = BitConverter.ToUInt16( BytesRead.Sub(1,2) )
|
||||
};
|
||||
|
||||
// If the total of the data is bigger then the whole packet
|
||||
if (UBufferedData.Length > DataPacket.HeaderLength + packet.DataLength) {
|
||||
packet.PacketData = UBufferedData.Sub( DataPacket.HeaderLength, packet.DataLength );
|
||||
UBufferedData = UBufferedData.Sub( DataPacket.HeaderLength, UBufferedData.Length - ( DataPacket.HeaderLength + packet.DataLength ) );
|
||||
return packet.Deserialize();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user