TimerComponent.cs 3.1 KB

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