Example #1
0
        public static UdpPacket Decode(Stream stream)
        {
            EndianBinaryReader endianBinaryReader = stream != null?stream.CreateSuitableBinaryReader() : throw new ArgumentNullException(nameof(stream));

            short         num1          = endianBinaryReader.ReadInt16();
            short         num2          = endianBinaryReader.ReadInt16();
            UdpPacketType udpPacketType = (UdpPacketType)endianBinaryReader.ReadByte();
            byte          num3          = endianBinaryReader.ReadByte();

            switch (udpPacketType)
            {
            case UdpPacketType.DataReqAck:
            case UdpPacketType.DataNotReqAck:
                if (num1 == 5)
                {
                    byte      num4       = endianBinaryReader.ReadByte();
                    UdpPacket udpPacket1 = new UdpPacket();
                    udpPacket1.Id   = num2;
                    udpPacket1.Type = udpPacketType;
                    UdpPacket   udpPacket2   = udpPacket1;
                    UdpResponse udpResponse1 = new UdpResponse();
                    udpResponse1.AvlPacketId = num3;
                    udpResponse1.AcceptedNum = num4;
                    UdpResponse udpResponse2 = udpResponse1;
                    udpPacket2.Payload = udpResponse2;
                    return(udpPacket1);
                }
                string    s          = ImeiEncoding.Decode(stream);
                int       count      = num1 - 2 - 1 - 1 - (2 + ImeiEncoding.Encoding.GetByteCount(s));
                byte[]    numArray   = endianBinaryReader.ReadBytes(count);
                UdpPacket udpPacket3 = new UdpPacket();
                udpPacket3.Id   = num2;
                udpPacket3.Type = udpPacketType;
                UdpPacket udpPacket4 = udpPacket3;
                UdpData   udpData1   = new UdpData();
                udpData1.AvlPacketId = num3;
                udpData1.Imei        = s;
                udpData1.Data        = numArray;
                UdpData udpData2 = udpData1;
                udpPacket4.Payload = udpData2;
                return(udpPacket3);

            case UdpPacketType.Ack:
                endianBinaryReader.ReadByte();
                return(new UdpPacket()
                {
                    Id = num2,
                    Type = udpPacketType,
                    Payload = null
                });

            default:
                throw new ApplicationException("Not supported packet type");
            }
        }
Example #2
0
        public static void Encode(UdpPacket packet, Stream stream)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }
            EndianBinaryWriter endianBinaryWriter = stream != null?stream.CreateSuitableBinaryWriter() : throw new ArgumentNullException(nameof(stream));

            switch (packet.Type)
            {
            case UdpPacketType.DataReqAck:
            case UdpPacketType.DataNotReqAck:
                UdpData payload1 = packet.Payload as UdpData;
                if (payload1 != null)
                {
                    int num = 6 + ImeiEncoding.Encoding.GetByteCount(payload1.Imei) + payload1.Data.Length;
                    endianBinaryWriter.Write((short)num);
                    endianBinaryWriter.Write(packet.Id);
                    endianBinaryWriter.Write((byte)packet.Type);
                    endianBinaryWriter.Write(payload1.AvlPacketId);
                    endianBinaryWriter.Write(ImeiEncoding.Encode(payload1.Imei));
                    endianBinaryWriter.Write(payload1.Data);
                    break;
                }
                UdpResponse payload2 = packet.Payload as UdpResponse;
                if (payload2 == null)
                {
                    throw new InvalidOperationException("Payload");
                }
                endianBinaryWriter.Write((short)5);
                endianBinaryWriter.Write(packet.Id);
                endianBinaryWriter.Write((byte)packet.Type);
                endianBinaryWriter.Write(payload2.AvlPacketId);
                endianBinaryWriter.Write(payload2.AcceptedNum);
                break;

            case UdpPacketType.Ack:
                if (packet.Payload as UdpResponse == null)
                {
                    throw new InvalidOperationException("UdpAck");
                }
                endianBinaryWriter.Write((short)3);
                endianBinaryWriter.Write(packet.Id);
                endianBinaryWriter.Write((byte)packet.Type);
                break;

            default:
                throw new ApplicationException("Not supported packet type");
            }
            endianBinaryWriter.Flush();
        }