TimerComponent.cs 3.1 KB

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