TimerComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace Model
  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. [ObjectEvent]
  13. public class TimerComponentEvent : ObjectEvent<TimerComponent>, IUpdate
  14. {
  15. public void Update()
  16. {
  17. this.Get().Update();
  18. }
  19. }
  20. public class TimerComponent: Component, IUpdate
  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 EQueue<long> timeoutTimer = new EQueue<long>();
  28. public void Update()
  29. {
  30. long timeNow = TimeHelper.Now();
  31. foreach (long time in this.timeId.Keys)
  32. {
  33. if (time > timeNow)
  34. {
  35. break;
  36. }
  37. this.timeoutTimer.Enqueue(time);
  38. }
  39. while (this.timeoutTimer.Count > 0)
  40. {
  41. long key = this.timeoutTimer.Dequeue();
  42. List<long> timeOutId = this.timeId[key];
  43. foreach (long id in timeOutId)
  44. {
  45. Timer timer;
  46. if (!this.timers.TryGetValue(id, out timer))
  47. {
  48. continue;
  49. }
  50. timer.tcs.SetResult(true);
  51. }
  52. this.timeId.Remove(key);
  53. }
  54. }
  55. private void Remove(long id)
  56. {
  57. Timer timer;
  58. if (!this.timers.TryGetValue(id, out timer))
  59. {
  60. return;
  61. }
  62. this.timers.Remove(id);
  63. }
  64. public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
  65. {
  66. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  67. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  68. this.timers[timer.Id] = timer;
  69. this.timeId.Add(timer.Time, timer.Id);
  70. cancellationToken.Register(() => { this.Remove(timer.Id); });
  71. return tcs.Task;
  72. }
  73. public Task WaitTillAsync(long tillTime)
  74. {
  75. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  76. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  77. this.timers[timer.Id] = timer;
  78. this.timeId.Add(timer.Time, timer.Id);
  79. return tcs.Task;
  80. }
  81. public Task WaitAsync(long time, CancellationToken cancellationToken)
  82. {
  83. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  84. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  85. this.timers[timer.Id] = timer;
  86. this.timeId.Add(timer.Time, timer.Id);
  87. cancellationToken.Register(() => { this.Remove(timer.Id); });
  88. return tcs.Task;
  89. }
  90. public Task WaitAsync(long time)
  91. {
  92. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  93. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  94. this.timers[timer.Id] = timer;
  95. this.timeId.Add(timer.Time, timer.Id);
  96. return tcs.Task;
  97. }
  98. }
  99. }