using System; namespace Mistox { static class Extensions { public static T[] Join( 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( this T[] data, int index, int length ) { T[] result = new T[length]; Array.Copy( data, index, result, 0, length ); return result; } } public class mSerialize { 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; } } }