TimerComponent.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using System.Collections.Generic;
  2. namespace ET
  3. {
  4. public enum TimerClass
  5. {
  6. None,
  7. OnceTimer,
  8. OnceWaitTimer,
  9. RepeatedTimer,
  10. }
  11. public class TimerAction
  12. {
  13. public static TimerAction Create(long id, TimerClass timerClass, long startTime, long time, int type, object obj)
  14. {
  15. TimerAction timerAction = ObjectPool.Instance.Fetch<TimerAction>();
  16. timerAction.Id = id;
  17. timerAction.TimerClass = timerClass;
  18. timerAction.StartTime = startTime;
  19. timerAction.Object = obj;
  20. timerAction.Time = time;
  21. timerAction.Type = type;
  22. return timerAction;
  23. }
  24. public long Id;
  25. public TimerClass TimerClass;
  26. public object Object;
  27. public long StartTime;
  28. public long Time;
  29. public int Type;
  30. public void Recycle()
  31. {
  32. this.Id = 0;
  33. this.Object = null;
  34. this.StartTime = 0;
  35. this.Time = 0;
  36. this.TimerClass = TimerClass.None;
  37. this.Type = 0;
  38. ObjectPool.Instance.Recycle(this);
  39. }
  40. }
  41. public struct TimerCallback
  42. {
  43. public object Args;
  44. }
  45. public class TimerComponent: Singleton<TimerComponent>, ISingletonUpdate
  46. {
  47. /// <summary>
  48. /// key: time, value: timer id
  49. /// </summary>
  50. private readonly MultiMap<long, long> TimeId = new();
  51. private readonly Queue<long> timeOutTime = new();
  52. private readonly Queue<long> timeOutTimerIds = new();
  53. private readonly Dictionary<long, TimerAction> timerActions = new();
  54. private long idGenerator;
  55. // 记录最小时间,不用每次都去MultiMap取第一个值
  56. private long minTime = long.MaxValue;
  57. private long GetId()
  58. {
  59. return ++this.idGenerator;
  60. }
  61. private static long GetNow()
  62. {
  63. return TimeHelper.ClientFrameTime();
  64. }
  65. public void Update(int timeMS)
  66. {
  67. if (this.TimeId.Count == 0)
  68. {
  69. return;
  70. }
  71. long timeNow = GetNow();
  72. if (timeNow < this.minTime)
  73. {
  74. return;
  75. }
  76. foreach (KeyValuePair<long, List<long>> kv in this.TimeId)
  77. {
  78. long k = kv.Key;
  79. if (k > timeNow)
  80. {
  81. this.minTime = k;
  82. break;
  83. }
  84. this.timeOutTime.Enqueue(k);
  85. }
  86. while (this.timeOutTime.Count > 0)
  87. {
  88. long time = this.timeOutTime.Dequeue();
  89. var list = this.TimeId[time];
  90. for (int i = 0; i < list.Count; ++i)
  91. {
  92. long timerId = list[i];
  93. this.timeOutTimerIds.Enqueue(timerId);
  94. }
  95. this.TimeId.Remove(time);
  96. }
  97. if (this.TimeId.Count == 0)
  98. {
  99. this.minTime = long.MaxValue;
  100. }
  101. while (this.timeOutTimerIds.Count > 0)
  102. {
  103. long timerId = this.timeOutTimerIds.Dequeue();
  104. if (!this.timerActions.Remove(timerId, out TimerAction timerAction))
  105. {
  106. continue;
  107. }
  108. this.Run(timerAction);
  109. }
  110. }
  111. private void Run(TimerAction timerAction)
  112. {
  113. switch (timerAction.TimerClass)
  114. {
  115. case TimerClass.OnceTimer:
  116. {
  117. EventSystem.Instance.Invoke(timerAction.Type, new TimerCallback() { Args = timerAction.Object });
  118. timerAction.Recycle();
  119. break;
  120. }
  121. case TimerClass.OnceWaitTimer:
  122. {
  123. ETTask tcs = timerAction.Object as ETTask;
  124. tcs.SetResult();
  125. timerAction.Recycle();
  126. break;
  127. }
  128. case TimerClass.RepeatedTimer:
  129. {
  130. long timeNow = GetNow();
  131. timerAction.StartTime = timeNow;
  132. this.AddTimer(timerAction);
  133. EventSystem.Instance.Invoke(timerAction.Type, new TimerCallback() { Args = timerAction.Object });
  134. break;
  135. }
  136. }
  137. }
  138. private void AddTimer(TimerAction timer)
  139. {
  140. long tillTime = timer.StartTime + timer.Time;
  141. this.TimeId.Add(tillTime, timer.Id);
  142. this.timerActions.Add(timer.Id, timer);
  143. if (tillTime < this.minTime)
  144. {
  145. this.minTime = tillTime;
  146. }
  147. }
  148. public bool Remove(ref long id)
  149. {
  150. long i = id;
  151. id = 0;
  152. return this.Remove(i);
  153. }
  154. private bool Remove(long id)
  155. {
  156. if (id == 0)
  157. {
  158. return false;
  159. }
  160. if (!this.timerActions.Remove(id, out TimerAction timerAction))
  161. {
  162. return false;
  163. }
  164. timerAction.Recycle();
  165. return true;
  166. }
  167. public async ETTask WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null)
  168. {
  169. long timeNow = GetNow();
  170. if (timeNow >= tillTime)
  171. {
  172. return;
  173. }
  174. ETTask tcs = ETTask.Create(true);
  175. TimerAction timer = TimerAction.Create(this.GetId(), TimerClass.OnceWaitTimer, timeNow, tillTime - timeNow, 0, tcs);
  176. this.AddTimer(timer);
  177. long timerId = timer.Id;
  178. void CancelAction()
  179. {
  180. if (this.Remove(timerId))
  181. {
  182. tcs.SetResult();
  183. }
  184. }
  185. try
  186. {
  187. cancellationToken?.Add(CancelAction);
  188. await tcs;
  189. }
  190. finally
  191. {
  192. cancellationToken?.Remove(CancelAction);
  193. }
  194. }
  195. public async ETTask WaitFrameAsync(ETCancellationToken cancellationToken = null)
  196. {
  197. await this.WaitAsync(1, cancellationToken);
  198. }
  199. public async ETTask WaitAsync(long time, ETCancellationToken cancellationToken = null)
  200. {
  201. if (time == 0)
  202. {
  203. return;
  204. }
  205. long timeNow = GetNow();
  206. ETTask tcs = ETTask.Create(true);
  207. TimerAction timer = TimerAction.Create(this.GetId(), TimerClass.OnceWaitTimer, timeNow, time, 0, tcs);
  208. this.AddTimer(timer);
  209. long timerId = timer.Id;
  210. void CancelAction()
  211. {
  212. if (this.Remove(timerId))
  213. {
  214. tcs.SetResult();
  215. }
  216. }
  217. try
  218. {
  219. cancellationToken?.Add(CancelAction);
  220. await tcs;
  221. }
  222. finally
  223. {
  224. cancellationToken?.Remove(CancelAction);
  225. }
  226. }
  227. // 用这个优点是可以热更,缺点是回调式的写法,逻辑不连贯。WaitTillAsync不能热更,优点是逻辑连贯。
  228. // wait时间短并且逻辑需要连贯的建议WaitTillAsync
  229. // wait时间长不需要逻辑连贯的建议用NewOnceTimer
  230. public long NewOnceTimer(long tillTime, int type, object args)
  231. {
  232. long timeNow = GetNow();
  233. if (tillTime < timeNow)
  234. {
  235. Log.Error($"new once time too small: {tillTime}");
  236. }
  237. TimerAction timer = TimerAction.Create(this.GetId(), TimerClass.OnceTimer, timeNow, tillTime - timeNow, type, args);
  238. this.AddTimer(timer);
  239. return timer.Id;
  240. }
  241. public long NewFrameTimer(int type, object args)
  242. {
  243. #if DOTNET
  244. return this.NewRepeatedTimerInner(100, type, args);
  245. #else
  246. return this.NewRepeatedTimerInner(0, type, args);
  247. #endif
  248. }
  249. /// <summary>
  250. /// 创建一个RepeatedTimer
  251. /// </summary>
  252. private long NewRepeatedTimerInner(long time, int type, object args)
  253. {
  254. #if DOTNET
  255. if (time < 100)
  256. {
  257. throw new Exception($"repeated timer < 100, timerType: time: {time}");
  258. }
  259. #endif
  260. long timeNow = GetNow();
  261. TimerAction timer = TimerAction.Create(this.GetId(), TimerClass.RepeatedTimer, timeNow, time, type, args);
  262. // 每帧执行的不用加到timerId中,防止遍历
  263. this.AddTimer(timer);
  264. return timer.Id;
  265. }
  266. public long NewRepeatedTimer(long time, int type, object args)
  267. {
  268. if (time < 100)
  269. {
  270. Log.Error($"time too small: {time}");
  271. return 0;
  272. }
  273. return this.NewRepeatedTimerInner(time, type, args);
  274. }
  275. }
  276. }