示例#1
0
        public void HandleRecv(byte[] date, int offset, int length)
        {
            if (this.IsDisposed)
            {
                return;
            }

            // 收到了kcp消息则将自己从连接状态移除
            if (!this.isRecvFirstKcpMessage)
            {
                this.GetService().RemoveFromWaitConnectChannels(this.RemoteConn);
                this.isRecvFirstKcpMessage = true;
            }

            Kcp.KcpInput(this.kcp, date, offset, length);
            this.GetService().AddToUpdateNextTime(0, this.Id);

            while (true)
            {
                if (this.IsDisposed)
                {
                    return;
                }
                int n = Kcp.KcpPeeksize(this.kcp);
                if (n < 0)
                {
                    return;
                }
                if (n == 0)
                {
                    this.OnError((int)SocketError.NetworkReset);
                    return;
                }

                byte[] buffer = this.packet.Bytes;
                this.packet.Stream.SetLength(n);
                this.packet.Stream.Seek(0, SeekOrigin.Begin);
                int count = Kcp.KcpRecv(this.kcp, buffer, ushort.MaxValue);
                if (n != count)
                {
                    return;
                }
                if (count <= 0)
                {
                    return;
                }

                this.lastRecvTime = this.GetService().TimeNow;

                this.OnRead(packet);
            }
        }
        //接收处理
        public void HandleRecv(byte[] date, int offset, int length)
        {
            if (this.IsDisposed)
            {
                return;
            }

            this.isConnected = true;

            Kcp.KcpInput(this.kcp, date, offset, length);
            this.GetService().AddToUpdateNextTime(0, this.Id);

            while (true)
            {
                if (this.IsDisposed)
                {
                    return;
                }
                //取出第一位
                int n = Kcp.KcpPeeksize(this.kcp);
                if (n < 0)
                {
                    return;
                }
                if (n == 0)
                {
                    this.OnError((int)SocketError.NetworkReset);
                    return;
                }

                byte[] buffer = this.memoryStream.GetBuffer();
                this.memoryStream.SetLength(n);
                this.memoryStream.Seek(0, SeekOrigin.Begin);
                int count = Kcp.KcpRecv(this.kcp, buffer, ushort.MaxValue);
                if (n != count)
                {
                    return;
                }
                if (count <= 0)
                {
                    return;
                }

                this.lastRecvTime = this.GetService().TimeNow;
                //从内存流读取 实际就是执行读取的回调而已
                this.OnRead(this.memoryStream);
            }
        }