TimerComponent.cs 3.3 KB

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