Пример #1
0
        public byte[] Serialize()
        {
            int    totalLen = Legth + RUDP_HEADER_LENGTH;
            var    bs       = RSocketUtils.CreateArray <byte>(totalLen);
            Stream stream   = new MemoryStream(bs);
            var    bW       = new BinaryWriter(stream);

            bW.Seek(0, SeekOrigin.Begin);

            bW.Write(Protcol);
            bW.Write(CRC);
            bW.Write(Session);
            bW.Write((byte)Type);
            bW.Write(MsgNum);
            bW.Write(ACK);
            bW.Write(Piece);
            bW.Write(Legth);
            bW.Write(rawData, offset, len);

            CRC = RSocketUtils.CalcCrc16(bs, 3, totalLen - 3);
            bW.Seek(1, SeekOrigin.Begin);
            bW.Write(CRC);

            bW.Close();
            return(bs);
        }
Пример #2
0
        private bool CheckMsg(byte[] data, int offset, int len)
        {
            //Protocol Check
            var  br         = new BinaryReader(new MemoryStream(data, offset, len - offset));
            byte protocol   = br.ReadByte();
            bool isProtocol = PROTOCOL == protocol;

            if (!isProtocol)
            {
                return(isProtocol);
            }

            //CRC Check
            int    crcOffset = offset + CRC_OFFSET;
            UInt16 _crc      = RSocketUtils.CalcCrc16(data, crcOffset, len - CRC_OFFSET);
            var    msgCRC    = br.ReadUInt16();
            bool   isCRC     = msgCRC == _crc;

            //Repeat Check

            return(isCRC);
        }