Exemple #1
0
        private static IEnumerator PingRoutine(string url, Action <bool> callback)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            Ping ping = new Ping(url);

            Log.Debug("Ping started");

            bool shouldSkip = false;

            CoroutineController.DoAfterGivenTime(PingTimeOutDuration / 1000f, () => shouldSkip = true, "PingSkipTimer");

            yield return(new WaitUntil(() => ping.isDone || shouldSkip));


            watch.Stop();

            if (ping.isDone)
            {
                Log.Debug("Ping completed.");
                Log.Debug("Ping time: " + ping.time);

                // If pinging exceeds the default time (timeout), Unity returns -1 as ping.time
                // This ping.time < 0 check is for that reason
                if (ping.time < 0 && ping.time > PingTimeOutDuration)
                {
                    // ----FAIL----
                    Log.Error("Rateus Timeout. ping.time:" + ping.time);
                    callback.Invoke(false);

                    if (CoroutineController.IsCoroutineRunning("PingSkipTimer"))
                    {
                        CoroutineController.StopCoroutine("PingSkipTimer");
                    }
                }
                else
                {
                    // ----SUCCESS-----
                    callback.Invoke(true);
                }
            }
            else if (shouldSkip)
            {
                // ----FAIL----
                Log.Error("Ping failed.");
                callback.Invoke(false);
            }
            else
            {
                // ----FAIL----
                Log.Error("Unknown state.");
                callback.Invoke(false);
            }

            Log.Debug("Watch time: " + watch.Elapsed.TotalMilliseconds);
        }