TimerComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. namespace ETModel
  4. {
  5. public struct Timer
  6. {
  7. public long Id { get; set; }
  8. public long Time { get; set; }
  9. public ETTaskCompletionSource tcs;
  10. }
  11. [ObjectSystem]
  12. public class TimerComponentUpdateSystem : UpdateSystem<TimerComponent>
  13. {
  14. public override void Update(TimerComponent self)
  15. {
  16. self.Update();
  17. }
  18. }
  19. public class TimerComponent : Component
  20. {
  21. private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
  22. /// <summary>
  23. /// key: time, value: timer id
  24. /// </summary>
  25. private readonly MultiMap<long, long> timeId = new MultiMap<long, long>();
  26. private readonly Queue<long> timeOutTime = new Queue<long>();
  27. private readonly Queue<long> timeOutTimerIds = new Queue<long>();
  28. // 记录最小时间,不用每次都去MultiMap取第一个值
  29. private long minTime;
  30. public void Update()
  31. {
  32. if (this.timeId.Count == 0)
  33. {
  34. return;
  35. }
  36. long timeNow = TimeHelper.Now();
  37. if (timeNow < this.minTime)
  38. {
  39. return;
  40. }
  41. foreach (KeyValuePair<long, List<long>> kv in this.timeId.GetDictionary())
  42. {
  43. long k = kv.Key;
  44. if (k > timeNow)
  45. {
  46. minTime = k;
  47. break;
  48. }
  49. this.timeOutTime.Enqueue(k);
  50. }
  51. while(this.timeOutTime.Count > 0)
  52. {
  53. long time = this.timeOutTime.Dequeue();
  54. foreach(long timerId in this.timeId[time])
  55. {
  56. this.timeOutTimerIds.Enqueue(timerId);
  57. }
  58. this.timeId.Remove(time);
  59. }
  60. while(this.timeOutTimerIds.Count > 0)
  61. {
  62. long timerId = this.timeOutTimerIds.Dequeue();
  63. Timer timer;
  64. if (!this.timers.TryGetValue(timerId, out timer))
  65. {
  66. continue;
  67. }
  68. this.timers.Remove(timerId);
  69. timer.tcs.SetResult();
  70. }
  71. }
  72. private void Remove(long id)
  73. {
  74. this.timers.Remove(id);
  75. }
  76. public ETTask WaitTillAsync(long tillTime, CancellationToken cancellationToken)
  77. {
  78. ETTaskCompletionSource tcs = new ETTaskCompletionSource();
  79. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  80. this.timers[timer.Id] = timer;
  81. this.timeId.Add(timer.Time, timer.Id);
  82. if (timer.Time < this.minTime)
  83. {
  84. this.minTime = timer.Time;
  85. }
  86. cancellationToken.Register(() => { this.Remove(timer.Id); });
  87. return tcs.Task;
  88. }
  89. public ETTask WaitTillAsync(long tillTime)
  90. {
  91. ETTaskCompletionSource tcs = new ETTaskCompletionSource();
  92. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  93. this.timers[timer.Id] = timer;
  94. this.timeId.Add(timer.Time, timer.Id);
  95. if (timer.Time < this.minTime)
  96. {
  97. this.minTime = timer.Time;
  98. }
  99. return tcs.Task;
  100. }
  101. public ETTask WaitAsync(long time, CancellationToken cancellationToken)
  102. {
  103. ETTaskCompletionSource tcs = new ETTaskCompletionSource();
  104. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  105. this.timers[timer.Id] = timer;
  106. this.timeId.Add(timer.Time, timer.Id);
  107. if (timer.Time < this.minTime)
  108. {
  109. this.minTime = timer.Time;
  110. }
  111. cancellationToken.Register(() => { this.Remove(timer.Id); });
  112. return tcs.Task;
  113. }
  114. public ETTask WaitAsync(long time)
  115. {
  116. ETTaskCompletionSource tcs = new ETTaskCompletionSource();
  117. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  118. this.timers[timer.Id] = timer;
  119. this.timeId.Add(timer.Time, timer.Id);
  120. if (timer.Time < this.minTime)
  121. {
  122. this.minTime = timer.Time;
  123. }
  124. return tcs.Task;
  125. }
  126. }
  127. }