TimerComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Base;
  5. namespace Model
  6. {
  7. [ObjectEvent]
  8. public class TimerComponentEvent: ObjectEvent<TimerComponent>, IUpdate
  9. {
  10. public void Update()
  11. {
  12. this.Get().Update();
  13. }
  14. }
  15. public class Timer
  16. {
  17. public long Id { get; set; }
  18. public long Time { get; set; }
  19. public TaskCompletionSource<bool> tcs;
  20. }
  21. public class TimerComponent: Component
  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. if (!this.timers.TryGetValue(id, out Timer timer))
  47. {
  48. continue;
  49. }
  50. this.Remove(id);
  51. timer.tcs.SetResult(true);
  52. }
  53. }
  54. }
  55. private void Remove(long id)
  56. {
  57. if (!this.timers.TryGetValue(id, out Timer timer))
  58. {
  59. return;
  60. }
  61. this.timers.Remove(id);
  62. this.timeId.Remove(timer.Time, timer.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. }