Beispiel #1
0
        /// <summary>
        /// Serializes the packet and returns the packed information
        /// to the caller.
        /// </summary>
        /// <exception cref="System.ArgumentException">Thrown when the packet was corrupt</exception>
        /// <returns>The serialized packet</returns>
        public byte[] Serialize()
        {
            // First check if this packet is corrupt
            if (Corrupted)
            {
                throw new ArgumentException("StreamPacket: Trying to serialize a corrupt packet.");
            }

            // Calculate the necessary packet size
            byte[] buffer = new byte[HEADER_BIT_SIZE / 8 + Data.Length];

            // ----------------------------------------------------
            // 1. Serialize the version
            // ----------------------------------------------------
            buffer[0] = ProtocolVersion;

            // ----------------------------------------------------
            // 2. Serialize the flags
            // ----------------------------------------------------
            buffer[1] = _flags;

            // ----------------------------------------------------
            // 3. Serialize the checksum
            // ----------------------------------------------------
            Array.Copy(BitConverter.GetBytes(_checksum), 0, buffer, 2, 2);

            // ----------------------------------------------------
            // 4. Serialize the type
            // ----------------------------------------------------
            Array.Copy(BitConverter.GetBytes((int)Type), 0, buffer, 4, 4);

            // ----------------------------------------------------
            // 5. Serialize the source address
            // ----------------------------------------------------
            Array.Copy(AddrStructureConvert.GetBytes(Source), 0, buffer, 8, 32);

            // ----------------------------------------------------
            // 6. Serialize the destination address
            // ----------------------------------------------------
            Array.Copy(AddrStructureConvert.GetBytes(Destination), 0, buffer, 40, 32);

            // ----------------------------------------------------
            // 7. Serialize the size of the payload
            // ----------------------------------------------------
            Array.Copy(BitConverter.GetBytes(Data.Length), 0, buffer, 72, 4);

            // ----------------------------------------------------
            // 8. Serialize the data
            // ----------------------------------------------------
            Array.Copy(Data, 0, buffer, 76, Data.Length);

            return(buffer);
        }
Beispiel #2
0
        /// <summary>
        /// Deserialize a new message from a given stream. Rethrows an IOException or
        /// ArgumentException to the caller.
        /// </summary>
        /// <exception cref="System.ArgumentException">Thrown when the argument was invalid</exception>
        /// <exception cref="System.IO.IOException">Thrown when the I/O failed</exception>
        /// <param name="stream"></param>
        public void Deserialize(Stream stream)
        {
            try
            {
                byte[] buffer = new byte[32];

                // ----------------------------------------------------
                // 1. Read the version
                // ----------------------------------------------------
                int bytes = stream.Read(buffer, 0, 1);
                if (bytes != 1)
                {
                    return;
                }

                ProtocolVersion = buffer[0];

                // ----------------------------------------------------
                // 2. Read the flags
                // ----------------------------------------------------
                bytes = stream.Read(buffer, 0, 1);
                if (bytes != 1)
                {
                    return;
                }

                _flags = buffer[0];

                // ----------------------------------------------------
                // 3. Read the checksum
                // ----------------------------------------------------
                bytes = stream.Read(buffer, 0, 2);
                if (bytes != 2)
                {
                    return;
                }

                _checksum = BitConverter.ToUInt16(buffer, 0);

                // ----------------------------------------------------
                // 4. Read the type
                // ----------------------------------------------------
                bytes = stream.Read(buffer, 0, 4);
                if (bytes != 4)
                {
                    return;
                }

                Type = (PacketType)Enum.ToObject(typeof(PacketType), BitConverter.ToUInt32(buffer, 0));

                bool blnIsExist = Enum.IsDefined(typeof(PacketType), Type);

                if (!blnIsExist)
                {
                    return;
                }

                // ----------------------------------------------------
                // 5. Read the source address
                // ----------------------------------------------------
                bytes = stream.Read(buffer, 0, 32);
                if (bytes != 32)
                {
                    return;
                }

                Source = AddrStructureConvert.GetEndPoint(buffer);

                // ----------------------------------------------------
                // 6. Read the destination address
                // ----------------------------------------------------
                bytes = stream.Read(buffer, 0, 32);
                if (bytes != 32)
                {
                    return;
                }

                Destination = AddrStructureConvert.GetEndPoint(buffer);

                // ----------------------------------------------------
                // 7. Read the size of the payload
                // ----------------------------------------------------
                bytes = stream.Read(buffer, 0, 4);
                if (bytes != 4)
                {
                    return;
                }

                int size = BitConverter.ToInt32(buffer, 0);

                // ----------------------------------------------------
                // 8. Read the data
                // ----------------------------------------------------
                _data = new byte[size];
                if (size != 0)
                {
                    bytes = stream.Read(_data, 0, size);
                    if (bytes != size)
                    {
                        return;
                    }
                }
            }
            catch
            {
                throw;  // Rethrow any exception
            }
        }