ActorProxySystem.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 ActorProxyAwakeSystem : AwakeSystem<ActorProxy>
  10. {
  11. public override void Awake(ActorProxy self)
  12. {
  13. self.LastSendTime = TimeHelper.Now();
  14. self.tcs = null;
  15. self.FailTimes = 0;
  16. self.MaxFailTimes = 5;
  17. self.ActorId = 0;
  18. }
  19. }
  20. [ObjectSystem]
  21. public class ActorProxyAwake2System : AwakeSystem<ActorProxy, long>
  22. {
  23. public override void Awake(ActorProxy self, long actorId)
  24. {
  25. self.LastSendTime = TimeHelper.Now();
  26. self.tcs = null;
  27. self.FailTimes = 0;
  28. self.MaxFailTimes = 0;
  29. self.ActorId = actorId;
  30. }
  31. }
  32. [ObjectSystem]
  33. public class ActorProxyStartSystem : StartSystem<ActorProxy>
  34. {
  35. public override async void Start(ActorProxy self)
  36. {
  37. if (self.ActorId == 0)
  38. {
  39. self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
  40. }
  41. self.Address = Game.Scene.GetComponent<StartConfigComponent>()
  42. .Get(IdGenerater.GetAppIdFromId(self.ActorId))
  43. .GetComponent<InnerConfig>().IPEndPoint;
  44. self.UpdateAsync();
  45. }
  46. }
  47. [ObjectSystem]
  48. public class ActorProxyDestroySystem : DestroySystem<ActorProxy>
  49. {
  50. public override void Destroy(ActorProxy self)
  51. {
  52. self.LastSendTime = 0;
  53. self.Address = null;
  54. while (self.WaitingTasks.Count > 0)
  55. {
  56. ActorTask actorTask = self.WaitingTasks.Dequeue();
  57. actorTask.RunFail(ErrorCode.ERR_NotFoundActor);
  58. }
  59. self.ActorId = 0;
  60. self.FailTimes = 0;
  61. var t = self.tcs;
  62. self.tcs = null;
  63. t?.SetResult(new ActorTask());
  64. }
  65. }
  66. public static class ActorProxyEx
  67. {
  68. private static void Add(this ActorProxy self, ActorTask task)
  69. {
  70. if (self.IsDisposed)
  71. {
  72. throw new Exception("ActorProxy Disposed! dont hold actorproxy");
  73. }
  74. self.WaitingTasks.Enqueue(task);
  75. // failtimes > 0表示正在重试,这时候不能加到正在发送队列
  76. if (self.FailTimes == 0)
  77. {
  78. self.AllowGet();
  79. }
  80. }
  81. private static void AllowGet(this ActorProxy self)
  82. {
  83. if (self.tcs == null || self.WaitingTasks.Count <= 0)
  84. {
  85. return;
  86. }
  87. ActorTask task = self.WaitingTasks.Peek();
  88. var t = self.tcs;
  89. self.tcs = null;
  90. t.SetResult(task);
  91. }
  92. private static Task<ActorTask> GetAsync(this ActorProxy self)
  93. {
  94. if (self.WaitingTasks.Count > 0)
  95. {
  96. ActorTask task = self.WaitingTasks.Peek();
  97. return Task.FromResult(task);
  98. }
  99. self.tcs = new TaskCompletionSource<ActorTask>();
  100. return self.tcs.Task;
  101. }
  102. public static async void UpdateAsync(this ActorProxy self)
  103. {
  104. while (true)
  105. {
  106. ActorTask actorTask = await self.GetAsync();
  107. if (self.IsDisposed)
  108. {
  109. return;
  110. }
  111. try
  112. {
  113. await self.RunTask(actorTask);
  114. }
  115. catch (Exception e)
  116. {
  117. Log.Error(e);
  118. return;
  119. }
  120. }
  121. }
  122. private static async Task RunTask(this ActorProxy self, ActorTask task)
  123. {
  124. try
  125. {
  126. IResponse response = await task.Run();
  127. // 如果没找到Actor,重试
  128. if (response.Error == ErrorCode.ERR_NotFoundActor)
  129. {
  130. ++self.FailTimes;
  131. // 失败10次则清空actor发送队列,返回失败
  132. if (self.FailTimes > self.MaxFailTimes)
  133. {
  134. // 失败直接删除actorproxy
  135. Log.Info($"actor send message fail, actorid: {self.Id}");
  136. Game.Scene.GetComponent<ActorProxyComponent>().Remove(self.Id);
  137. return;
  138. }
  139. // 等待1s再发送
  140. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
  141. self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
  142. self.Address = Game.Scene.GetComponent<StartConfigComponent>()
  143. .Get(IdGenerater.GetAppIdFromId(self.ActorId))
  144. .GetComponent<InnerConfig>().IPEndPoint;
  145. self.AllowGet();
  146. return;
  147. }
  148. // 发送成功
  149. self.LastSendTime = TimeHelper.Now();
  150. self.FailTimes = 0;
  151. self.WaitingTasks.Dequeue();
  152. }
  153. catch (Exception e)
  154. {
  155. Log.Error(e);
  156. }
  157. }
  158. public static void Send(this ActorProxy self, IActorMessage message)
  159. {
  160. ActorTask task = new ActorTask { message = message, proxy = self };
  161. self.Add(task);
  162. }
  163. public static Task<IResponse> Call(this ActorProxy self, IActorRequest request)
  164. {
  165. ActorTask task = new ActorTask { message = request, proxy = self, Tcs = new TaskCompletionSource<IResponse>() };
  166. self.Add(task);
  167. return task.Tcs.Task;
  168. }
  169. public static string DebugQueue(this ActorProxy self, Queue<ActorTask> tasks)
  170. {
  171. string s = "";
  172. foreach (ActorTask task in tasks)
  173. {
  174. s += $" {task.message.GetType().Name}";
  175. }
  176. return s;
  177. }
  178. }
  179. }