TimerComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 List<long> timeOutId = new List<long>();
  28. public void Update()
  29. {
  30. if (this.timeId.Count == 0)
  31. {
  32. return;
  33. }
  34. long timeNow = TimeHelper.Now();
  35. timeOutId.Clear();
  36. while (this.timeId.Count > 0)
  37. {
  38. long k = this.timeId.FirstKey();
  39. if (k > timeNow)
  40. {
  41. break;
  42. }
  43. foreach (long ll in this.timeId[k])
  44. {
  45. this.timeOutId.Add(ll);
  46. }
  47. this.timeId.Remove(k);
  48. }
  49. foreach (long k in this.timeOutId)
  50. {
  51. Timer timer;
  52. if (!this.timers.TryGetValue(k, out timer))
  53. {
  54. continue;
  55. }
  56. this.timers.Remove(k);
  57. timer.tcs.SetResult(true);
  58. }
  59. }
  60. private void Remove(long id)
  61. {
  62. Timer timer;
  63. if (!this.timers.TryGetValue(id, out timer))
  64. {
  65. return;
  66. }
  67. this.timers.Remove(id);
  68. }
  69. public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
  70. {
  71. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  72. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  73. this.timers[timer.Id] = timer;
  74. this.timeId.Add(timer.Time, timer.Id);
  75. cancellationToken.Register(() => { this.Remove(timer.Id); });
  76. return tcs.Task;
  77. }
  78. public Task WaitTillAsync(long tillTime)
  79. {
  80. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  81. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  82. this.timers[timer.Id] = timer;
  83. this.timeId.Add(timer.Time, timer.Id);
  84. return tcs.Task;
  85. }
  86. public Task WaitAsync(long time, CancellationToken cancellationToken)
  87. {
  88. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  89. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  90. this.timers[timer.Id] = timer;
  91. this.timeId.Add(timer.Time, timer.Id);
  92. cancellationToken.Register(() => { this.Remove(timer.Id); });
  93. return tcs.Task;
  94. }
  95. public Task WaitAsync(long time)
  96. {
  97. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  98. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  99. this.timers[timer.Id] = timer;
  100. this.timeId.Add(timer.Time, timer.Id);
  101. return tcs.Task;
  102. }
  103. }
  104. }