示例#1
0
            protected void ScanPackets()
            {
                bool packetFound = false;

                do
                {
                    packetFound = false;

                    //如果大于包头长度
                    if (m_recBufOffset >= MinPacketSize)
                    {
                        PacketHeader header = new PacketHeader();
                        header.HeaderSign = BitConverter.ToInt16(m_recBuf, 0);
                        header.BodySize   = BitConverter.ToInt32(m_recBuf, sizeof(Int16));

                        int dwPkgLen = (int)(MinPacketSize + header.BodySize);
                        if (m_recBufOffset >= dwPkgLen)
                        {
                            //接收到了一个完整的数据包
                            packetFound     = true;
                            header.RandSeed = (byte)BitConverter.ToChar(m_recBuf, sizeof(Int16) + sizeof(Int32));
                            header.CheckSum = (byte)BitConverter.ToChar(m_recBuf, sizeof(Int16) + sizeof(Int32) + sizeof(byte));
                            header.Compress = BitConverter.ToInt32(m_recBuf, sizeof(Int16) + sizeof(Int32) + sizeof(byte) * 2);
                            header.CMD      = BitConverter.ToInt16(m_recBuf, sizeof(Int16) + sizeof(Int32) * 2 + sizeof(byte) * 2);
                            header.Index    = BitConverter.ToInt16(m_recBuf, sizeof(Int16) * 2 + sizeof(Int32) * 2 + sizeof(byte) * 2);

                            //将数据表扔到接收队列里
                            RecvBuffer msg = null;
                            lock (cacheRecvMsgs)
                            {
                                msg = cacheRecvMsgs.Get();
                            }
                            msg.tick = DateTime.Now.Ticks;
                            msg.cmd  = header.CMD;

                            //解包数据流
                            if (header.BodySize != 0)
                            {
                                PacketUtil.Decrypt(m_recBuf, PacketHeader.Length, header.BodySize);
                                if (header.GetCompressType() == (int)CompressType.LZ4)
                                {
                                    int len = header.GetUnCompressLength();
                                    PacketUtil.uncompress_lz4(m_recBuf, PacketHeader.Length, header.BodySize, msg.data, len);
                                    msg.length = len;
                                }
                                else
                                {
                                    msg.length = header.BodySize;
                                    Utils.CopyTo(m_recBuf, PacketHeader.Length, msg.data, 0, msg.length);
                                }
                            }

                            lock (m_netMgr.recvMsgs)
                            {
                                m_netMgr.recvMsgs.Enqueue(msg);
                            }

                            //接收缓冲区数据往回移
                            int offset = dwPkgLen;
                            if (m_recBufOffset > offset)
                            {
                                for (int i = offset, j = 0; i < m_recBufOffset; i++, j++)
                                {
                                    m_recBuf[j] = m_recBuf[i];
                                }
                                m_recBufOffset -= offset;
                            }
                            else
                            {
                                m_recBufOffset = 0;
                            }
                        }
                    }
                }while (packetFound && !IsTerminateFlagSet());
            }