Esempio n. 1
0
        public void update()
        {
            updatetime += RealTime.deltaTime;
            if (updatetime < 0.1)
            {
                return;
            }
            updatetime = 0;

            if (m_reconnect != null)
            {
                m_reconnect.Update();
            }

            if (m_prepareConnect && m_connector == null)
            {
                m_prepareConnect = false;
#if (NETMGR_USE_IP_FOR_CON)
                _ConnectAsyncWithIp();
#else
                _ConnectAsyncWithUrl();
#endif
            }

            if (m_connector != null)
            {
                float time = Time.time;

                if (m_connector.CheckTimeOut(time))
                {
                    m_connector.Abort();
                    PrepareToConnect();
                    m_connector = null;
                    //   LogMgr.LogError("Connect Time out");
                    return;
                }

                if (m_connector.IsError())
                {
                    m_connector.WaitTermination();
                    string msg = m_connector.ErrorMsg();
                    LogMgr.LogError("Connect error:" + msg);
                    m_connector = null;
                    PrepareToConnect();
                    return;
                }

                if (m_connector.IsConnected() == false)
                {
                    // LogMgr.LogError("Wait for connect");
                    return;
                }

                m_connector.WaitTermination();
                m_connector = null;

                if (!m_tcpClient.Connected)
                {
                    //LogMgr.LogError("connect fail");
                    PrepareToConnect();
                    return;
                }
                else
                {
                    //LogMgr.LogError("connect ok");
                    connectOK();
                }
            }
            else
            {
                if (m_isConnectted)
                {
                    bool pollret      = m_tcpClient.Client.Poll(1000, SelectMode.SelectRead);
                    bool available    = m_tcpClient.Client.Available == 0;
                    bool isConnecting = !((pollret && available) || !m_tcpClient.Connected);
                    if (isConnecting)
                    {
                        checkCounter = 0;
                    }
                    //bool isConnecting = !m_tcpClient.Connected;
                    if (isConnecting == false)
                    {
                        checkCounter++;
                        //增加3次统计
                        if (checkCounter >= 3)
                        {
                            LogMgr.LogError("m_tcpClient.Connected:" + m_tcpClient.Connected + " pollret:" + pollret + " available:" + available);
                            m_isConnectted = false;
                            onError(10051);
                            //在网络状况发生变化的时候结束当前收发线程
                            LogMgr.UnityLog("SetTerminateFlag!");
                            SetTerminateFlag();

                            LogMgr.UnityLog("onClose!");
                            if (onClose != null)
                            {
                                onClose();
                            }
                        }
                    }
                    else if (m_currentTry != null)
                    {
                        //连接上时,当前连接请求已经完成,不再需要
                        m_currentTry = null;

                        StartSendThread();
                        StartReceiveThread();
                    }
                }

                dispatchMsg();
            }
            //processPendingMsg();
        }