ActorProxy.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace Model
  7. {
  8. [ObjectSystem]
  9. public class ActorProxySystem : ObjectSystem<ActorProxy>, IAwake, IStart
  10. {
  11. public void Awake()
  12. {
  13. this.Get().Awake();
  14. }
  15. public void Start()
  16. {
  17. this.Get().Start();
  18. }
  19. }
  20. public sealed class ActorProxy : Component
  21. {
  22. // actor的地址
  23. public IPEndPoint Address;
  24. // 已发送等待回应的消息
  25. public Queue<ActorTask> RunningTasks = new Queue<ActorTask>();
  26. // 还没发送的消息
  27. public Queue<ActorTask> WaitingTasks = new Queue<ActorTask>();
  28. // 发送窗口大小
  29. public int WindowSize = 1;
  30. // 最大窗口
  31. public const int MaxWindowSize = 1;
  32. // 最近发送消息的时间
  33. public long LastSendTime;
  34. private TaskCompletionSource<ActorTask> tcs;
  35. public CancellationTokenSource CancellationTokenSource;
  36. private int failTimes;
  37. public void Awake()
  38. {
  39. this.LastSendTime = TimeHelper.Now();
  40. this.WindowSize = 1;
  41. this.tcs = null;
  42. this.CancellationTokenSource = new CancellationTokenSource();
  43. }
  44. public override void Dispose()
  45. {
  46. if (this.IsDisposed)
  47. {
  48. return;
  49. }
  50. base.Dispose();
  51. this.LastSendTime = 0;
  52. this.Address = null;
  53. this.RunningTasks.Clear();
  54. this.WaitingTasks.Clear();
  55. this.failTimes = 0;
  56. var t = this.tcs;
  57. this.tcs = null;
  58. t?.SetResult(new ActorTask());
  59. }
  60. public async void Start()
  61. {
  62. int appId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(this.Id);
  63. this.Address = Game.Scene.GetComponent<StartConfigComponent>().Get(appId).GetComponent<InnerConfig>().IPEndPoint;
  64. this.UpdateAsync();
  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. private 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(IMessage message)
  175. {
  176. ActorTask task = new ActorTask();
  177. task.message = (MessageObject)message;
  178. task.proxy = this;
  179. this.Add(task);
  180. }
  181. public Task<IResponse> Call(IRequest request)
  182. {
  183. ActorTask task = new ActorTask();
  184. task.message = (MessageObject)request;
  185. task.proxy = this;
  186. task.Tcs = new TaskCompletionSource<IResponse>();
  187. this.Add(task);
  188. return task.Tcs.Task;
  189. }
  190. public async Task<IResponse> RealCall(ActorRequest request, CancellationToken cancellationToken)
  191. {
  192. try
  193. {
  194. //Log.Debug($"realcall {MongoHelper.ToJson(request)} {this.Address}");
  195. request.Id = this.Id;
  196. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.Address);
  197. IResponse response = await session.Call(request, cancellationToken);
  198. return response;
  199. }
  200. catch (RpcException e)
  201. {
  202. Log.Error($"{this.Address} {e}");
  203. throw;
  204. }
  205. }
  206. public string DebugQueue(Queue<ActorTask> tasks)
  207. {
  208. string s = "";
  209. foreach (ActorTask task in tasks)
  210. {
  211. s += $" {task.message.GetType().Name}";
  212. }
  213. return s;
  214. }
  215. }
  216. }