Region.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /// <summary>Weighted ping time.</summary>
  27. /// <remarks>
  28. /// Regions gets pinged 5 times (RegionPinger.Attempts).
  29. /// Out of those, the worst rtt is discarded and the best will be counted two times for a weighted average.
  30. /// </remarks>
  31. public int Ping { get; set; }
  32. public bool WasPinged { get { return this.Ping != int.MaxValue; } }
  33. public Region(string code, string address)
  34. {
  35. this.SetCodeAndCluster(code);
  36. this.HostAndPort = address;
  37. this.Ping = int.MaxValue;
  38. }
  39. public Region(string code, int ping)
  40. {
  41. this.SetCodeAndCluster(code);
  42. this.Ping = ping;
  43. }
  44. private void SetCodeAndCluster(string codeAsString)
  45. {
  46. if (codeAsString == null)
  47. {
  48. this.Code = "";
  49. this.Cluster = "";
  50. return;
  51. }
  52. codeAsString = codeAsString.ToLower();
  53. int slash = codeAsString.IndexOf('/');
  54. this.Code = slash <= 0 ? codeAsString : codeAsString.Substring(0, slash);
  55. this.Cluster = slash <= 0 ? "" : codeAsString.Substring(slash+1, codeAsString.Length-slash-1);
  56. }
  57. public override string ToString()
  58. {
  59. return this.ToString(false);
  60. }
  61. public string ToString(bool compact = false)
  62. {
  63. string regionCluster = this.Code;
  64. if (!string.IsNullOrEmpty(this.Cluster))
  65. {
  66. regionCluster += "/" + this.Cluster;
  67. }
  68. if (compact)
  69. {
  70. return string.Format("{0}:{1}", regionCluster, this.Ping);
  71. }
  72. else
  73. {
  74. return string.Format("{0}[{2}]: {1}ms", regionCluster, this.Ping, this.HostAndPort);
  75. }
  76. }
  77. }
  78. }