Beispiel #1
0
        /// <summary>
        /// 更新
        /// </summary>
        public void Update()
        {
            //input
            lock (LOCK)
            {
                while (this.received.Count > 0)
                {
                    ByteBuf bb = this.received.First.Value;
                    kcp.Input(bb);
                    this.received.RemoveFirst();
                }
            }
            //receive
            int len;

            while ((len = kcp.PeekSize()) > 0)
            {
                ByteBuf bb = new ByteBuf(len);
                int     n  = kcp.Receive(bb);
                if (n > 0)
                {
                    this.lastTime = DateTime.Now;
                    this.HandleReceive(bb);
                }
            }
            //send
            lock (this.SEND_LOCK)
            {
                while (this.sendList.Count > 0)
                {
                    ByteBuf item = this.sendList.First.Value;
                    this.kcp.Send(item);
                    this.sendList.RemoveFirst();
                }
            }
            //update kcp status
            int cur = (int)DateTime.Now.Ticks;

            if (this.needUpdate || cur >= kcp.GetNextUpdate())
            {
                kcp.Update(cur);
                kcp.SetNextUpdate(kcp.Check(cur));
                this.needUpdate = false;
            }
            //check timeout
            if (this.timeout > 0 && lastTime != DateTime.MinValue)
            {
                double del = (DateTime.Now - this.lastTime).TotalMilliseconds;
                if (del > this.timeout)
                {
                    this.HandleTimeout();
                }
            }
        }
Beispiel #2
0
        /**
         * update one kcp
         *
         * @param addr
         * @param kcp
         */
        public void Update()
        {
            //input
            lock (LOCK)
            {
                while (this.received.Count > 0)
                {
                    byte[] dp = this.received.First.Value;
                    int    r  = kcp.Input(new ByteBuf(dp));
                    this.received.RemoveFirst();
                    if (r < 0)//error
                    {
                        this.HandleException(new Exception("kcp输入状态异常:" + r));
                        return;
                    }
                }
            }
            //receive
            int len;

            while ((len = kcp.PeekSize()) > 0)
            {
                ByteBuf bb = new ByteBuf(len);
                int     n  = kcp.Receive(bb);
                if (n > 0)
                {
                    this.lastTime = DateTime.Now;
                    this.HandleReceive(bb);
                }
            }
            //update kcp status
            int cur = (int)DateTime.Now.Ticks;

            if (this.needUpdate || cur >= kcp.GetNextUpdate())
            {
                kcp.Update(cur);
                kcp.SetNextUpdate(kcp.Check(cur));
                this.needUpdate = false;
            }
            //check timeout
            if (this.timeout > 0 && lastTime != DateTime.MinValue)
            {
                double del = (DateTime.Now - this.lastTime).TotalMilliseconds;
                if (del > this.timeout)
                {
                    this.HandleTimeout();
                }
            }
        }