示例#1
0
    /// <summary>
    /// Connects to the Arduino
    /// </summary>
    public IEnumerator Connect()
    {
        Ping ping = new Ping(Host);

        yield return(new WaitForSecondsRealtime(TimeOut));

        if (!ping.isDone)
        {
            ping.DestroyPing();
            Debug.LogError("Unable to connect to Arduino");
            yield break;
        }
        ping.DestroyPing();
        try
        {
            mySocket    = new TcpClient(Host, Port);
            stream      = mySocket.GetStream();
            writer      = new StreamWriter(stream);
            reader      = new StreamReader(stream);
            SocketReady = true;
        }
        catch (Exception e)
        {
            Debug.Log("Socket error:" + e);
        }
        yield break;
    }
示例#2
0
        void Update()
        {
            if (!searchingConnection)
            {
                activeRequestCount = 0;
                for (int i = queue.Count - 1; i >= 0; i--)
                {
                    BaseRequest request = queue[i];

                    //Si ya se completo se ignora este ciclo
                    if (!request.isComplete)
                    {
                        if (activeRequestCount >= simultaneousRequests)
                        {
                            //Ya nadie mas puede hacer request
                            break;
                        }

                        if (request.isRequesting)
                        {
                            activeRequestCount++;
                        }
                        else
                        {
                            //Necesita esperar para no saturar de llamadas fallidas sin internet
                            if (!request.failed || request.getTimeSinceFailed() >= waitTimeForFailedRequest)
                            {
                                //Activamos la request
                                request.OnComplete += OnRequestComplete;
                                request.OnFailed   += OnRequestFailed;
                                request.OnTimeout  += OnRequestTimeout;
                                request.start();
                                activeRequestCount++;
                            }
                        }
                    }
                }
            }
            else
            {
                if (ping.isDone)
                {
                    alreadyConnected();
                }
                else
                {
                    //Al parecer no importa si se recupero la conexion el ping no lo detecta
                    //Le damos un timeout para la deteccion
                    if (Time.realtimeSinceStartup - pingStartTime > pingTimeout)
                    {
                        //Iniciamos de nuevo
                        ping.DestroyPing();
                        ping          = new Ping(trustedIPAddress);
                        pingStartTime = Time.realtimeSinceStartup;
                    }
                }
            }
        }
示例#3
0
    //-------------------------------------------------------------------------
    public void start(string ip)
    {
        mStart = true;
        mIp    = ip;

        if (mPing != null)
        {
            mPing.DestroyPing();
            mPing = null;
        }
        mPing = new Ping(mIp);
    }
示例#4
0
 public static void SetIp(string ip)
 {
     if (ping != null)
     {
         ping.DestroyPing();
     }
     NetSpeed.ip             = ip;
     times                   = 0;
     totalTime               = 0;
     frameTimes              = 0;
     continuityOverTimeTimes = 0;
     lastSpeed               = 0;
     ping = new Ping(ip);
 }
示例#5
0
 void Update()
 {
     if (ping != null && ping.isDone)
     {
         log.Remove(0, log.Length);
         log.Append(ping.time);
         log.Append("MS");
         text.text = log.ToString();
         if (ping.time <= 100)
         {
             text.color = Color.white;
         }
         else if (ping.time < 300)
         {
             text.color = new Color(1f, 0.5f, 0f, 1f);
         }
         else
         {
             text.color = Color.red;
         }
         ping.DestroyPing();
         ping = null;
         Invoke("Ping", 1);
     }
 }
示例#6
0
        private void Ping()
        {
            if (connectCor != null && connection.IsConnected &&
                Time.time - prevSendPingTime > pingInterval &&
                ping != null)
            {
                if (ping.time <= 0)
                {
                    return;
                }

                if (ping.time > delayTimeThreshold && PingTime > delayTimeThreshold)
                {
                    var closeThreshold = delayTimeThreshold * 2;
                    if (ping.time > closeThreshold && PingTime > closeThreshold)
                    {
                        PingTime = 0;
                        Close();
                        return;
                    }
                    InvokeStatusEvent(NetworkStatus.HighLatency);
                }

                PingTime = ping.time;
                ping.DestroyPing();
                prevSendPingTime = Time.time;
                ping             = new Ping(NetworkAddress.ip);
            }
        }
示例#7
0
    private IEnumerator Pingip()
    {
        float pingTime = 0;

        ping      = new Ping(IP);
        delayTime = Math.Abs(ping.time);
        while (!ping.isDone)
        {
            yield return(new WaitForSeconds(0.1f)); //0.1秒检测一次

            if (pingTime > 3.0)                     //如果大于3秒还没有ping成功,默认网络不可用
            {
                Debug.Log("ping失败");
                break;
            }
            pingTime += 0.1f;
        }
        if (ping.isDone)
        {
            Debug.Log("ping成功");
        }
        ping.DestroyPing();
        yield return(new WaitForSeconds(1f));//1秒重新ping一次

        StartCoroutine(Pingip());
    }
示例#8
0
 void Update()
 {
     if (getPing)
     {
         if (null != netPing && netPing.isDone)
         {
             getPing = false;
             //Log.info("--------------------------------------netPing.time: " + netPing.time);
             pingTime = netPing.time >= 0 ? netPing.time : int.MaxValue;
             netPing.DestroyPing();
             netPing = null;
             //Log.info("--------------------------------------ping: " + pingTime);
             EventManager.Send(EventType_Net.UPDATE_GAME_PING, pingTime);
         }
     }
 }
示例#9
0
 void DestroyPing()
 {
     if (null != _Ping)
     {
         _Ping.DestroyPing();
         _Ping = null;
     }
 }
示例#10
0
    static int DestroyPing(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 1);
        Ping obj = LuaScriptMgr.GetNetObject <Ping>(L, 1);

        obj.DestroyPing();
        return(0);
    }
示例#11
0
 public void DestroyPing()
 {
     if (ping != null)
     {
         ping.DestroyPing();
         ping = null;
     }
 }
示例#12
0
 // Update is called once per frame
 void Update()
 {
     if (ping != null && ping.isDone)
     {
         float delayTime = ping.time;
         ping.DestroyPing();
         ping = null;
         upDateWifiStaut(delayTime);
     }
 }
示例#13
0
 public override void Tick()
 {
     if (Time.realtimeSinceStartup - _timeStart > _rate)
     {
         _timeStart = Time.realtimeSinceStartup;
         Action(ping.time);
         ping.DestroyPing();
         ping = new Ping(_ip);
     }
 }
示例#14
0
 private void InitPing()
 {
     if (_ping != null)
     {
         _ping.DestroyPing();
     }
     _ping       = null;
     _start_time = 0;
     _last_time  = -_ITERATE;
 }
示例#15
0
 //输出延迟时间,即ping结果,如果Ping执行完,会再次初始化一个继续执行
 public void PingTest1()
 {
     Out((int)delayTime);
     if (null != ping && ping.isDone)
     {
         delayTime = ping.time;
         ping.DestroyPing();
         ping = null;
         Invoke("SendPing", 1.0F);//每秒Ping一次
     }
 }
示例#16
0
    IEnumerator PingConnect()
    {
        // Ping網站
        Ping ping = new Ping(s_ip);

        int addTime      = 0;
        int requestCount = s_timeout * 10; // 0.1秒 请求 1 次,所以请求次数是 n秒 x 10

        // 等待请求返回
        while (!ping.isDone)
        {
            yield return(new WaitForSeconds(0.1f));

            // 链接失败
            if (addTime > requestCount)
            {
                addTime = 0;

                if (s_callback != null)
                {
                    s_callback(ping.time);
                    ping.DestroyPing();
                    //Destroy(this.gameObject);
                }
                yield break;
            }
            addTime++;
        }

        // 链接成功
        if (ping.isDone)
        {
            if (s_callback != null)
            {
                s_callback(ping.time);
                ping.DestroyPing();
                //Destroy(this.gameObject);
            }
            yield return(null);
        }
    }
示例#17
0
    IEnumerator PingUpdate()
    {
        if (string.IsNullOrEmpty(NetData.instance._Ip))
        {
            // test environment
            yield break;
        }

        while (true)
        {
            Ping p = new Ping(NetData.instance._Ip);             //ip부분은 다시

            yield return(new WaitForSeconds(0.1f));

            while (!p.isDone)
            {
                yield return(null);
            }

            int ping = p.time;
            if (ping < 0)
            {
                ping = 0;
            }

            Ping.text = string.Format(_LowDataMgr.instance.GetStringCommon(1178), ping);//string.Format("Ping {0}", p.time);
            if (ping >= 200)
            {
                Ping.color = RedColor;
            }
            else if (ping >= 100)
            {
                Ping.color = Color.yellow;
            }
            else
            {
                Ping.color = GreenColor;
            }

            p.DestroyPing();
            p = null;

            if (DeviceTime.Minute != DateTime.Now.Minute)//분단위로 찍으니깐 다르면 다시 갱신하자.
            {
                SetBottomInfo();
            }

            yield return(new WaitForSeconds(1f));
        }
    }
示例#18
0
    void OnGUI()
    {
        if (null != ping && (isNetWorkLose || ping.isDone))
        {
            isNetWorkLose = false;
            label         = ping.time.ToString();
            SetColor(ping.time);
            ping.DestroyPing();
            ping = null;
            Invoke("SendPing", 1);//每秒Ping一次
        }

        GUI.Label(new Rect(10, 60, 200, 50), "Ping:" + label + "ms", guiStyle);
    }
示例#19
0
    IEnumerator Ping()
    {
        p        = new Ping("127.0.0.1");
        lastTime = Time.realtimeSinceStartup;
        while (!p.isDone && Time.realtimeSinceStartup - lastTime < 1)
        {
            yield return(null);
        }
        PingTime = p.time;
        p.DestroyPing();
        yield return(new WaitForSeconds(1));

        StartCoroutine(Ping());
    }
示例#20
0
    // Update is called once per frame
    void Update()
    {
        m_FrameUpdate++;
        if (Time.realtimeSinceStartup - m_LastUpdateShowTime >= m_UpdateShowDeltaTime)
        {
            m_FPS                = m_FrameUpdate / (Time.realtimeSinceStartup - m_LastUpdateShowTime);
            m_FrameUpdate        = 0;
            m_LastUpdateShowTime = Time.realtimeSinceStartup;
        }

        if (pingIp == string.Empty || ping == null)
        {
            return;
        }

        if (null != ping && ping.isDone)
        {
            delayTime = ping.time;
            ping.DestroyPing();
            ping = null;
            Invoke("SendPing", 1.0F);            //每秒Ping一次
        }
    }
示例#21
0
 public IEnumerator pingIP()
 {
     while (true)
     {
         ping = new Ping(ip);
         while (!ping.isDone)
         {
             yield return(0);
         }
         time = ping.time;
         ping.DestroyPing();
         yield return(new WaitForSeconds(1f));
     }
 }
示例#22
0
    private IEnumerator CheckAccess(float timeout)
    {
        float startTime = Time.realtimeSinceStartup;

        this.resolvingConnectivity = true;
        Ping  ping     = new Ping(this.ipAddress);
        float timeLeft = timeout;

        while (!ping.isDone && timeLeft > 0f)
        {
            yield return(null);

            timeLeft -= GameTime.RealTimeDelta;
        }
        if (ping.isDone && (float)ping.time > 0f)
        {
            if (!this.connected && this.OnNetworkChange != null)
            {
                this.OnNetworkChange(true);
            }
            if (this.OnCheckResponse != null)
            {
                this.OnCheckResponse(true);
            }
            this.connected             = true;
            this.OnCheckResponse       = null;
            this.resolvingConnectivity = false;
            this.waitingCheck          = false;
        }
        else if (this.hasFocus)
        {
            this.fallbackChecking = true;
            base.StartCoroutine(this.WaitFallback());
            this.fallbackThread = new Thread(new ThreadStart(this.FallbackResolveAccess));
            this.fallbackThread.Start();
        }
        else
        {
            this.OnFocus = (Action)Delegate.Combine(this.OnFocus, new Action(delegate()
            {
                this.fallbackChecking = true;
                base.StartCoroutine(this.WaitFallback());
                this.fallbackThread = new Thread(new ThreadStart(this.FallbackResolveAccess));
                this.fallbackThread.Start();
            }));
        }
        ping.DestroyPing();
        yield break;
    }
示例#23
0
 // Update is called once per frame
 void Update()
 {
     if (null != ping && ping.isDone)
     {
         isNetWorkLose = false;
         sb.Remove(0, sb.Length);
         sb.Append(ping.time);
         SetNetPicType(ping.time);
         sb.Append("ms");
         PingLabel.text = sb.ToString();
         ping.DestroyPing();
         ping = null;
         Invoke("SendPing", 5);//每秒Ping一次
     }
 }
示例#24
0
        /// <summary>
        /// 网络延迟检测(越小网络越好).
        /// </summary>
        public int NetWorkPing(string varIP)
        {
            Ping tempPing = new Ping(varIP);

            while (true)
            {
                if (tempPing != null && tempPing.isDone)
                {
                    int Time;
                    Time = tempPing.time;
                    tempPing.DestroyPing();
                    tempPing = null;
                    return(Time);                   //单位 ms
                }
            }
        }
示例#25
0
    IEnumerator ping()
    {
        WaitForSeconds w = new WaitForSeconds(1f);

        while (true)
        {
            Ping p = new Ping(mClient.connection.address);
            while (!p.isDone)
            {
                yield return(null);
            }
            netDelay = 0.001f * p.time;
            p.DestroyPing();
            yield return(w);
        }
    }
示例#26
0
 // Update is called once per frame
 void Update()
 {
     if (pinging)
     {
         timer += Time.deltaTime;
         if (ping.isDone)
         {
             Debug.Log("ping: " + ping.time);
             SceneManager.LoadScene("Lobby");
         }
         if (timer > 3)
         {
             pinging = false;
             timer   = 0;
             ping.DestroyPing();
             Debug.Log("ping timeout");
         }
     }
 }
示例#27
0
    void OnGUI()
    {
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            label = "460";
            SetColor(460);
            isNetWorkLose = true;
        }
        else if (isNetWorkLose || (null != ping && ping.isDone))
        {
            isNetWorkLose = false;
            label         = ping.time.ToString();
            SetColor(ping.time);
            ping.DestroyPing();
            ping = null;
            Invoke("SendPing", 1);            //每秒Ping一次
        }

        GUI.Label(new Rect(10, 50, 200, 50), "ping:" + label + "ms", guiStyle);
    }
示例#28
0
        private IEnumerator I_CheckInternet(System.Action <bool> res)
        {
            CheckRunning = true;
            Ping  ping = new Ping(PingIP);
            float t    = 0f;

            while (!ping.isDone)
            {
                t += Time.deltaTime;
                if (t >= Timeout)
                {
                    break;
                }

                yield return(new WaitForEndOfFrame());
            }

            bool response = ping.time > 0;

            if (res != null)
            {
                res.Invoke(response);
            }

            if (response)
            {
                OnInternet.Invoke();
            }
            else
            {
                OnNoInternet.Invoke();
            }

            LastResponse = response;

            ping.DestroyPing();

            CheckRunning = false;

            yield return(null);
        }
示例#29
0
        IEnumerator PingUpdate()
        {
            IPHostEntry host = null;

            try
            {
                host = Dns.GetHostEntry("google.com");
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                HasConnection = false;
            }

            if (host != null && host.AddressList.Length > 0)
            {
                //Debug.Log("Adresses: \n" + string.Join(";\n", host.AddressList.Select(adr => adr.ToString())));
                Ping ping = new Ping(host.AddressList.First().ToString());
                yield return(new WaitUntil(() => ping.isDone || ping.time >= 5000));

                if (ping.isDone == false)
                {
                    HasConnection = false;
                }
                else
                {
                    HasConnection = true;
                }
                ping.DestroyPing();
            }
            else
            {
                HasConnection = false;
            }

            yield return(new WaitForSeconds(1f));

            StartCoroutine(PingUpdate());
        }
示例#30
0
    private IEnumerator RequestPing()
    {
        if (string.IsNullOrEmpty(address))
        {
            address = "8.8.8.8";
        }

        float pingTime = 0.0f;
        Ping  ping     = new Ping(address);

        while (!ping.isDone)
        {
            yield return(null);

            pingTime += Time.deltaTime;
            if (pingTime >= maxPingWaitingTime)
            {
                break;
            }
        }

        lastPingResult = ping.isDone;
        lastPingTime   = ping.time;

        if (OnPingComplete != null)
        {
            OnPingComplete.Invoke(LastPingResult, LastPingTime);
        }

        ping.DestroyPing();

        //Петля
        if (autoPing)
        {
            yield return(new WaitForSeconds(timerAutoPing));

            Ping();
        }
    }