Exemplo n.º 1
0
        public static void UnWrap(ref byte[] recvBuffer, ref int nRecvLen)
        {
            int  offset         = 0;
            bool bCompress      = false;
            bool bEncrypt       = false;
            int  nMessageLen    = 0;
            int  datasize       = nRecvLen;
            int  nPacketHeadLen = PacketConstDefine.PacketHeadLen;

            //尝试读取包头
            while (offset + nPacketHeadLen < datasize)
            {
                if (nMessageLen == 0)
                {
                    //包头还没有读取过
                    //第一个字节 是否压缩
                    //第二个 + 第三个 代表包体长度
                    bCompress   = (recvBuffer[offset] & 0x01) > 0;
                    bEncrypt    = (recvBuffer[offset] & 0x02) > 0;
                    nMessageLen = CUtility.BytesToUshort(recvBuffer, offset + 1);
                }

                if (nMessageLen <= 0)
                {
                    //GDebug.LogError("<color=red> 消息长度小于等于0 </color>");
                    offset     += nPacketHeadLen;
                    nMessageLen = 0;
                    continue;
                }
                else if (offset + nMessageLen + nPacketHeadLen > datasize)
                {
                    //byte offset0 = recvBuffer[offset];
                    //UInt16 offset12 = CUtility.BytesToUshort(recvBuffer, offset+1);
                    //byte offset3 = recvBuffer[offset + 3];    //大消息号
                    //byte offset4 = recvBuffer[offset + 4];    //小消息号
                    //GDebug.LogError("<color=red> 被拆分的包体 offset0 = " + offset0 + " offset12 = " + offset12 + " offset3 = " + offset3 + " offset4 = " + offset4 + " iscompress = " + m_bCompress + " m_nMessageLen = " + m_nMessageLen + " offset  " + offset + "  datasize " + datasize + "</color>");
                    break;
                }

                //尝试读取包体
                if ((offset + nMessageLen + nPacketHeadLen) <= datasize)
                {
                    //消息包头已经读取,并且剩下的数据足够读包体
                    //先解密再解压
                    byte[] bytes = CUtility.BytesToBytes(recvBuffer, offset + nPacketHeadLen, nMessageLen);
                    byte[] desdata;
                    if (bEncrypt)
                    {
                        Init();
                        desdata = dec.TransformFinalBlock(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        desdata = bytes;
                    }

                    byte[] uncompressData;
                    // 判断是否需要解压
                    if (bCompress)
                    {
                        uncompressData = CUtility.deCompressBytes(desdata);
                    }
                    else
                    {
                        uncompressData = desdata;
                    }

                    NetWorkConnection.Instance.Push(uncompressData, (int)uncompressData.Length);

                    offset     += nMessageLen + nPacketHeadLen;
                    nMessageLen = 0;
                }
            }

            nRecvLen = datasize - offset;
            if (nRecvLen > 0)
            {
                Array.Copy(recvBuffer, offset, recvBuffer, 0, nRecvLen);
            }
        }