Example #1
0
        /// <summary>
        /// 由Server.Update来调用,经过KCP处理后,如果确认是客户端发来的信息,则调用m_KCPServer.OnRecvData
        /// </summary>
        /// GG:修改代码采用回调,不直接调用 m_KCPServer
        internal void process_recv_queue(SocketAsyncEventArgs e)
        {
#if DEV
            IRQLog.AppLog.Log(this.m_netIndex.ToString() + ",接收1");
#endif
            m_Kcp.Input(e.Buffer, e.Offset, e.BytesTransferred);

            m_NeedUpdateFlag = true;

            for (var size = m_Kcp.PeekSize(); size > 0; size = m_Kcp.PeekSize())
            {
                byte[] buffer;
                buffer = (UdpLibConfig.UseBytePool ? m_KCPServer.BytePool.Rent(size) : new byte[size]);
                try
                {
                    if (m_Kcp.Recv(buffer) > 0)
                    {
                        m_LastRecvTimestamp = m_KCPServer.m_watch.Elapsed;

                        uint key = 0;
                        KCPLib.ikcp_decode32u(buffer, 0, ref key);
                        if (m_KCPServer.IsClientKeyCorrect(this.m_netIndex, (int)key) == false)
                        {
#if DEBUG
                            Console.WriteLine("index:{0} key 不对", this.m_netIndex);
#endif
                            m_KCPServer.BytePool.Return(buffer, true);
                            DisposeReason = ClientSessionDisposeReason.IndexKeyError;
                            //key不对
                            Dispose();
                            return;
                        }
#if DEV
                        IRQLog.AppLog.Log(this.m_netIndex.ToString() + ",接收2");
#endif
                        m_KCPServer.OnRecvData(this, buffer, 0, size);
                    }
                }
                finally
                {
                    if (UdpLibConfig.UseBytePool)
                    {
                        m_KCPServer.BytePool.Return(buffer, true);
                    }
                }
            }
        }
Example #2
0
        void process_recv_queue()
        {
            m_RecvQueue.Switch();

            while (!m_RecvQueue.Empty())
            {
                var buf = m_RecvQueue.Pop();
                if (this.Status == ClientSessionStatus.InConnect)
                {
                    //服务端将返回和握手请求相同的响应数据
                    uint _index = 0, _key = 0;
                    if (HandshakeUtility.IsHandshakeDataRight(buf, 0, buf.Length, out _index, out _key))
                    {
                        if (_index == m_NetIndex && _key == m_Key)
                        {
#if DEBUG
                            Console.WriteLine("连接握手成功");
#endif
                            this.Status = ClientSessionStatus.Connected;
                            var e = Event;
                            if (e != null)
                            {
                                e(UdpClientEvents.Connected, null);
                            }
                            continue;
                        }
                    }
                }


                m_Kcp.Input(buf, 0, buf.Length);
                m_NeedUpdateFlag = true;

                for (var size = m_Kcp.PeekSize(); size > 0; size = m_Kcp.PeekSize())
                {
                    var buffer = new byte[size];
                    if (m_Kcp.Recv(buffer) > 0)
                    {
                        var e = Event;
                        if (e != null)
                        {
                            e(UdpClientEvents.Recv, buffer);
                        }
                    }
                }
            }
        }