示例#1
0
        public static UDPPackage Parse(byte[] bytes)
        {
            Stream       stream   = new MemoryStream(bytes);
            BinaryReader reader   = new BinaryReader(stream);
            uint         crc      = reader.ReadUInt32();
            uint         checkCRC = CRC32.Calculate(reader.ReadBytes(bytes.Length - 4));

            if (crc != checkCRC)
            {
                return(new UDPPackage());                 //пакет битый
            }
            stream.Position = 4;
            uint      sign  = reader.ReadUInt32();
            ushort    parts = reader.ReadUInt16();
            BytesList body  = new BytesList();

            if (parts == 0)
            {
                body.Add(reader.ReadBytes(bytes.Length - 4 - 4 - 2));
                return(new UDPPackage(body, sign));
            }
            else
            {
                ushort part           = reader.ReadUInt16();
                ushort partsPackageId = reader.ReadUInt16();
                body.Add(reader.ReadBytes(bytes.Length - 4 - 4 - 2 - 2 - 2));
                return(new UDPPackage(partsPackageId, parts, part, body, sign));
            }
        }
示例#2
0
        public void TestNested()
        {
            var ba1 = new BytesList();
            var ba2 = new BytesList();

            ba1.Add(new[] { (byte)'a', (byte)'b', (byte)'c' });
            ba1.Add(new[] { (byte)'d', (byte)'e' });

            ba2.Add(new[] { (byte)'f', (byte)'g' });
            ba2.Add((byte)'h');
            ba2.Add(ba1);

            Assert.AreEqual(8, ba2.BytesLength);

            byte[] bytes = ba2.Bytes();

            var ascii = Encoding.ASCII.GetString(bytes);

            Assert.AreEqual("fghabcde", ascii);
        }
示例#3
0
        public UDPPackage(BytesList body, UInt32 sign = 0)
        {
            this.Body = body;
            this.Sign = sign;

            BytesList preCRCData = new BytesList();

            preCRCData.AddUInt(this.Sign);
            preCRCData.AddUShort(this.Parts);//==0
            preCRCData.Add(body);

            this.CRC = CRC32.Calculate(preCRCData);
            this.FullData.AddUInt(this.CRC);
            this.FullData.Add(preCRCData);
        }
示例#4
0
 public ShellCommand(CommandBaseUDP command, ushort initPartsPackageId)
 {
     if (command.BytesList.Count > maxByteSize)
     {
         ushort parts = (ushort)(command.BytesList.Count / maxByteSize);
         this.PartsPackageId = initPartsPackageId;
         for (ushort part = 0; part < parts; part++)
         {
             BytesList data = new BytesList(); data.Add(command.BytesList.GetBytes(maxByteSize * part, maxByteSize));
             this.Packages.Add(new UDPPackage(this.PartsPackageId, parts, part, data));
         }
     }
     else
     {
         this.Packages.Add(new UDPPackage(command.BytesList));
     }
 }
示例#5
0
        public UDPPackage(ushort partsPackageId, ushort parts, ushort part, BytesList body, UInt32 sign = 0)
        {
            this.PartsPackageId = partsPackageId;
            this.Parts          = parts;
            this.Part           = part;
            this.Body           = body;
            this.Sign           = sign;

            BytesList preCRCData = new BytesList();

            preCRCData.AddUInt(this.Sign);
            preCRCData.AddUShort(this.Parts);//!=0
            preCRCData.AddUShort(this.Part);
            preCRCData.AddUShort(this.PartsPackageId);
            preCRCData.Add(body);

            this.CRC = CRC32.Calculate(preCRCData);
            this.FullData.AddUInt(this.CRC);
            this.FullData.Add(preCRCData);
        }