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()); } }
public void Connect2() { var gameVersion = menuLoaded ? "eddy" + 5 : "1"; PhotonNetwork.PhotonServerSettings.AppID = bs.settings.serv.appIds[appId % bs.settings.serv.appIds.Length]; if (serverRegion == -1) { PhotonNetwork.ConnectToBestCloudServer(gameVersion); } else { PhotonNetwork.ConnectToMaster(ServerSettings.FindServerAddressForRegion(serverRegion), 5055, PhotonNetwork.PhotonServerSettings.AppID, gameVersion); } }
IEnumerator ConnectToBestRegionInternal(string gameVersion) { while (isPinging) { yield return(0); // wait until pinging finished (offline mode won't ping) } ServerSettings settings = (ServerSettings)Resources.Load(PhotonNetwork.serverSettingsAssetFile, typeof(ServerSettings)); if (settings.HostType == ServerSettings.HostingOption.OfflineMode) { PhotonNetwork.ConnectUsingSettings(gameVersion); } else { PhotonNetwork.Connect(ServerSettings.FindServerAddressForRegion(closestRegion), settings.ServerPort, settings.AppID, gameVersion); } }
IEnumerator ConnectToBestRegionInternal() { CloudServerRegion bestRegion; if (!ClosestRegionAvailable || !ServerSettings.TryParseCloudServerRegion(ClosestRegion, out bestRegion)) { RefreshCloudServerRating(); } while (isPinging) { yield return(0); // wait until pinging finished (offline mode won't ping) } ServerSettings.TryParseCloudServerRegion(ClosestRegion, out bestRegion); string bestServerAddress = ServerSettings.FindServerAddressForRegion(bestRegion); string bestServerFullAddress = bestServerAddress + ":" + ServerSettings.DefaultMasterPort; PhotonNetwork.networkingPeer.Connect(bestServerFullAddress, ServerConnection.MasterServer); }
protected virtual void OnGuiSetupCloudAppId() { GUILayout.Label(CurrentLang.AppIdLabel); GUILayout.BeginHorizontal(); this.cloudAppId = EditorGUILayout.TextField(this.cloudAppId); open = GUILayout.Toggle(open, HelpIcon, GUIStyle.none, GUILayout.ExpandWidth(false)); GUILayout.EndHorizontal(); if (open) { GUILayout.Label(CurrentLang.AppIdInfoLabel); } EditorGUILayout.Separator(); GUILayout.Label(CurrentLang.CloudRegionLabel); int selectedRegion = ServerSettings.FindRegionForServerAddress(this.photonAddress); GUILayout.BeginHorizontal(); int toolbarValue = GUILayout.Toolbar(selectedRegion, cloudServerRegionNames); // the enum CloudServerRegion is converted into a string[] in init (toolbar can't use enum) helpRegion = GUILayout.Toggle(helpRegion, HelpIcon, GUIStyle.none, GUILayout.ExpandWidth(false)); GUILayout.EndHorizontal(); if (helpRegion) { GUILayout.Label(CurrentLang.RegionalServersInfo); } if (selectedRegion != toolbarValue) { //Debug.Log("Replacing region: " + selectedRegion + " with: " + toolbarValue); this.photonAddress = ServerSettings.FindServerAddressForRegion(toolbarValue); } EditorGUILayout.Separator(); GUILayout.BeginHorizontal(); if (GUILayout.Button(CurrentLang.CancelButton)) { GUIUtility.keyboardControl = 0; this.ReApplySettingsToWindow(); } if (GUILayout.Button(CurrentLang.SaveButton)) { GUIUtility.keyboardControl = 0; this.cloudAppId = this.cloudAppId.Trim(); PhotonEditor.Current.UseCloud(this.cloudAppId, selectedRegion); PhotonEditor.Save(); EditorUtility.DisplayDialog(CurrentLang.SettingsSavedTitle, CurrentLang.SettingsSavedMessage, CurrentLang.OkButton); } GUILayout.EndHorizontal(); GUILayout.Space(20); GUILayout.Label(CurrentLang.SetupOwnServerLabel); if (GUILayout.Button(CurrentLang.SelfHostSettingsButton)) { this.photonAddress = ServerSettings.DefaultServerAddress; this.photonPort = ServerSettings.DefaultMasterPort; this.photonSetupState = PhotonSetupStates.SetupSelfHosted; } EditorGUILayout.Separator(); GUILayout.Label(CurrentLang.OwnHostCloudCompareLabel); if (GUILayout.Button(CurrentLang.ComparisonPageButton)) { Application.OpenURL(UrlCompare); } }
protected virtual void OnGuiSetupCloudAppId() { GUILayout.Label("Your AppId"); GUILayout.BeginHorizontal(); this.cloudAppId = EditorGUILayout.TextField(this.cloudAppId); open = GUILayout.Toggle(open, HelpIcon, GUIStyle.none, GUILayout.ExpandWidth(false)); GUILayout.EndHorizontal(); if (open) { GUILayout.Label("The AppId a Guid that identifies your game in the Photon Cloud. Find it on your dashboard page."); } EditorGUILayout.Separator(); GUILayout.Label("Cloud Region"); int selectedRegion = ServerSettings.FindRegionForServerAddress(this.photonAddress); GUILayout.BeginHorizontal(); int toolbarValue = GUILayout.Toolbar(selectedRegion, ServerSettings.CloudServerRegionNames); helpRegion = GUILayout.Toggle(helpRegion, HelpIcon, GUIStyle.none, GUILayout.ExpandWidth(false)); GUILayout.EndHorizontal(); if (helpRegion) { GUILayout.Label("Photon Cloud has regional servers. Picking one near your customers improves ping times. You could use more than one but this setup does not support it."); } if (selectedRegion != toolbarValue) { //Debug.Log("Replacing region: " + selectedRegion + " with: " + toolbarValue); this.photonAddress = ServerSettings.FindServerAddressForRegion(toolbarValue); } EditorGUILayout.Separator(); GUILayout.BeginHorizontal(); if (GUILayout.Button("Cancel")) { GUIUtility.keyboardControl = 0; this.ReApplySettingsToWindow(); } if (GUILayout.Button("Save")) { GUIUtility.keyboardControl = 0; this.cloudAppId = this.cloudAppId.Trim(); PhotonEditor.Current.UseCloud(this.cloudAppId, selectedRegion); PhotonEditor.Save(); EditorUtility.DisplayDialog("Success", "Saved your settings.", "ok"); } GUILayout.EndHorizontal(); GUILayout.Space(20); GUILayout.Label("Running my app in the cloud was fun but...\nLet me setup my own Photon server."); if (GUILayout.Button("Open self-hosting settings")) { this.photonAddress = ServerSettings.DefaultServerAddress; this.photonPort = ServerSettings.DefaultMasterPort; this.photonSetupState = PhotonSetupStates.SetupSelfHosted; } EditorGUILayout.Separator(); GUILayout.Label("I am not quite sure how 'my own host' compares to 'cloud'."); if (GUILayout.Button("See comparison page")) { Application.OpenURL(UrlCompare); } }