Ejemplo n.º 1
0
        public void HandleRecv(byte[] date, uint timeNow)
        {
            this.kcp.Input(date);
            // 加入update队列
            this.GetService().AddToUpdate(this.Id);

            while (true)
            {
                int n = kcp.PeekSize();
                if (n == 0)
                {
                    this.OnError((int)SocketError.NetworkReset);
                    return;
                }
                int count = this.kcp.Recv(this.cacheBytes);
                if (count <= 0)
                {
                    return;
                }

                lastRecvTime = timeNow;

                // 收到的数据放入缓冲区
                byte[] sizeBuffer = BitConverter.GetBytes((ushort)count);
                this.recvBuffer.Write(sizeBuffer, 0, sizeBuffer.Length);
                this.recvBuffer.Write(cacheBytes, 0, count);

                if (this.recvTcs == null)
                {
                    continue;
                }

                try
                {
                    bool isOK = this.parser.Parse();
                    if (!isOK)
                    {
                        continue;
                    }

                    Packet packet = this.parser.GetPacket();
                    var    tcs    = this.recvTcs;
                    this.recvTcs = null;
                    tcs.SetResult(packet);
                }
                catch (Exception e)
                {
                    this.OnError(ErrorCode.ERR_PacketParserError);

                    var tcs = this.recvTcs;
                    this.recvTcs = null;
                    tcs.SetException(e);
                }
            }
        }
Ejemplo n.º 2
0
        public void HandleRecv(byte[] date, uint timeNow)
        {
            this.kcp.Input(date);
            // 加入update队列
            this.GetService().AddToUpdate(this.Id);

            while (true)
            {
                int n = kcp.PeekSize();
                if (n == 0)
                {
                    this.OnError((int)SocketError.NetworkReset);
                    return;
                }
                int count = this.kcp.Recv(this.cacheBytes);
                if (count <= 0)
                {
                    return;
                }

                lastRecvTime = timeNow;

                // 收到的数据放入缓冲区
                byte[] sizeBuffer = BitConverter.GetBytes((ushort)count);
                this.recvBuffer.Write(sizeBuffer, 0, sizeBuffer.Length);
                this.recvBuffer.Write(cacheBytes, 0, count);

                while (true)
                {
                    if (!this.parser.Parse())
                    {
                        break;
                    }

                    Packet packet = this.parser.GetPacket();
                    this.OnRead(packet);
                }
            }
        }