ActorLocationSenderSystem.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using ETModel;
  3. namespace ETHotfix
  4. {
  5. [ObjectSystem]
  6. public class ActorLocationSenderAwakeSystem : AwakeSystem<ActorLocationSender>
  7. {
  8. public override void Awake(ActorLocationSender self)
  9. {
  10. self.LastSendTime = TimeHelper.Now();
  11. self.Tcs = null;
  12. self.FailTimes = 0;
  13. self.ActorId = 0;
  14. }
  15. }
  16. [ObjectSystem]
  17. public class ActorLocationSenderStartSystem : StartSystem<ActorLocationSender>
  18. {
  19. public override void Start(ActorLocationSender self)
  20. {
  21. StartAsync(self).Coroutine();
  22. }
  23. public async ETVoid StartAsync(ActorLocationSender self)
  24. {
  25. self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
  26. self.Address = StartConfigComponent.Instance.GetInnerAddress(IdGenerater.GetAppIdFromId(self.ActorId));
  27. self.UpdateAsync().Coroutine();
  28. }
  29. }
  30. [ObjectSystem]
  31. public class ActorLocationSenderDestroySystem : DestroySystem<ActorLocationSender>
  32. {
  33. public override void Destroy(ActorLocationSender self)
  34. {
  35. self.RunError(ErrorCode.ERR_ActorRemove);
  36. self.Id = 0;
  37. self.LastSendTime = 0;
  38. self.Address = null;
  39. self.ActorId = 0;
  40. self.FailTimes = 0;
  41. self.Tcs = null;
  42. }
  43. }
  44. public static class ActorLocationSenderHelper
  45. {
  46. private static void Add(this ActorLocationSender self, ActorTask task)
  47. {
  48. if (self.IsDisposed)
  49. {
  50. throw new Exception("ActorLocationSender Disposed! dont hold ActorMessageSender");
  51. }
  52. self.WaitingTasks.Enqueue(task);
  53. // failtimes > 0表示正在重试,这时候不能加到正在发送队列
  54. if (self.FailTimes == 0)
  55. {
  56. self.AllowGet();
  57. }
  58. }
  59. public static void RunError(this ActorLocationSender self, int errorCode)
  60. {
  61. while (self.WaitingTasks.Count > 0)
  62. {
  63. ActorTask actorTask = self.WaitingTasks.Dequeue();
  64. actorTask.Tcs?.SetException(new RpcException(errorCode, ""));
  65. }
  66. self.WaitingTasks.Clear();
  67. }
  68. private static void AllowGet(this ActorLocationSender self)
  69. {
  70. if (self.Tcs == null || self.WaitingTasks.Count <= 0)
  71. {
  72. return;
  73. }
  74. ActorTask task = self.WaitingTasks.Peek();
  75. var t = self.Tcs;
  76. self.Tcs = null;
  77. t.SetResult(task);
  78. }
  79. private static ETTask<ActorTask> GetAsync(this ActorLocationSender self)
  80. {
  81. if (self.WaitingTasks.Count > 0)
  82. {
  83. ActorTask task = self.WaitingTasks.Peek();
  84. return ETTask.FromResult(task);
  85. }
  86. self.Tcs = new ETTaskCompletionSource<ActorTask>();
  87. return self.Tcs.Task;
  88. }
  89. public static async ETVoid UpdateAsync(this ActorLocationSender self)
  90. {
  91. try
  92. {
  93. long instanceId = self.InstanceId;
  94. while (true)
  95. {
  96. if (self.InstanceId != instanceId)
  97. {
  98. return;
  99. }
  100. ActorTask actorTask = await self.GetAsync();
  101. if (self.InstanceId != instanceId)
  102. {
  103. return;
  104. }
  105. if (actorTask.ActorRequest == null)
  106. {
  107. return;
  108. }
  109. await self.RunTask(actorTask);
  110. }
  111. }
  112. catch (Exception e)
  113. {
  114. Log.Error(e);
  115. }
  116. }
  117. private static async ETTask RunTask(this ActorLocationSender self, ActorTask task)
  118. {
  119. ActorMessageSender actorMessageSender = Game.Scene.GetComponent<ActorMessageSenderComponent>().Get(self.ActorId);
  120. IActorResponse response = await actorMessageSender.Call(task.ActorRequest);
  121. // 发送成功
  122. switch (response.Error)
  123. {
  124. case ErrorCode.ERR_NotFoundActor:
  125. // 如果没找到Actor,重试
  126. ++self.FailTimes;
  127. // 失败MaxFailTimes次则清空actor发送队列,返回失败
  128. if (self.FailTimes > ActorLocationSender.MaxFailTimes)
  129. {
  130. // 失败直接删除actorproxy
  131. Log.Info($"actor send message fail, actorid: {self.Id}");
  132. self.RunError(response.Error);
  133. self.GetParent<ActorLocationSenderComponent>().Remove(self.Id);
  134. return;
  135. }
  136. // 等待0.5s再发送
  137. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(500);
  138. self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
  139. self.Address = StartConfigComponent.Instance.GetInnerAddress(IdGenerater.GetAppIdFromId(self.ActorId));
  140. self.AllowGet();
  141. return;
  142. case ErrorCode.ERR_ActorNoMailBoxComponent:
  143. self.RunError(response.Error);
  144. self.GetParent<ActorLocationSenderComponent>().Remove(self.Id);
  145. return;
  146. default:
  147. self.LastSendTime = TimeHelper.Now();
  148. self.FailTimes = 0;
  149. self.WaitingTasks.Dequeue();
  150. if (task.Tcs == null)
  151. {
  152. return;
  153. }
  154. IActorLocationResponse actorLocationResponse = response as IActorLocationResponse;
  155. if (actorLocationResponse == null)
  156. {
  157. task.Tcs.SetException(new Exception($"actor location respose is not IActorLocationResponse, but is: {response.GetType().Name}"));
  158. }
  159. task.Tcs.SetResult(actorLocationResponse);
  160. return;
  161. }
  162. }
  163. public static void Send(this ActorLocationSender self, IActorLocationMessage request)
  164. {
  165. if (request == null)
  166. {
  167. throw new Exception($"actor location send message is null");
  168. }
  169. ActorTask task = new ActorTask(request);
  170. self.Add(task);
  171. }
  172. public static ETTask<IActorLocationResponse> Call(this ActorLocationSender self, IActorLocationRequest request)
  173. {
  174. if (request == null)
  175. {
  176. throw new Exception($"actor location call message is null");
  177. }
  178. ETTaskCompletionSource<IActorLocationResponse> tcs = new ETTaskCompletionSource<IActorLocationResponse>();
  179. ActorTask task = new ActorTask(request, tcs);
  180. self.Add(task);
  181. return task.Tcs.Task;
  182. }
  183. }
  184. }