Exemple #1
0
        /// <summary>
        /// 从缓存数据中读取包数据
        /// 如果缓存数据中包含完整包数据,则从该缓存数据中取出,并读入到bodybuffer中
        /// </summary>
        public bool UF_Read(CBytesBuffer rawBuffer)
        {
            m_TmpBufferToRead.UF_clear();
            m_TmpBufferToRead.UF_write(rawBuffer);
            //			CBytesBuffer tmpbuff = new CBytesBuffer(rawBuffer);
            CBytesBuffer tmpbuff = m_TmpBufferToRead;

            int packetsize = 0;

            if (tmpbuff.UF_getSize() < HEAD_SIZE)
            {
                //不是完整的协议数据包,等待下次读取
                Debugger.UF_Warn(string.Format("NetPacket Buffer Size[{0}] not Long enought to read", tmpbuff.UF_getSize()));
                return(false);
            }

            //校验码比较
            uint pMagic = CBytesConvert.UF_readuint32(tmpbuff);

            if (MAGIC != pMagic)
            {
                ///校验码不通过,丢弃该包缓存的全部数据
                rawBuffer.UF_popBytes(rawBuffer.Length);

                Debugger.UF_Error(string.Format("discard package: magic<{0}> | MAGIC<{1}>,RawBuffer Clear", MAGIC, pMagic));

                return(false);
            }


            //读出包头
            this.id      = (int)CBytesConvert.UF_readuint32(tmpbuff);
            this.retCode = (short)CBytesConvert.UF_readuint16(tmpbuff);
            this.corCode = (int)CBytesConvert.UF_readuint32(tmpbuff);
            this.size    = (int)CBytesConvert.UF_readuint32(tmpbuff);

            packetsize += HEAD_SIZE;

            //包体不为0,读出包体
            if (this.size > 0)
            {
                //buffer 比读出的size长
                if (this.size > tmpbuff.UF_getSize())
                {
                    //不是完整的协议数据包,等待下次读取
                    return(false);
                }
                //写入到body中
                this.m_BodyBuffer.UF_write(tmpbuff.Buffer, (int)this.size);
                packetsize += this.size;
            }

            //清空已经读取的RawBuffer
            rawBuffer.UF_popBytes(packetsize);

            return(true);
        }