ActorLocationSenderComponentSystem.cs 3.4 KB

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