RouterAddressComponentSystem.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. namespace ET.Client
  6. {
  7. [FriendOf(typeof(RouterAddressComponent))]
  8. public static class RouterAddressComponentSystem
  9. {
  10. public class RouterAddressComponentAwakeSystem: AwakeSystem<RouterAddressComponent, string, int>
  11. {
  12. protected override void Awake(RouterAddressComponent self, string address, int port)
  13. {
  14. self.RouterManagerHost = address;
  15. self.RouterManagerPort = port;
  16. }
  17. }
  18. public static async ETTask Init(this RouterAddressComponent self)
  19. {
  20. self.RouterManagerIPAddress = NetworkHelper.GetHostAddress(self.RouterManagerHost);
  21. await self.GetAllRouter();
  22. }
  23. private static async ETTask GetAllRouter(this RouterAddressComponent self)
  24. {
  25. string url = $"http://{self.RouterManagerHost}:{self.RouterManagerPort}/get_router?v={RandomGenerator.RandUInt32()}";
  26. Log.Debug($"start get router info: {url}");
  27. string routerInfo = await HttpClientHelper.Get(url);
  28. Log.Debug($"recv router info: {routerInfo}");
  29. HttpGetRouterResponse httpGetRouterResponse = JsonHelper.FromJson<HttpGetRouterResponse>(routerInfo);
  30. self.Info = httpGetRouterResponse;
  31. Log.Debug($"start get router info finish: {JsonHelper.ToJson(httpGetRouterResponse)}");
  32. // 打乱顺序
  33. RandomGenerator.BreakRank(self.Info.Routers);
  34. self.WaitTenMinGetAllRouter().Coroutine();
  35. }
  36. // 等10分钟再获取一次
  37. public static async ETTask WaitTenMinGetAllRouter(this RouterAddressComponent self)
  38. {
  39. await TimerComponent.Instance.WaitAsync(5 * 60 * 1000);
  40. if (self.IsDisposed)
  41. {
  42. return;
  43. }
  44. await self.GetAllRouter();
  45. }
  46. public static IPEndPoint GetAddress(this RouterAddressComponent self)
  47. {
  48. if (self.Info.Routers.Count == 0)
  49. {
  50. return null;
  51. }
  52. string address = self.Info.Routers[self.RouterIndex++ % self.Info.Routers.Count];
  53. string[] ss = address.Split(':');
  54. IPAddress ipAddress = IPAddress.Parse(ss[0]);
  55. if (self.RouterManagerIPAddress.AddressFamily == AddressFamily.InterNetworkV6)
  56. {
  57. ipAddress = ipAddress.MapToIPv6();
  58. }
  59. return new IPEndPoint(ipAddress, int.Parse(ss[1]));
  60. }
  61. public static IPEndPoint GetRealmAddress(this RouterAddressComponent self, string account)
  62. {
  63. int v = account.Mode(self.Info.Realms.Count);
  64. string address = self.Info.Realms[v];
  65. string[] ss = address.Split(':');
  66. IPAddress ipAddress = IPAddress.Parse(ss[0]);
  67. //if (self.IPAddress.AddressFamily == AddressFamily.InterNetworkV6)
  68. //{
  69. // ipAddress = ipAddress.MapToIPv6();
  70. //}
  71. return new IPEndPoint(ipAddress, int.Parse(ss[1]));
  72. }
  73. }
  74. }