/// <summary> /// Pings a server several times to get an average RTT. If that's lower than the current best, the region becomes closestRegion. /// </summary> /// <param name="region"></param> /// <returns></returns> IEnumerator PingRegion(CloudServerRegion region) { string hostname = ServerSettings.FindServerAddressForRegion(region); string regionIp = ResolveHost(hostname); if (string.IsNullOrEmpty(regionIp)) { Debug.LogError("Could not resolve host: " + hostname); yield break; } int pingSum = 0; int tries = 3; int skipped = 0; float timeout = 0.700f; // 700 milliseconds is our max, after this we assume a timeout. for (int i = 0; i < tries; i++) { float startTime = Time.time; Ping ping = new Ping(regionIp); while (!ping.isDone && Time.time < startTime + timeout) { // sometimes Unity ping never returns, so we use a timeout here yield return(0); } if (ping.time == -1) { if (skipped > 5) { pingSum += (int)(timeout * 1000) * tries; break; } else { i -= 1; //Sometimes Unity ping doesnt return, we therefor retry a few times.. skipped++; continue; } } pingSum += ping.time; } int pingAverage = pingSum / tries; //Debug.LogWarning (hostname + ": " + regionAverage + "ms"); if (pingAverage < lowestRegionAverage || lowestRegionAverage == -1) { lowestRegionAverage = pingAverage; SaveAndSetRegion(region.ToString()); } }
public static void OverrideRegion(CloudServerRegion region) { SaveAndSetRegion(region.ToString()); }
/// <summary> /// Pings a server several times to get an average RTT. If that's lower than the current best, the region becomes closestRegion. /// </summary> /// <param name="region"></param> /// <returns></returns> IEnumerator PingRegion(CloudServerRegion region) { string hostname = ServerSettings.FindServerAddressForRegion(region); string regionIp = ResolveHost(hostname); if (string.IsNullOrEmpty(regionIp)) { Debug.LogError("Could not resolve host: " + hostname); yield break; } int pingSum = 0; int tries = 3; int skipped = 0; float timeout = 0.700f; // 700 milliseconds is our max, after this we assume a timeout. for (int i = 0; i < tries; i++) { float startTime = Time.time; Ping ping = new Ping(regionIp); while (!ping.isDone && Time.time < startTime + timeout) { // sometimes Unity ping never returns, so we use a timeout here yield return 0; } if (ping.time == -1) { if (skipped > 5) { pingSum += (int)(timeout * 1000) * tries; break; } else { i -= 1; //Sometimes Unity ping doesnt return, we therefor retry a few times.. skipped++; continue; } } pingSum += ping.time; } int pingAverage = pingSum / tries; //Debug.LogWarning (hostname + ": " + regionAverage + "ms"); if (pingAverage < lowestRegionAverage || lowestRegionAverage == -1) { lowestRegionAverage = pingAverage; SaveAndSetRegion(region.ToString()); } }
private static void SetRegion(CloudServerRegion region) { closestRegion = region; PlayerPrefs.SetString(playerPrefsKey, region.ToString()); }