TimerComponent.cs 3.3 KB

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