Ejemplo n.º 1
0
        /// <summary>
        /// 处理接收到的数据
        /// </summary>
        /// <param name="cache"> 接收到的数据</param>
        public void HandleRecv(byte[] cache)
        {
            if (this.disposed)
            {
                return;
            }

            kcp.Input(cache);

            while (true)
            {
                try
                {
                    if (this.disposed)
                    {
                        return;
                    }

                    int size = kcp.PeekSize();

                    if (size < 0)
                    {
                        return;
                    }

                    if (size == 0)
                    {
                        //发生错误,应该断开
                        this.OnError((ErrorCode)SocketError.NetworkReset);
                        this.Dispose();
                        return;
                    }

                    if (!isConnected)
                    {
                        this.isConnected = true;
                    }

                    var buffer = new byte[size];
                    kcp.Recv(buffer);

                    this.lastRecvTime = (uint)SystemTime.ClientNow();

                    this.OnRead(buffer);
                }
                catch (Exception e)
                {
                    Debug.LogError(e);
                }
            }
        }