/// <summary>
        /// Sets the <see cref="CollatedPacketMessage.UncompressedBytes"/> value.
        /// </summary>
        /// <param name="size">The number of uncompressed in the payload.</param>
        protected void SetUncompressedBytesSize(uint size)
        {
            // Write to the uncompressed size bytes.
            int offset = PacketHeader.Size + CollatedPacketMessage.UncompressedBytesOffset;

            WriteHeaderData(offset, BitConverter.GetBytes(Endian.ToNetwork(size)));
        }
        /// <summary>
        /// Sets the <see cref="PacketHeader.PayloadSize"/> value.
        /// </summary>
        /// <param name="size">The packet payload size (compressed).</param>
        protected void SetPayloadSize(ushort size)
        {
            // Write to the payload size bytes.
            int offset = PacketHeader.PayloadSizeOffset;

            WriteHeaderData(offset, BitConverter.GetBytes(Endian.ToNetwork(size)));
        }
Beispiel #3
0
 /// <summary>
 /// Write data into the packet.
 /// </summary>
 /// <param name="bytes">Data to write. Bytes are converted to network Endian (Big Endian)</param>
 /// <param name="toNetworkEndian">When <code>true</code>, <paramref name="bytes"/> are converted
 /// to network Endian (Big Endian), otherwise <paramref name="bytes"/> are written as is.</param>
 /// <param name="offset">Offset from the start of <paramref name="bytes"/> to start writing from.</param>
 /// <param name="length">The number of bytes to write. Zero for all bytes from <paramref name="offset"/>
 /// to the end of <paramref name="bytes"/>.</param>
 /// <remarks>
 /// The buffer size is increased if required.
 /// </remarks>
 public void WriteBytes(byte[] bytes, bool toNetworkEndian, int offset = 0, int length = 0)
 {
     EnsureBufferCapacity(_cursor + _currentByteCount + bytes.Length);
     length = Math.Max(0, (length > 0) ? length : bytes.Length - offset);
     Array.Copy((toNetworkEndian) ? Endian.ToNetwork(bytes) : bytes, offset, _internalBuffer, _cursor + _currentByteCount, length);
     _currentByteCount += bytes.Length;
 }
Beispiel #4
0
        /// <summary>
        /// Finalise an output packet.
        /// </summary>
        /// <param name="addCrc">True to calculate and add the CRC.</param>
        /// <remarks>
        /// Finalises an output packet by updating the packet size, calculating the
        /// CRC and writing the CRC to the buffer. The buffer is ready for <see cref="ExportTo(BinaryWriter)"/>
        /// </remarks>
        public bool FinalisePacket(bool addCrc = true)
        {
            if (!ValidHeader)
            {
                return(false);
            }

            // Calculate the packet CRC.
            // Fix up the payload size.
            _header.PayloadSize = (ushort)(Math.Max(0, _currentByteCount - PacketHeader.Size));
            if (_header.PayloadSize != _currentByteCount - PacketHeader.Size)
            {
                // Payload is too large.
                return(false);
            }
            byte[] sizeBytes = BitConverter.GetBytes(Endian.ToNetwork(_header.PayloadSize));
            for (int i = 0; i < sizeBytes.Length; ++i)
            {
                _internalBuffer[i + PacketHeader.PayloadSizeOffset] = sizeBytes[i];
            }

            // Calculate the CRC.
            if (addCrc)
            {
                ushort crc = Crc16.Crc.Calculate(_internalBuffer, (uint)_currentByteCount);
                // Don't do Endian swap here. WriteBytes does it.
                WriteBytes(BitConverter.GetBytes(crc), true);
            }
            else
            {
                // Add the NoCrc flag.
                _internalBuffer[PacketHeader.FlagsOffset] |= (byte)PacketFlag.NoCrc;
            }
            Status = PacketBufferStatus.Complete;
            return(true);
        }
Beispiel #5
0
 /// <summary>
 /// Write a 32-bit integer value.
 /// </summary>
 /// <param name="value">The value to write.</param>
 public override void Write(uint value)
 {
     base.Write(Endian.ToNetwork(value));
 }