コード例 #1
0
        public void Deserialize(PacketChecksumType checksumType, byte[] data)
        {
            MemoryStream       ms     = new MemoryStream(data);
            EndianBinaryReader reader = new EndianBinaryReader(EndianBitConverter.Little, ms);

            if (reader.ReadByte() != 0x01)
            {
                throw new InvalidDataException("Message start byte must be 0x01");
            }
            StatusCode status = (StatusCode)reader.ReadByte();

            if (status != StatusCode.CYRET_SUCCESS)
            {
                throw new BootloaderError(status);
            }
            ushort length = reader.ReadUInt16();

            if (length > data.Length - 7)
            {
                throw new InvalidDataException("Invalid data length");
            }
            this.DeserializePayload(reader, length);
            ushort checksum = reader.ReadUInt16();

            if (checksum != PacketChecksum.ComputeChecksum(checksumType, data, length + 4))
            {
                throw new InvalidDataException("Invalid checksum");
            }
            if (reader.ReadByte() != 0x17)
            {
                throw new InvalidDataException("Message end byte must be 0x17");
            }
        }
コード例 #2
0
        public byte[] Serialize()
        {
            MemoryStream       ms     = new MemoryStream();
            EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, ms);

            writer.Write((byte)0x01);         // Begin command
            writer.Write(this.CommandCode);   // Code
            writer.Write((UInt16)0x0000);     // Fill in the length later
            this.SerializePayload(writer);
            writer.Seek(2, SeekOrigin.Begin); // Go back and write the length
            writer.Write((UInt16)(ms.Length - 4));
            writer.Seek(0, SeekOrigin.End);
            ushort checksum = PacketChecksum.ComputeChecksum(this._checksumType, ms.GetBuffer(), ms.Length);

            writer.Write(checksum);
            writer.Write((byte)0x17);

            byte[] ret = new byte[ms.Length];
            Array.Copy(ms.GetBuffer(), ret, ms.Length);
            return(ret);
        }