Seven is the number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB

4 years ago
3 years ago
4 years ago
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="Region.cs" company="Exit Games GmbH">
  3. // Loadbalancing Framework for Photon - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Represents regions in the Photon Cloud.
  7. // </summary>
  8. // <author>developer@photonengine.com</author>
  9. // ----------------------------------------------------------------------------
  10. #if UNITY_4_7 || UNITY_5 || UNITY_5_3_OR_NEWER
  11. #define SUPPORTED_UNITY
  12. #endif
  13. namespace Photon.Realtime
  14. {
  15. using ExitGames.Client.Photon;
  16. #if SUPPORTED_UNITY || NETFX_CORE
  17. using Hashtable = ExitGames.Client.Photon.Hashtable;
  18. using SupportClass = ExitGames.Client.Photon.SupportClass;
  19. #endif
  20. public class Region
  21. {
  22. public string Code { get; private set; }
  23. /// <summary>Unlike the CloudRegionCode, this may contain cluster information.</summary>
  24. public string Cluster { get; private set; }
  25. public string HostAndPort { get; protected internal set; }
  26. public int Ping { get; set; }
  27. public bool WasPinged { get { return this.Ping != int.MaxValue; } }
  28. public Region(string code, string address)
  29. {
  30. this.SetCodeAndCluster(code);
  31. this.HostAndPort = address;
  32. this.Ping = int.MaxValue;
  33. }
  34. public Region(string code, int ping)
  35. {
  36. this.SetCodeAndCluster(code);
  37. this.Ping = ping;
  38. }
  39. private void SetCodeAndCluster(string codeAsString)
  40. {
  41. if (codeAsString == null)
  42. {
  43. this.Code = "";
  44. this.Cluster = "";
  45. return;
  46. }
  47. codeAsString = codeAsString.ToLower();
  48. int slash = codeAsString.IndexOf('/');
  49. this.Code = slash <= 0 ? codeAsString : codeAsString.Substring(0, slash);
  50. this.Cluster = slash <= 0 ? "" : codeAsString.Substring(slash+1, codeAsString.Length-slash-1);
  51. }
  52. public override string ToString()
  53. {
  54. return this.ToString(false);
  55. }
  56. public string ToString(bool compact = false)
  57. {
  58. string regionCluster = this.Code;
  59. if (!string.IsNullOrEmpty(this.Cluster))
  60. {
  61. regionCluster += "/" + this.Cluster;
  62. }
  63. if (compact)
  64. {
  65. return string.Format("{0}:{1}", regionCluster, this.Ping);
  66. }
  67. else
  68. {
  69. return string.Format("{0}[{2}]: {1}ms", regionCluster, this.Ping, this.HostAndPort);
  70. }
  71. }
  72. }
  73. }