ActorProxy.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace ETModel
  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. public sealed class ActorProxy : Component
  27. {
  28. // actor的地址
  29. public IPEndPoint Address;
  30. // 已发送等待回应的消息
  31. public Queue<ActorTask> RunningTasks = new Queue<ActorTask>();
  32. // 还没发送的消息
  33. public Queue<ActorTask> WaitingTasks = new Queue<ActorTask>();
  34. // 发送窗口大小
  35. public int WindowSize = 1;
  36. // 最大窗口
  37. public const int MaxWindowSize = 1;
  38. // 最近发送消息的时间
  39. public long LastSendTime;
  40. private TaskCompletionSource<ActorTask> tcs;
  41. public CancellationTokenSource CancellationTokenSource;
  42. private int failTimes;
  43. public void Awake()
  44. {
  45. this.LastSendTime = TimeHelper.Now();
  46. this.WindowSize = 1;
  47. this.tcs = null;
  48. this.CancellationTokenSource = new CancellationTokenSource();
  49. }
  50. public override void Dispose()
  51. {
  52. if (this.IsDisposed)
  53. {
  54. return;
  55. }
  56. base.Dispose();
  57. this.LastSendTime = 0;
  58. this.Address = null;
  59. this.RunningTasks.Clear();
  60. this.WaitingTasks.Clear();
  61. this.failTimes = 0;
  62. var t = this.tcs;
  63. this.tcs = null;
  64. t?.SetResult(new ActorTask());
  65. }
  66. private void Add(ActorTask task)
  67. {
  68. if (this.IsDisposed)
  69. {
  70. throw new Exception("ActorProxy Disposed! dont hold actorproxy");
  71. }
  72. this.WaitingTasks.Enqueue(task);
  73. // failtimes > 0表示正在重试,这时候不能加到正在发送队列
  74. if (this.failTimes == 0)
  75. {
  76. this.AllowGet();
  77. }
  78. }
  79. private void AllowGet()
  80. {
  81. if (this.tcs == null || this.WaitingTasks.Count <= 0 || this.RunningTasks.Count >= this.WindowSize)
  82. {
  83. return;
  84. }
  85. ActorTask task = this.WaitingTasks.Dequeue();
  86. this.RunningTasks.Enqueue(task);
  87. var t = this.tcs;
  88. this.tcs = null;
  89. t.SetResult(task);
  90. }
  91. private Task<ActorTask> GetAsync()
  92. {
  93. if (this.WaitingTasks.Count > 0)
  94. {
  95. ActorTask task = this.WaitingTasks.Dequeue();
  96. this.RunningTasks.Enqueue(task);
  97. return Task.FromResult(task);
  98. }
  99. this.tcs = new TaskCompletionSource<ActorTask>();
  100. return this.tcs.Task;
  101. }
  102. public async void UpdateAsync()
  103. {
  104. while (true)
  105. {
  106. ActorTask actorTask = await this.GetAsync();
  107. if (this.IsDisposed)
  108. {
  109. return;
  110. }
  111. try
  112. {
  113. this.RunTask(actorTask);
  114. }
  115. catch (Exception e)
  116. {
  117. Log.Error(e.ToString());
  118. return;
  119. }
  120. }
  121. }
  122. private async void RunTask(ActorTask task)
  123. {
  124. try
  125. {
  126. IResponse response = await task.Run();
  127. // 如果没找到Actor,发送窗口减少为1,重试
  128. if (response.Error == ErrorCode.ERR_NotFoundActor)
  129. {
  130. this.CancellationTokenSource.Cancel();
  131. this.WindowSize = 1;
  132. ++this.failTimes;
  133. while (this.WaitingTasks.Count > 0)
  134. {
  135. ActorTask actorTask = this.WaitingTasks.Dequeue();
  136. this.RunningTasks.Enqueue(actorTask);
  137. }
  138. ObjectHelper.Swap(ref this.RunningTasks, ref this.WaitingTasks);
  139. // 失败3次则清空actor发送队列,返回失败
  140. if (this.failTimes > 3)
  141. {
  142. while (this.WaitingTasks.Count > 0)
  143. {
  144. ActorTask actorTask = this.WaitingTasks.Dequeue();
  145. actorTask.RunFail(response.Error);
  146. }
  147. // 失败直接删除actorproxy
  148. Game.Scene.GetComponent<ActorProxyComponent>().Remove(this.Id);
  149. return;
  150. }
  151. // 等待一会再发送
  152. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(this.failTimes * 500);
  153. int appId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(this.Id);
  154. this.Address = Game.Scene.GetComponent<StartConfigComponent>().Get(appId).GetComponent<InnerConfig>().IPEndPoint;
  155. this.CancellationTokenSource = new CancellationTokenSource();
  156. this.AllowGet();
  157. return;
  158. }
  159. // 发送成功
  160. this.LastSendTime = TimeHelper.Now();
  161. this.failTimes = 0;
  162. if (this.WindowSize < MaxWindowSize)
  163. {
  164. ++this.WindowSize;
  165. }
  166. this.RunningTasks.Dequeue();
  167. this.AllowGet();
  168. }
  169. catch (Exception e)
  170. {
  171. Log.Error(e.ToString());
  172. }
  173. }
  174. public void Send(IActorMessage message)
  175. {
  176. ActorTask task = new ActorTask
  177. {
  178. message = message,
  179. proxy = this
  180. };
  181. this.Add(task);
  182. }
  183. public Task<IResponse> Call(IActorRequest request)
  184. {
  185. ActorTask task = new ActorTask
  186. {
  187. message = request,
  188. proxy = this,
  189. Tcs = new TaskCompletionSource<IResponse>()
  190. };
  191. this.Add(task);
  192. return task.Tcs.Task;
  193. }
  194. public string DebugQueue(Queue<ActorTask> tasks)
  195. {
  196. string s = "";
  197. foreach (ActorTask task in tasks)
  198. {
  199. s += $" {task.message.GetType().Name}";
  200. }
  201. return s;
  202. }
  203. }
  204. }