Ejemplo n.º 1
0
        public void Update()
        {
            if (this.IsDispose)
            {
                return;
            }

            uint timeNow = this.GetService().TimeNow;

            // 如果还没连接上,发送连接请求
            if (!this.isConnected)
            {
                // 10秒没连接上则报错
                if (timeNow - this.createTime > 10 * 1000)
                {
                    this.OnError(ErrorCode.ERR_KcpCantConnect);
                    kService?.Remove(this.Id);
                    return;
                }

                if (timeNow - this.lastRecvTime < 500)
                {
                    return;
                }

                switch (ChannelType)
                {
                case ChannelType.Accept:
                    this.Accept();
                    break;

                case ChannelType.Connect:
                    this.Connect();
                    break;
                }

                return;
            }

            try
            {
                Kcp.KcpUpdate(this.kcp, timeNow);
            }
            catch (Exception e)
            {
                Log.Error(e);
                this.OnError(ErrorCode.ERR_SocketError);
                kService.Remove(this.Id);
                return;
            }


            if (this.kcp != IntPtr.Zero)
            {
                uint nextUpdateTime = Kcp.KcpCheck(this.kcp, timeNow);
                this.GetService().AddToUpdateNextTime(nextUpdateTime, this.Id);
            }
        }
Ejemplo n.º 2
0
 public void Disconnect(int error)
 {
     kService.Remove(this.Id);
     this.OnError(error);
 }