TimerComponent.cs 3.6 KB

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