ActorProxySystem.cs 4.3 KB

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