TimerComponent.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Base;
  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. TimerComponent component = this.GetValue();
  19. component.Update();
  20. }
  21. }
  22. public class TimerComponent: Component
  23. {
  24. private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
  25. /// <summary>
  26. /// key: time, value: timer id
  27. /// </summary>
  28. private readonly MultiMap<long, long> timeId = new MultiMap<long, long>();
  29. private readonly Queue<long> timeoutTimer = new Queue<long>();
  30. public void Update()
  31. {
  32. long timeNow = TimeHelper.Now();
  33. foreach (long time in this.timeId.Keys)
  34. {
  35. if (time > timeNow)
  36. {
  37. break;
  38. }
  39. this.timeoutTimer.Enqueue(time);
  40. }
  41. while (this.timeoutTimer.Count > 0)
  42. {
  43. long key = this.timeoutTimer.Dequeue();
  44. long[] timeOutId = this.timeId.GetAll(key);
  45. foreach (long id in timeOutId)
  46. {
  47. Timer timer;
  48. if (!this.timers.TryGetValue(id, out timer))
  49. {
  50. continue;
  51. }
  52. this.Remove(id);
  53. timer.tcs.SetResult(true);
  54. }
  55. }
  56. }
  57. private void Remove(long id)
  58. {
  59. Timer timer;
  60. if (!this.timers.TryGetValue(id, out timer))
  61. {
  62. return;
  63. }
  64. this.timers.Remove(id);
  65. this.timeId.Remove(timer.Time, timer.Id);
  66. }
  67. public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
  68. {
  69. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  70. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  71. this.timers[timer.Id] = timer;
  72. this.timeId.Add(timer.Time, timer.Id);
  73. cancellationToken.Register(() =>
  74. {
  75. this.Remove(timer.Id);
  76. });
  77. return tcs.Task;
  78. }
  79. public Task WaitTillAsync(long tillTime)
  80. {
  81. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  82. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  83. this.timers[timer.Id] = timer;
  84. this.timeId.Add(timer.Time, timer.Id);
  85. return tcs.Task;
  86. }
  87. public Task WaitAsync(long time, CancellationToken cancellationToken)
  88. {
  89. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  90. Timer timer = new Timer
  91. {
  92. Id = IdGenerater.GenerateId(),
  93. Time = TimeHelper.Now() + time,
  94. tcs = tcs
  95. };
  96. this.timers[timer.Id] = timer;
  97. this.timeId.Add(timer.Time, timer.Id);
  98. cancellationToken.Register(() =>
  99. {
  100. this.Remove(timer.Id);
  101. });
  102. return tcs.Task;
  103. }
  104. public Task WaitAsync(long time)
  105. {
  106. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  107. Timer timer = new Timer
  108. {
  109. Id = IdGenerater.GenerateId(),
  110. Time = TimeHelper.Now() + time,
  111. tcs = tcs
  112. };
  113. this.timers[timer.Id] = timer;
  114. this.timeId.Add(timer.Time, timer.Id);
  115. return tcs.Task;
  116. }
  117. }
  118. }