TimerComponent.cs 2.9 KB

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