TimerComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace Model
  5. {
  6. [ObjectEvent]
  7. public class TimerComponentEvent: ObjectEvent<TimerComponent>, IUpdate
  8. {
  9. public void Update()
  10. {
  11. this.Get().Update();
  12. }
  13. }
  14. public class Timer
  15. {
  16. public long Id { get; set; }
  17. public long Time { get; set; }
  18. public TaskCompletionSource<bool> tcs;
  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> timeoutTimer = new Queue<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. long[] timeOutId = this.timeId.GetAll(key);
  43. foreach (long id in timeOutId)
  44. {
  45. if (!this.timers.TryGetValue(id, out Timer timer))
  46. {
  47. continue;
  48. }
  49. this.Remove(id);
  50. timer.tcs.SetResult(true);
  51. }
  52. }
  53. }
  54. private void Remove(long id)
  55. {
  56. if (!this.timers.TryGetValue(id, out Timer timer))
  57. {
  58. return;
  59. }
  60. this.timers.Remove(id);
  61. this.timeId.Remove(timer.Time, timer.Id);
  62. }
  63. public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
  64. {
  65. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  66. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  67. this.timers[timer.Id] = timer;
  68. this.timeId.Add(timer.Time, timer.Id);
  69. cancellationToken.Register(() => { this.Remove(timer.Id); });
  70. return tcs.Task;
  71. }
  72. public Task WaitTillAsync(long tillTime)
  73. {
  74. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  75. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  76. this.timers[timer.Id] = timer;
  77. this.timeId.Add(timer.Time, timer.Id);
  78. return tcs.Task;
  79. }
  80. public Task WaitAsync(long time, CancellationToken cancellationToken)
  81. {
  82. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  83. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  84. this.timers[timer.Id] = timer;
  85. this.timeId.Add(timer.Time, timer.Id);
  86. cancellationToken.Register(() => { this.Remove(timer.Id); });
  87. return tcs.Task;
  88. }
  89. public Task WaitAsync(long time)
  90. {
  91. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  92. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  93. this.timers[timer.Id] = timer;
  94. this.timeId.Add(timer.Time, timer.Id);
  95. return tcs.Task;
  96. }
  97. }
  98. }