Beispiel #1
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;
         }
     }
 }
Beispiel #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();
        }
Beispiel #3
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();
                }
            }
        }
Beispiel #4
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();
 }
Beispiel #5
0
 void update(UInt32 current)
 {
     if (mNeedUpdateFlag || current >= mNextUpdateTime)
     {
         mKcp.Update(current);
         mNextUpdateTime = mKcp.Check(current);
         mNeedUpdateFlag = false;
     }
 }
Beispiel #6
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;
            }
        }
Beispiel #7
0
    void update(UInt32 current)
    {
        process_recv_queue();

        if (mNeedUpdateFlag || current >= mNextUpdateTime)
        {
            mKcp.Update(current);
            mNextUpdateTime = mKcp.Check(current);
            mNeedUpdateFlag = false;
        }
    }
Beispiel #8
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;
            }
        }
Beispiel #9
0
        public void Tick(uint currentTimeMS)
        {
            DoReceiveInMain();
            uint current = currentTimeMS;

            if (needKcpUpdateFlag || current >= nextKcpUpdateTime)
            {
                kcp.Update(current);
                nextKcpUpdateTime = kcp.Check(current);
                needKcpUpdateFlag = false;
            }
        }
Beispiel #10
0
 private void UpdateKCP(UInt32 current)
 {
     if (mKcp == null)
     {
         return;
     }
     if (mNeedUpdateFlag || current >= mNextUpdateTime)
     {
         mKcp.Update(current);
         mNextUpdateTime = mKcp.Check(current);
         mNeedUpdateFlag = false;
     }
 }
Beispiel #11
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;
     }
 }
Beispiel #12
0
        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;
            }
        }
Beispiel #13
0
    public void Update()
    {
        if (mKCP == null)
        {
            return;
        }

        if (0 == mNextUpdateTime || mKCP.CurrentMS >= mNextUpdateTime)
        {
            mKCP.Update();
            mNextUpdateTime = mKCP.Check();
        }
    }
Beispiel #14
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;
            }
        }
    }
 public void Tick()
 {
     if (Connected)
     {
         DoReceiveInMain();
         uint current = (uint)TimeUtility.GetTotalMillisecondsSince1970();
         if (needKcpUpdateFlag || current >= nextKcpUpdateTime)
         {
             kcp.Update(current);
             nextKcpUpdateTime = kcp.Check(current);
             needKcpUpdateFlag = false;
         }
     }
 }
Beispiel #16
0
        public void Update(uint current)
        {
            if (kcp == null)
            {
                return;
            }

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

            kcp.Update(current);
            nextUpdateTime = kcp.Check(current);
        }
Beispiel #17
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;
            }
        }
    }
Beispiel #18
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
            {
                //处理重连
            }
        }
Beispiel #19
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);
        }
Beispiel #20
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;
            }
        }
Beispiel #21
0
        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();
        }
    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);
    }
Beispiel #23
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;
            }
        }
    }
Beispiel #24
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;
                }
            }
        }