public static EncapsulationHelper Decode(byte[] data) { EncapsulationHelper enchelper = new EncapsulationHelper(); enchelper.stream = new MemoryStream(data); enchelper.isDecode = true; enchelper.customPacketID = Convert.ToByte(enchelper.stream.ReadByte()); //enchelper.packetCount = packetCount; enchelper.stream.Position = 1; byte[] packetCountb = new byte[3]; enchelper.stream.Read(packetCountb, 0, 3); //Array.Reverse(packetCountb, 0, 3); enchelper.packetCount = new Triad(packetCountb); enchelper.stream.Position = 4; enchelper.encapsulationFormat = Convert.ToByte(enchelper.stream.ReadByte()); enchelper.stream.Position = 5; byte[] packetLengthb = new byte[2]; enchelper.stream.Read(packetLengthb, 0, 2); Array.Reverse(packetLengthb, 0, 2); enchelper.packetLength = BitConverter.ToInt16(packetLengthb, 0); if(enchelper.encapsulationFormat == 0x00){ enchelper.stream.Position = 7; enchelper.packetID = Convert.ToByte(enchelper.stream.ReadByte()); byte[] packetContent = new byte[enchelper.stream.Length - 8]; enchelper.stream.Read(packetContent, 0, packetContent.Length); enchelper.stream = new MemoryStream(packetContent); }else if(enchelper.encapsulationFormat == 0x40){ enchelper.stream.Position = 10; enchelper.packetID = Convert.ToByte(enchelper.stream.ReadByte()); byte[] packetContent = new byte[enchelper.stream.Length - 11]; enchelper.stream.Read(packetContent, 0, packetContent.Length); enchelper.stream = new MemoryStream(packetContent); }else if(enchelper.encapsulationFormat == 0x60){ enchelper.stream.Position = 14; enchelper.packetID = Convert.ToByte(enchelper.stream.ReadByte()); byte[] packetContent = new byte[enchelper.stream.Length - 15]; enchelper.stream.Read(packetContent, 0, packetContent.Length); enchelper.stream = new MemoryStream(packetContent); } return enchelper; }
public static EncapsulationHelper Encode(byte[] data, byte encapsulationFormat, Triad packetCount, byte customPacket) { EncapsulationHelper enchelper = new EncapsulationHelper(); enchelper.stream = new MemoryStream(); enchelper.isDecode = false; enchelper.customPacketID = customPacket; enchelper.stream.WriteByte(customPacket); enchelper.packetCount = packetCount; enchelper.encapsulationFormat = encapsulationFormat; enchelper.packetID = data[0]; //Custom Packet Format byte[] packetCountb = new byte[3]; packetCountb = packetCount.GetBytes(); //Array.Reverse(packetCountb, 0, 3); enchelper.stream.Write(packetCountb, 0, 3); enchelper.stream.WriteByte(encapsulationFormat); enchelper.packetLength = (short) data.Length; enchelper.GeneratePayload(); enchelper.stream.Write(data, 0, data.Length); return enchelper; }