RouterAddressComponentSystem.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Runtime.InteropServices;
  6. namespace ET.Client
  7. {
  8. [FriendOf(typeof(RouterAddressComponent))]
  9. public static class RouterAddressComponentSystem
  10. {
  11. public class RouterAddressComponentAwakeSystem: AwakeSystem<RouterAddressComponent, string>
  12. {
  13. public override void Awake(RouterAddressComponent self, string routerManagerAddress)
  14. {
  15. self.RouterManagerAddress = routerManagerAddress;
  16. }
  17. }
  18. public static async ETTask Init(this RouterAddressComponent self)
  19. {
  20. await self.GetAllRouter();
  21. }
  22. public static async ETTask GetAllRouter(this RouterAddressComponent self)
  23. {
  24. string url = $"http://{self.RouterManagerAddress}/get_router?v={RandomHelper.RandUInt32()}";
  25. Log.Debug($"start get router info: {url}");
  26. string routerInfo = await HttpClientHelper.Get(url);
  27. Log.Debug($"recv router info: {routerInfo}");
  28. HttpGetRouterResponse httpGetRouterResponse = JsonHelper.FromJson<HttpGetRouterResponse>(routerInfo);
  29. self.Info = httpGetRouterResponse;
  30. Log.Debug($"start get router info finish: {JsonHelper.ToJson(httpGetRouterResponse)}");
  31. // 打乱顺序
  32. RandomHelper.BreakRank(self.Info.Routers);
  33. self.WaitTenMinGetAllRouter().Coroutine();
  34. }
  35. // 等10分钟再获取一次
  36. public static async ETTask WaitTenMinGetAllRouter(this RouterAddressComponent self)
  37. {
  38. await TimerComponent.Instance.WaitAsync(5 * 60 * 1000);
  39. if (self.IsDisposed)
  40. {
  41. return;
  42. }
  43. await self.GetAllRouter();
  44. }
  45. public static string GetAddress(this RouterAddressComponent self)
  46. {
  47. if (self.Info.Routers.Count == 0)
  48. {
  49. return null;
  50. }
  51. return self.Info.Routers[self.RouterIndex++ % self.Info.Routers.Count];
  52. }
  53. public static string GetRealmAddress(this RouterAddressComponent self, string account)
  54. {
  55. int v = account.Mode(self.Info.Realms.Count);
  56. return self.Info.Realms[v];
  57. }
  58. }
  59. }