Wire up the event handlers
This commit is contained in:
1 parent
a04516c470
commit
a689f2656f
11 files changed
+90
-76
No files matched your search
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
|
||||
namespace Mistox {
|
||||
|
||||
static class Extensions {
|
||||
public static T[] Join<T>( this T[] first, T[] second ) {
|
||||
T[] bytes = new T[first.Length + second.Length];
|
||||
Buffer.BlockCopy( first, 0, bytes, 0, first.Length );
|
||||
Buffer.BlockCopy( second, 0, bytes, first.Length, second.Length );
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static T[] Sub<T>( this T[] data, int index, int length ) {
|
||||
T[] result = new T[length];
|
||||
Array.Copy( data, index, result, 0, length );
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class Serializer {
|
||||
|
||||
static byte[] TBufferedData = [];
|
||||
public static dynamic tReceive( byte [] BytesRead ) {
|
||||
// Append the new data onto the old data
|
||||
TBufferedData = TBufferedData.Join( BytesRead );
|
||||
|
||||
// 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 = [];
|
||||
public static dynamic uReceive( byte [] BytesRead ) {
|
||||
// Append the new data onto the old data
|
||||
UBufferedData = UBufferedData.Join( BytesRead );
|
||||
|
||||
// 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