ActorLocationSenderComponentSystem.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.IO;
  3. namespace ET
  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进行回收,过期时间是2分钟
  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(ref self.CheckTimer);
  23. }
  24. }
  25. public static class ActorLocationSenderComponentSystem
  26. {
  27. public static void Check(this ActorLocationSenderComponent self)
  28. {
  29. using (ListComponent<long> list = ListComponent<long>.Create())
  30. {
  31. long timeNow = TimeHelper.ServerNow();
  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 GetOrCreate(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.CreateWithParentAndId<ActorLocationSender>(self, id);
  57. return (ActorLocationSender) actorLocationSender;
  58. }
  59. private static void Remove(this ActorLocationSenderComponent self, long id)
  60. {
  61. if (!self.Children.TryGetValue(id, out Entity actorMessageSender))
  62. {
  63. return;
  64. }
  65. actorMessageSender.Dispose();
  66. }
  67. public static void Send(this ActorLocationSenderComponent self, long entityId, IActorRequest message)
  68. {
  69. self.Call(entityId, message).Coroutine();
  70. }
  71. public static async ETTask<IActorResponse> Call(this ActorLocationSenderComponent self, long entityId, IActorRequest iActorRequest)
  72. {
  73. ActorLocationSender actorLocationSender = self.GetOrCreate(entityId);
  74. // 先序列化好
  75. int rpcId = ActorMessageSenderComponent.Instance.GetRpcId();
  76. iActorRequest.RpcId = rpcId;
  77. (ushort _, MemoryStream stream) = MessageSerializeHelper.MessageToStream(0, iActorRequest);
  78. long actorLocationSenderInstanceId = actorLocationSender.InstanceId;
  79. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ActorLocationSender, entityId))
  80. {
  81. if (actorLocationSender.InstanceId != actorLocationSenderInstanceId)
  82. {
  83. throw new RpcException(ErrorCode.ERR_ActorTimeout, $"{stream.ToActorMessage()}");
  84. }
  85. // 队列中没处理的消息返回跟上个消息一样的报错
  86. if (actorLocationSender.Error == ErrorCode.ERR_NotFoundActor)
  87. {
  88. return ActorHelper.CreateResponse(iActorRequest, actorLocationSender.Error);
  89. }
  90. try
  91. {
  92. return await self.CallInner(actorLocationSender, rpcId, stream);
  93. }
  94. catch (RpcException)
  95. {
  96. self.Remove(actorLocationSender.Id);
  97. throw;
  98. }
  99. catch (Exception e)
  100. {
  101. self.Remove(actorLocationSender.Id);
  102. throw new Exception($"{stream.ToActorMessage()}", e);
  103. }
  104. }
  105. }
  106. private static async ETTask<IActorResponse> CallInner(this ActorLocationSenderComponent self, ActorLocationSender actorLocationSender, int rpcId, MemoryStream memoryStream)
  107. {
  108. int failTimes = 0;
  109. long instanceId = actorLocationSender.InstanceId;
  110. actorLocationSender.LastSendOrRecvTime = TimeHelper.ServerNow();
  111. while (true)
  112. {
  113. if (actorLocationSender.ActorId == 0)
  114. {
  115. actorLocationSender.ActorId = await LocationProxyComponent.Instance.Get(actorLocationSender.Id);
  116. if (actorLocationSender.InstanceId != instanceId)
  117. {
  118. throw new RpcException(ErrorCode.ERR_ActorLocationSenderTimeout2, $"{memoryStream.ToActorMessage()}");
  119. }
  120. }
  121. if (actorLocationSender.ActorId == 0)
  122. {
  123. IActorRequest iActorRequest = (IActorRequest)memoryStream.ToActorMessage();
  124. return ActorHelper.CreateResponse(iActorRequest, ErrorCode.ERR_NotFoundActor);
  125. }
  126. IActorResponse response = await ActorMessageSenderComponent.Instance.Call(actorLocationSender.ActorId, rpcId, memoryStream, false);
  127. if (actorLocationSender.InstanceId != instanceId)
  128. {
  129. throw new RpcException(ErrorCode.ERR_ActorLocationSenderTimeout3, $"{memoryStream.ToActorMessage()}");
  130. }
  131. switch (response.Error)
  132. {
  133. case ErrorCode.ERR_NotFoundActor:
  134. {
  135. // 如果没找到Actor,重试
  136. ++failTimes;
  137. if (failTimes > 20)
  138. {
  139. Log.Debug($"actor send message fail, actorid: {actorLocationSender.Id}");
  140. actorLocationSender.Error = ErrorCode.ERR_NotFoundActor;
  141. // 这里不能删除actor,要让后面等待发送的消息也返回ERR_NotFoundActor,直到超时删除
  142. return response;
  143. }
  144. // 等待0.5s再发送
  145. await TimerComponent.Instance.WaitAsync(500);
  146. if (actorLocationSender.InstanceId != instanceId)
  147. {
  148. throw new RpcException(ErrorCode.ERR_ActorLocationSenderTimeout4, $"{memoryStream.ToActorMessage()}");
  149. }
  150. actorLocationSender.ActorId = 0;
  151. continue;
  152. }
  153. case ErrorCode.ERR_ActorNoMailBoxComponent:
  154. case ErrorCode.ERR_ActorTimeout:
  155. {
  156. throw new RpcException(response.Error, $"{memoryStream.ToActorMessage()}");
  157. }
  158. }
  159. if (ErrorCode.IsRpcNeedThrowException(response.Error))
  160. {
  161. throw new RpcException(response.Error, $"Message: {response.Message} Request: {memoryStream.ToActorMessage()}");
  162. }
  163. return response;
  164. }
  165. }
  166. }
  167. }