IEnumerator PingRegion(CloudServerRegion region) { // JP can't be pinged at the moment, so we skip it! if (region == CloudServerRegion.Japan) { yield break; } string hostname = ServerSettings.FindServerAddressForRegion(region); string regionIp = ResolveHost(hostname); if (string.IsNullOrEmpty(regionIp)) { Debug.LogError("Could not resolve host: " + hostname); yield break; } int averagePing = 0; int tries = 3; int skipped = 0; float timeout = 0.500f; // 500 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) { // Timeout after 500ms: sometimes Unity ping never returns yield return(0); } if (ping.time == -1) { if (skipped > 5) { averagePing += (int)(timeout * 1000) * tries; break; } else { i -= 1; //Sometimes Unity ping doesnt return, we therefor retry a few times.. skipped++; continue; } } averagePing += ping.time; } int regionAverage = averagePing / tries; //Debug.LogWarning (hostname + ": " + average + "ms"); if (regionAverage < lowestRegionAverage || lowestRegionAverage == -1) { lowestRegionAverage = regionAverage; SetRegion(region); } }
IEnumerator PingRegion(CloudServerRegion region) { // JP can't be pinged at the moment, so we skip it! if (region == CloudServerRegion.Japan) { yield break; } string hostname = ServerSettings.FindServerAddressForRegion(region); string regionIp = ResolveHost(hostname); if (string.IsNullOrEmpty(regionIp)) { Debug.LogError("Could not resolve host: " + hostname); yield break; } int averagePing = 0; int tries = 3; int skipped = 0; float timeout = 0.500f; // 500 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) { // Timeout after 500ms: sometimes Unity ping never returns yield return 0; } if (ping.time == -1) { if (skipped > 5) { averagePing += (int)(timeout * 1000) * tries; break; } else { i -= 1; //Sometimes Unity ping doesnt return, we therefor retry a few times.. skipped++; continue; } } averagePing += ping.time; } int regionAverage = averagePing / tries; //Debug.LogWarning (hostname + ": " + average + "ms"); if (regionAverage < lowestRegionAverage || lowestRegionAverage == -1) { lowestRegionAverage = regionAverage; SetRegion(region); } }
/// <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()); } }
void Awake() { SP = this; // load settings and ping only if none available if (PlayerPrefs.GetString(playerPrefsKey, "") != "") { string reg = PlayerPrefs.GetString(playerPrefsKey, ""); closestRegion = (CloudServerRegion)Enum.Parse(typeof(CloudServerRegion), reg, true); return; } StartCoroutine(PingAllRegions()); }
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()); } }
public static string FindServerAddressForRegion(CloudServerRegion regionIndex) { return ServerSettings.DefaultCloudServerUrl.Replace("app-eu", ServerSettings.CloudServerRegionPrefixes[(int)regionIndex]); }
public static void OverrideRegion(CloudServerRegion region) { SetRegion(region); }
/// <summary> /// Tries to convert a region shortcut to a value of CloudServerRegion enum. Defaults to CloudServerRegion.US, if that fails. /// </summary> /// <returns>If conversion is successful.</returns> public static bool TryParseCloudServerRegion(string regionShortcut, out CloudServerRegion region) { region = CloudServerRegion.US; try { region = (CloudServerRegion)Enum.Parse(typeof(CloudServerRegion), regionShortcut, true); } catch { return false; } return true; }
private static void SetRegion(CloudServerRegion region) { closestRegion = region; PlayerPrefs.SetString(playerPrefsKey, region.ToString()); }
public static string FindServerAddressForRegion(CloudServerRegion regionIndex) { return(ServerSettings.DefaultCloudServerUrl.Replace("app-eu", ServerSettings.CloudServerRegionPrefixes[(int)regionIndex])); }
/// <summary> /// Overwrites the region that is used for ConnectToBestCloudServer(string gameVersion) /// This will overwrite the result that was gotten by pinging all cloud servers. /// </summary> /// <remarks> /// Use this to allow your users to specify their region manually. /// </remarks> public static void OverrideBestCloudServer(CloudServerRegion region) { PingCloudRegions.OverrideRegion(region); }