Exemple #1
0
        /// <summary>
        /// Convert a message to a packet using packet length and specific header
        /// </summary>
        /// <param name="messageData">The message data to convert.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of bytes to convert.</param>
        /// <param name="sendBuffer">The send buffer which is associated with each connection. It is used to avoid allocating memory every time.</param>
        /// <returns>The packed byte array segment with length and header tag if UseMakePacket property is true; otherwise the input message data with doing nothing.</returns>
        public ArraySegment <byte> MakePacket(byte[] messageData, int offset, int count, DynamicBufferStream sendBuffer)
        {
            Contract.Assert(messageData != null && count > 0);
            if (!UseMakePacket)
            {
                return(new ArraySegment <byte>(messageData, offset, count));
            }

            int dataLen   = count;
            int packetLen = dataLen;

            if (UseNetworkByteOrder)
            {
                packetLen = IPAddress.HostToNetworkOrder(dataLen);
            }

            sendBuffer.SetLength(8 + dataLen);
            //write header
            Buffer.BlockCopy(BitConverter.GetBytes(_headerTag), 0, sendBuffer.Buffer, 0, 4);
            //write message length
            Buffer.BlockCopy(BitConverter.GetBytes(packetLen), 0, sendBuffer.Buffer, 4, 4);
            if (dataLen > 0)
            {
                Buffer.BlockCopy(messageData, offset, sendBuffer.Buffer, 8, dataLen);
            }
            return(new ArraySegment <byte>(sendBuffer.Buffer, 0, (int)sendBuffer.Length));
        }
Exemple #2
0
        /// <summary>
        /// Convert a message to a packet using end mark.
        /// </summary>
        /// <param name="messageData">The message data to convert.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of bytes to convert.</param>
        /// <param name="sendBuffer">The send buffer which is associated with each connection. It is used to avoid allocating memory every time.</param>
        /// <returns>The packed byte array segment with end mark if UseMakePacket property is true; otherwise the input message data with doing nothing.</returns>
        public ArraySegment <byte> MakePacket(byte[] messageData, int offset, int count, DynamicBufferStream sendBuffer)
        {
            Contract.Requires(messageData != null && messageData.Length > 0);
            if (!UseMakePacket)
            {
                return(new ArraySegment <byte>(messageData, offset, count));
            }

            sendBuffer.SetLength(count + _endMark.Length);
            //write data
            Buffer.BlockCopy(messageData, offset, sendBuffer.Buffer, 0, count);
            //write end mark
            Buffer.BlockCopy(_endMark, 0, sendBuffer.Buffer, count, _endMark.Length);
            return(new ArraySegment <byte>(sendBuffer.Buffer, 0, (int)sendBuffer.Length));
        }