ActorLocationSenderSystem.cs 5.8 KB

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