Exemplo n.º 1
0
        DataBlock WrapSendData(Byte[] data, int length = 0)
        {
            UInt32 len = (UInt32)(length > 0 ? length : data.Length);
            UInt16 cmd = TcpContentHead.COMMON_CMD;
            UInt32 seq = nextSeq(_sendSeq);

            _sendSeq = seq;
            if (!BitConverter.IsLittleEndian)
            {
                len = len.BigEndianValue();
                cmd = cmd.BigEndianValue();
                seq = seq.BigEndianValue();
            }

            // fk
            _sendFrameHead[0] = TcpContentHead.STATIC_FRAME_KEY;

            // len
            _sendFrameHead[1] = (Byte)((len >> 24) & 0xFF);
            _sendFrameHead[2] = (Byte)((len >> 16) & 0xFF);
            _sendFrameHead[3] = (Byte)((len >> 8) & 0xFF);
            _sendFrameHead[4] = (Byte)(len & 0xFF);

            // cmd
            _sendFrameHead[5] = (Byte)((cmd >> 8) & 0xFF);
            _sendFrameHead[6] = (Byte)(cmd & 0xFF);

            // seq
            _sendFrameHead[7]  = (Byte)((seq >> 24) & 0xFF);
            _sendFrameHead[8]  = (Byte)((seq >> 16) & 0xFF);
            _sendFrameHead[9]  = (Byte)((seq >> 8) & 0xFF);
            _sendFrameHead[10] = (Byte)(seq & 0xFF);

            int arrLength = TcpContentHead.HEAD_SIZE + (int)len;

            Byte[] newRet = BlockPool.Instance.Alloc(arrLength);
            for (int i = 0; i < arrLength; i++)
            {
                if (i < TcpContentHead.HEAD_SIZE)
                {
                    newRet[i] = _sendFrameHead[i];
                }
                else
                {
                    newRet[i] = data[i - TcpContentHead.HEAD_SIZE];
                }
            }
            DataBlock db = new DataBlock();

            db.data   = newRet;
            db.length = arrLength;
            return(db);
        }