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