예제 #1
0
        protected override void Update()
        {
            var now = (DateTime.UtcNow.Ticks - epoch) / 10000;

            base.Update();
            _kcp.Update((uint)now); //TODO: 改成通过check进行schedule的方式可以节省cpu
        }
예제 #2
0
        public void Tick()
        {
            if (!isRunning)
            {
                return;
            }
            DoReceiveInMain();
            uint current = (uint)TimeUtility.GetTotalMillisecondsSince1970();

            if (needKcpUpdateFlag || current >= nextKcpUpdateTime)
            {
                if (kcp != null)
                {
                    kcp.Update(current);
                    nextKcpUpdateTime = kcp.Check(current);
                    needKcpUpdateFlag = false;
                }
            }
            if (isWaitReconnect)
            {
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    Reconnect();
                }
                else
                {
                    Debuger.Log("等待重连,但是网络不可用!");
                }
            }
            CheckTimeOut();
        }
예제 #3
0
 private void Run()
 {
     udpClient = new UdpClient(ip, port);
     state     = "connected";
     stopwatch.Start();
     while (state == "connected")
     {
         try
         {
             SendPendingBuffer();
             UpdateKcpInput();
             uint currMs = (uint)stopwatch.ElapsedMilliseconds;
             kcp.Update(currMs);
             RecvPendingBuffer();
             uint nextTime  = kcp.Check(currMs);
             uint sleepTime = nextTime - currMs;
             //Debug.Log("next time " + (sleepTime/ 1000f));
             if (nextTime > 0)
             {
                 bool ret = autoResetEvent.WaitOne(1000 / 60);
                 //Debug.Log("event ret " + ret);
             }
         }
         catch (Exception e) {
             Debug.Log("Exception " + e);
             End();
             break;
         }
     }
 }
예제 #4
0
        private void Update()
        {
            while (true)
            {
                if (_receiveMeesages.Count > 0)
                {
                    var buf = _receiveMeesages.Dequeue();

                    _kcp.Input(buf);
                    // mNeedUpdateFlag = true;

                    for (var size = _kcp.PeekSize(); size > 0; size = _kcp.PeekSize())
                    {
                        var buffer = new byte[size];
                        if (_kcp.Recv(buffer) > 0)
                        {
                            _reveiveHandler(buffer);
                        }
                    }
                }

                _kcp.Update(iclock());
                Thread.Sleep(10);
            }
        }
예제 #5
0
    public static void Update()
    {
        if (!Connected)
        {
            return;
        }

        if (WillDisconnect)
        {
            Disconnect();
            WillDisconnect = false;

            return;
        }

        UpdateTime += FrameReplay.deltaTime;
        Kcp.Update((uint)Mathf.FloorToInt(UpdateTime * 1000));

        for (var size = Kcp.PeekSize(); size > 0; size = Kcp.PeekSize())
        {
            var buffer = new byte[size];
            int n      = Kcp.Recv(buffer);
            if (n > 0)
            {
                PacketProxy.SetBuffer(buffer);
                PacketProxy.Analysis(n, Packet);
            }
        }
    }
예제 #6
0
        public void Tick()
        {
            if (IsActived)
            {
                DoReceiveInMain();

                uint current = (uint)TimeUtils.GetTotalMillisecondsSince1970();

                if (m_NeedKcpUpdateFlag || current >= m_NextKcpUpdateTime)
                {
                    if (m_Kcp != null)
                    {
                        m_Kcp.Update(current);
                        m_NextKcpUpdateTime = m_Kcp.Check(current);
                        m_NeedKcpUpdateFlag = false;
                    }
                }
            }
            else
            {
                if (m_waitReconnect)
                {
                    TryReconnect();
                }
            }
        }
예제 #7
0
 public void Update()
 {
     current = (UInt32)(Convert.ToInt64(DateTime.UtcNow.Subtract(utc_time).TotalMilliseconds) & 0xffffffff);
     if (_send)
     {
         m_LastSendTimestamp = UnityEngine.Time.time;
         _send = false;
     }
     if (state > 0)
     {
         //UnityEngine.Main.Debug("m_LastSendTimestamp:" + m_LastSendTimestamp+"---"+(UnityEngine.Time.time - m_LastSendTimestamp));
         if (m_LastSendTimestamp > 0 && UnityEngine.Time.time - m_LastSendTimestamp > parent.HeartTime)
         {//超过1秒没发生数据,发送心跳包
             m_LastSendTimestamp = UnityEngine.Time.time;
             parent.SendHeart();
         }
         //if (UnityEngine.Time.time - m_LastRecvTimestamp > 5)
         //{//超过5秒没收到数据,短线
         //    Main.Debug("超过5秒没收到数据,短线");
         //    NetError();
         //}
     }
     //Main.Debug("222222222222222");
     process_recv_queue();
     if (m_Kcp != null)
     {
         if (m_NeedUpdateFlag || current >= m_NextUpdateTime)
         {
             m_Kcp.Update(current);
             m_NextUpdateTime = m_Kcp.Check(current);
             m_NeedUpdateFlag = false;
         }
     }
     DealData();
 }
예제 #8
0
 void update(UInt32 current)
 {
     if (mNeedUpdateFlag || current >= mNextUpdateTime)
     {
         mKcp.Update(current);
         mNextUpdateTime = mKcp.Check(current);
         mNeedUpdateFlag = false;
     }
 }
예제 #9
0
        public void Update()
        {
            uint current = KcpUtils.Clock32();

            switch (status_)
            {
            case Status.Connecting: {
                HandleConnectAck(current);
                if (ConnectTimeout(current))
                {
                    Clean();
                    status_ = Status.Timemout;
                    eventCallback_(0, KcpEvent.KCP_EV_CONNFAILED, null, "Timeout");
                    return;
                }
                if (NeedSendConnectReq(current))
                {
                    lastConnectReqTime_ = current;
                    kcpCommand_.cmd     = KcpCmd.KCP_CMD_CONNECT_REQ;
                    SendKcpCommand(kcpCommand_);
                }
            } break;

            case Status.Connected: {
                ProcessRecvQueue(current);
                if (SessionTimeout(current))
                {
                    uint conv = conv_;
                    Clean();
                    status_ = Status.Timemout;
                    eventCallback_(conv, KcpEvent.KCP_EV_DISCONNECT, null, "Timeout");
                }
                else
                {
                    if (needUpdate_ || current >= nextUpdateTime_)
                    {
                        kcp_.Update(current);
                        nextUpdateTime_ = kcp_.Check(current);
                        needUpdate_     = false;
                    }
                    if (NeedHeartbeat(current))
                    {
                        lastHearbeatTime_ = current;
                        kcpCommand_.cmd   = KcpCmd.KCP_CMD_HEARTBEAT;
                        kcpCommand_.conv  = conv_;
                        SendKcpCommand(kcpCommand_);
                    }
                }
            } break;

            case Status.Disconnecting: {
            }
            break;
            }
        }
예제 #10
0
    void update(UInt32 current)
    {
        process_recv_queue();

        if (mNeedUpdateFlag || current >= mNextUpdateTime)
        {
            mKcp.Update(current);
            mNextUpdateTime = mKcp.Check(current);
            mNeedUpdateFlag = false;
        }
    }
예제 #11
0
        // Update is called once per frame
        void Update()
        {
            uint currentTimeMS = GetClockMS();

            HandleRecvQueue();
            if (needKcpUpdateFlag || currentTimeMS >= nextKcpUpdateTime)
            {
                kcpObject.Update(currentTimeMS);
                nextKcpUpdateTime = kcpObject.Check(currentTimeMS);
                needKcpUpdateFlag = false;
            }
        }
예제 #12
0
        public void Tick(uint currentTimeMS)
        {
            DoReceiveInMain();
            uint current = currentTimeMS;

            if (needKcpUpdateFlag || current >= nextKcpUpdateTime)
            {
                kcp.Update(current);
                nextKcpUpdateTime = kcp.Check(current);
                needKcpUpdateFlag = false;
            }
        }
예제 #13
0
    public override void Update()
    {
        HandleCmdQueue();

        if (_connected)
        {
            HandleRecvQueue();

            uint current = GetClockMS();
            _kcp.Update(current);
        }
    }
예제 #14
0
 private void UpdateKCP(UInt32 current)
 {
     if (mKcp == null)
     {
         return;
     }
     if (mNeedUpdateFlag || current >= mNextUpdateTime)
     {
         mKcp.Update(current);
         mNextUpdateTime = mKcp.Check(current);
         mNeedUpdateFlag = false;
     }
 }
예제 #15
0
 void update(UInt32 current)
 {
     if (m_Status != ClientSessionStatus.Connected)
     {
         return;
     }
     if (m_NeedUpdateFlag || current >= m_NextUpdateTime)
     {
         m_Kcp.Update(current);
         m_NextUpdateTime = m_Kcp.Check(current);
         m_NeedUpdateFlag = false;
     }
 }
예제 #16
0
    public void Update()
    {
        if (mKCP == null)
        {
            return;
        }

        if (0 == mNextUpdateTime || mKCP.CurrentMS >= mNextUpdateTime)
        {
            mKCP.Update();
            mNextUpdateTime = mKCP.Check();
        }
    }
예제 #17
0
파일: KCPSession.cs 프로젝트: zj831007/SGF
        public void Tick(int currentTimeMS)
        {
            DoReceiveInMain();

            uint current = (uint)currentTimeMS;

            if (m_NeedKcpUpdateFlag || current >= m_NextKcpUpdateTime)
            {
                m_Kcp.Update(current);
                m_NextKcpUpdateTime = m_Kcp.Check(current);
                m_NeedKcpUpdateFlag = false;
            }
        }
예제 #18
0
    void update(UInt32 current)
    {
        if (mInConnectStage)
        {
            if (connect_timeout(current))
            {
                evHandler(cliEvent.ConnectFailed, null, "Timeout");
                mInConnectStage = false;
                return;
            }

            if (need_send_connect_packet(current))
            {
                mLastSendConnectTime = current;
                mUdpClient.Send(new byte[4] {
                    0, 0, 0, 0
                }, 4);
            }

            process_connect_packet();

            return;
        }

        if (mConnectSucceed)
        {
            // 处理数据队列
            process_recv_queue();

            // 更新kcp
            if (mNeedUpdateFlag || current >= mNextUpdateTime)
            {
                mKcp.Update(current);
                mNextUpdateTime = mKcp.Check(current);
                mNeedUpdateFlag = false;
            }
        }

        // 关闭阶段
        if (mInCloseStage)
        {
            // 如果有待发送的数据等待数据发完之后在发送
            // 如果超时仍然没有发完那么直接关闭
            if (mKcp == null || mKcp.WaitSnd() <= 0 || close_timeout(current))
            {
                evHandler(cliEvent.Disconnect, null, mCloseReason);
                mUdpClient.Close();
                mInCloseStage = false;
            }
        }
    }
예제 #19
0
 public void Tick()
 {
     if (Connected)
     {
         DoReceiveInMain();
         uint current = (uint)TimeUtility.GetTotalMillisecondsSince1970();
         if (needKcpUpdateFlag || current >= nextKcpUpdateTime)
         {
             kcp.Update(current);
             nextKcpUpdateTime = kcp.Check(current);
             needKcpUpdateFlag = false;
         }
     }
 }
예제 #20
0
        public void Update(uint current)
        {
            if (kcp == null)
            {
                return;
            }

            if (!ProcessReceiveQueue() && current < nextUpdateTime)
            {
                return;
            }

            kcp.Update(current);
            nextUpdateTime = kcp.Check(current);
        }
예제 #21
0
    void update(UInt32 current)
    {
        switch (this.Status)
        {
        case ClientSessionStatus.InConnect:
            ProcessHandshake();
            break;

        case ClientSessionStatus.ConnectFail:
            return;

        case ClientSessionStatus.Connected:
            if (m_LastSendTimestamp > 0 && UnityEngine.Time.time - m_LastSendTimestamp > 1)
            {    //超过1秒没发生数据,发送心跳包
                m_LastSendTimestamp = UnityEngine.Time.time;
                Send(200);
                //UnityEngine.Debug.Log("发送心跳包");
            }
            if (UnityEngine.Time.time - m_LastRecvTimestamp > 5)
            {    //超过5秒没收到数据,短线
                this.Status = ClientSessionStatus.ConnectFail;
                UnityEngine.Debug.Log("超过5秒没收到数据,短线");
                Close();
                if (Event != null)
                {
                    Event(UdpClientEvents.Close, null);
                }
            }
            break;

        default:
            break;
        }

        process_recv_queue();

        if (m_Kcp != null)
        {
            if (m_NeedUpdateFlag || current >= m_NextUpdateTime)
            {
                m_Kcp.Update(current);
                m_NextUpdateTime = m_Kcp.Check(current);
                m_NeedUpdateFlag = false;
            }
        }
    }
예제 #22
0
 //KCP-->应用层
 private void  Update()
 {
     //Log.Net("KCPUpdate");
     kcp.Update(Time.deltaTime);
     while (true)
     {
         var pack = kcp.Recv();
         if (pack != null)
         {
             msgReader.process(pack, (uint)pack.Length, null);
         }
         else
         {
             break;
         }
     }
 }
예제 #23
0
        public void Tick()
        {
            if (Connected)
            {
                DoReceiveInMain();

                uint current = (uint)TimeUtils.GetTotalMillisecondsSince1970();

                if (m_NeedKcpUpdateFlag || current >= m_NextKcpUpdateTime)
                {
                    m_Kcp.Update(current);
                    m_NextKcpUpdateTime = m_Kcp.Check(current);
                    m_NeedKcpUpdateFlag = false;
                }
            }
            else
            {
                //处理重连
            }
        }
예제 #24
0
        /// <summary>
        /// 执行更新
        /// </summary>
        /// <param name="current">时钟</param>
        private void Update(uint current)
        {
            if (kcp == null || !connected)
            {
                return;
            }

            if (SystemTime.Clock() - timer > outTime)
            {
                connected = false;
                OnConnectState?.Invoke(conv, connected);
            }

            if (current < nextUpdateTime)
            {
                return;
            }

            kcp.Update(current);
            nextUpdateTime = kcp.Check(current);
        }
예제 #25
0
        public virtual void Update()
        {
            var current = Helper.iclock();

            switch (status)
            {
            case ClientStatus.InConnect:
                if (IsConnectTimeout(current))
                {
                    status = ClientStatus.Disconnected;
                    Console.WriteLine("Connect Timeout");
                    return;
                }
                if (IsRehandshake(current))
                {
                    mLastSendConnectTime = current;
                    SendHandshake();
                }
                ProcessConnectQueue();
                break;

            case ClientStatus.Connected:
                ProcessRecvQueue();
                if (mKcp == null)
                {
                    return;
                }
                if (mNeedUpdateFlag || current >= mNextUpdateTime)
                {
                    mKcp.Update(current);
                    mNextUpdateTime = mKcp.Check(current);
                    mNeedUpdateFlag = false;
                }
                break;

            case ClientStatus.Disconnected:
                break;
            }
        }
예제 #26
0
파일: FSPClient.cs 프로젝트: zj831007/SGF
        public void Tick()
        {
            if (!m_IsRunning)
            {
                return;
            }

            DoReceiveInMain();

            uint current = (uint)TimeUtils.GetTotalMillisecondsSince1970();

            if (m_NeedKcpUpdateFlag || current >= m_NextKcpUpdateTime)
            {
                if (m_Kcp != null)
                {
                    m_Kcp.Update(current);
                    m_NextKcpUpdateTime = m_Kcp.Check(current);
                    m_NeedKcpUpdateFlag = false;
                }
            }


            if (m_WaitForReconnect)
            {
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    Reconnect();
                }
                else
                {
                    Debuger.Log("等待重连,但是网络不可用!");
                }
            }


            CheckTimeout();
        }
예제 #27
0
    void update(UInt32 current)
    {
        //if (mInConnectStage)
        //{
        //    if (connect_timeout(current))
        //    {
        //        m_connectStatusCallback(NetworkState.ConnectBreak);
        //        isConnect = false;
        //        mInConnectStage = false;
        //        return;
        //    }

        //    if (need_send_connect_packet(current))
        //    {
        //        mLastSendConnectTime = current;
        //        mUdpClient.Send(new byte[4] { 0, 0, 0, 0 }, 4);
        //    }

        //    process_connect_packet();

        //    return;
        //}

        if (isConnect)
        {
            process_recv_queue(current);

            if (mNeedUpdateFlag || current >= mNextUpdateTime)
            {
                mKcp.Update(current);
                mNextUpdateTime = mKcp.Check(current);
                //Debug.Log("mNextUpdateTime :" + (current));
                mNeedUpdateFlag = false;
            }
        }
        //SendHeartPackage(current);
    }
예제 #28
0
        void update(UInt32 current)
        {
            if (mInConnectStage)
            {
                if (connect_timeout(current))
                {
                    evHandler(cliEvent.ConnectFailed, null, "Timeout");
                    mInConnectStage = false;
                    return;
                }

                if (need_send_connect_packet(current))
                {
                    mLastSendConnectTime = current;
                    mUdpClient.Send(new byte[4] {
                        0, 0, 0, 0
                    }, 4);
                }

                process_connect_packet();

                return;
            }

            if (mConnectSucceed)
            {
                process_recv_queue();

                if (mNeedUpdateFlag || current >= mNextUpdateTime)
                {
                    mKcp.Update(current);
                    mNextUpdateTime = mKcp.Check(current);
                    mNeedUpdateFlag = false;
                }
            }
        }
예제 #29
0
    void update(UInt32 current)
    {
        if (mInConnectStage)
        {
            if (connect_timeout(current))
            {
                m_connectStatusCallback(NetworkState.ConnectBreak);
                mInConnectStage = false;
                return;
            }

            if (need_send_connect_packet(current))
            {
                mLastSendConnectTime = current;
                mUdpClient.Send(new byte[4] {
                    0, 0, 0, 0
                }, 4);
            }

            process_connect_packet();

            return;
        }

        if (mConnectSucceed)
        {
            process_recv_queue();

            if (mNeedUpdateFlag || current >= mNextUpdateTime)
            {
                mKcp.Update(current);
                mNextUpdateTime = mKcp.Check(current);
                mNeedUpdateFlag = false;
            }
        }
    }
예제 #30
0
        // 测试用例
        static void KCPTest(int mode)
        {
            // 创建模拟网络:丢包率10%,Rtt 60ms~125ms
            vnet = new LatencySimulator(10, 60, 125);

            // 创建两个端点的 kcp对象,第一个参数 conv是会话编号,同一个会话需要相同
            // 最后一个是 user参数,用来传递标识
            var kcp1 = new KCP(0x11223344, 1);
            var kcp2 = new KCP(0x11223344, 2);

            // 设置kcp的下层输出,这里为 udp_output,模拟udp网络输出函数
            kcp1.SetOutput(udp_output);
            kcp2.SetOutput(udp_output);

            UInt32 current = Utils.iclock();
            UInt32 slap    = current + 20;
            UInt32 index   = 0;
            UInt32 next    = 0;
            Int64  sumrtt  = 0;
            int    count   = 0;
            int    maxrtt  = 0;

            // 配置窗口大小:平均延迟200ms,每20ms发送一个包,
            // 而考虑到丢包重发,设置最大收发窗口为128
            kcp1.WndSize(128, 128);
            kcp2.WndSize(128, 128);

            if (mode == 0) // 默认模式
            {
                kcp1.NoDelay(0, 10, 0, 0);
                kcp2.NoDelay(0, 10, 0, 0);
            }
            else if (mode == 1) // 普通模式,关闭流控等
            {
                kcp1.NoDelay(0, 10, 0, 1);
                kcp2.NoDelay(0, 10, 0, 1);
            }
            else // 启动快速模式
            {
                // 第1个参数 nodelay-启用以后若干常规加速将启动
                // 第2个参数 interval为内部处理时钟,默认设置为 10ms
                // 第3个参数 resend为快速重传指标,设置为2
                // 第4个参数 为是否禁用常规流控,这里禁止
                kcp1.NoDelay(1, 10, 2, 1);
                kcp2.NoDelay(1, 10, 2, 1);
                kcp1.SetMinRTO(10);
                kcp1.SetFastResend(1);
            }

            var    buffer = new byte[2000];
            int    hr     = 0;
            UInt32 ts1    = Utils.iclock();

            while (true)
            {
                Thread.Sleep(1);
                current = Utils.iclock();
                kcp1.Update(current);
                kcp2.Update(current);

                // 每隔 20ms,kcp1发送数据
                for (; current >= slap; slap += 20)
                {
                    KCP.ikcp_encode32u(buffer, 0, index++);
                    KCP.ikcp_encode32u(buffer, 4, current);

                    // 发送上层协议包
                    kcp1.Send(buffer, 0, 8);
                }

                // 处理虚拟网络:检测是否有udp包从p1->p2
                while (true)
                {
                    hr = vnet.Recv(1, buffer, 2000);
                    if (hr < 0)
                    {
                        break;
                    }

                    // 如果 p2收到udp,则作为下层协议输入到kcp2
                    hr = kcp2.Input(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // 处理虚拟网络:检测是否有udp包从p2->p1
                while (true)
                {
                    hr = vnet.Recv(0, buffer, 2000);
                    if (hr < 0)
                    {
                        break;
                    }

                    // 如果 p1收到udp,则作为下层协议输入到kcp1
                    hr = kcp1.Input(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // kcp2接收到任何包都返回回去
                while (true)
                {
                    hr = kcp2.Recv(buffer, 0, 10);
                    if (hr < 0)
                    {
                        break;
                    }

                    // 如果收到包就回射
                    hr = kcp2.Send(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // kcp1收到kcp2的回射数据
                while (true)
                {
                    hr = kcp1.Recv(buffer, 0, 10);
                    if (hr < 0) // 没有收到包就退出
                    {
                        break;
                    }

                    int    offset = 0;
                    UInt32 sn     = KCP.ikcp_decode32u(buffer, ref offset);
                    UInt32 ts     = KCP.ikcp_decode32u(buffer, ref offset);
                    UInt32 rtt    = current - ts;

                    if (sn != next)
                    {
                        // 如果收到的包不连续
                        Console.WriteLine(String.Format("ERROR sn {0}<->{1}", count, next));
                        return;
                    }
                    next++;
                    sumrtt += rtt;
                    count++;
                    if (rtt > maxrtt)
                    {
                        maxrtt = (int)rtt;
                    }

                    Console.WriteLine(String.Format("[RECV] mode={0} sn={1} rtt={2}", mode, sn, rtt));
                }
                if (next > 1000)
                {
                    break;
                }
            }
            ts1 = Utils.iclock() - ts1;
            var names = new string[3] {
                "default", "normal", "fast"
            };

            Console.WriteLine("{0} mode result ({1}ms):", names[mode], ts1);
            Console.WriteLine("avgrtt={0} maxrtt={1} tx={2}", sumrtt / count, maxrtt, vnet.tx1);
            Console.WriteLine("Press any key to next...");
            Console.Read();
        }