ActorLocationSenderComponentSystem.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. namespace ET
  3. {
  4. [ObjectSystem]
  5. public class ActorLocationSenderComponentAwakeSystem : AwakeSystem<ActorLocationSenderComponent>
  6. {
  7. public override void Awake(ActorLocationSenderComponent self)
  8. {
  9. ActorLocationSenderComponent.Instance = self;
  10. // 每10s扫描一次过期的actorproxy进行回收,过期时间是1分钟
  11. // 可能由于bug或者进程挂掉,导致ActorLocationSender发送的消息没有确认,结果无法自动删除,每一分钟清理一次这种ActorLocationSender
  12. self.CheckTimer = TimerComponent.Instance.NewRepeatedTimer(10 * 1000, self.Check);
  13. }
  14. }
  15. [ObjectSystem]
  16. public class ActorLocationSenderComponentDestroySystem: DestroySystem<ActorLocationSenderComponent>
  17. {
  18. public override void Destroy(ActorLocationSenderComponent self)
  19. {
  20. ActorLocationSenderComponent.Instance = null;
  21. TimerComponent.Instance.Remove(self.CheckTimer);
  22. self.CheckTimer = 0;
  23. }
  24. }
  25. public static class ActorLocationSenderComponentSystem
  26. {
  27. public static void Check(this ActorLocationSenderComponent self, bool isTimeOut)
  28. {
  29. using (ListComponent<long> list = EntityFactory.Create<ListComponent<long>>(self.Domain))
  30. {
  31. long timeNow = TimeHelper.Now();
  32. foreach ((long key, Entity value) in self.Children)
  33. {
  34. ActorLocationSender actorLocationMessageSender = (ActorLocationSender) value;
  35. if (timeNow > actorLocationMessageSender.LastSendOrRecvTime + ActorLocationSenderComponent.TIMEOUT_TIME)
  36. {
  37. list.List.Add(key);
  38. }
  39. }
  40. foreach (long id in list.List)
  41. {
  42. self.Remove(id);
  43. }
  44. }
  45. }
  46. private static ActorLocationSender Get(this ActorLocationSenderComponent self, long id)
  47. {
  48. if (id == 0)
  49. {
  50. throw new Exception($"actor id is 0");
  51. }
  52. if (self.Children.TryGetValue(id, out Entity actorLocationSender))
  53. {
  54. return (ActorLocationSender)actorLocationSender;
  55. }
  56. actorLocationSender = EntityFactory.CreateWithId<ActorLocationSender>(self.Domain, id);
  57. actorLocationSender.Parent = self;
  58. return (ActorLocationSender)actorLocationSender;
  59. }
  60. private static void Remove(this ActorLocationSenderComponent self, long id)
  61. {
  62. if (!self.Children.TryGetValue(id, out Entity actorMessageSender))
  63. {
  64. return;
  65. }
  66. actorMessageSender.Dispose();
  67. }
  68. public static void Send(this ActorLocationSenderComponent self, long entityId, IActorLocationMessage message)
  69. {
  70. ActorLocationSender actorLocationSender = self.Get(entityId);
  71. actorLocationSender.Send(message);
  72. }
  73. public static async ETTask<IActorResponse> Call(this ActorLocationSenderComponent self, long entityId, IActorLocationRequest message)
  74. {
  75. ActorLocationSender actorLocationSender = self.Get(entityId);
  76. return await actorLocationSender.Call(message);
  77. }
  78. }
  79. }