TimerComponent.cs 2.7 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
  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. public void Update()
  28. {
  29. long timeNow = TimeHelper.Now();
  30. while (true)
  31. {
  32. if (this.timeId.Count <= 0)
  33. {
  34. return;
  35. }
  36. var kv = this.timeId.First();
  37. if (kv.Key > timeNow)
  38. {
  39. break;
  40. }
  41. List<long> timeOutId = kv.Value;
  42. foreach (long id in timeOutId)
  43. {
  44. Timer timer;
  45. if (!this.timers.TryGetValue(id, out timer))
  46. {
  47. continue;
  48. }
  49. timer.tcs.SetResult(true);
  50. }
  51. this.timeId.Remove(kv.Key);
  52. }
  53. }
  54. private void Remove(long id)
  55. {
  56. Timer timer;
  57. if (!this.timers.TryGetValue(id, out timer))
  58. {
  59. return;
  60. }
  61. this.timers.Remove(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. }