TimerComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace Model
  5. {
  6. public struct Timer
  7. {
  8. public long Id { get; set; }
  9. public long Time { get; set; }
  10. public TaskCompletionSource<bool> tcs;
  11. }
  12. [ObjectEvent]
  13. public class TimerComponentSystem : ObjectSystem<TimerComponent>, IUpdate
  14. {
  15. public void Update()
  16. {
  17. this.Get().Update();
  18. }
  19. }
  20. public class TimerComponent : Component
  21. {
  22. private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
  23. /// <summary>
  24. /// key: time, value: timer id
  25. /// </summary>
  26. private readonly MultiMap<long, long> timeId = new MultiMap<long, long>();
  27. private readonly List<long> timeOutId = new List<long>();
  28. public void Update()
  29. {
  30. long timeNow = TimeHelper.Now();
  31. while (true)
  32. {
  33. if (this.timeId.Count <= 0)
  34. {
  35. return;
  36. }
  37. var kv = this.timeId.First();
  38. if (kv.Key > timeNow)
  39. {
  40. break;
  41. }
  42. timeOutId.Clear();
  43. timeOutId.AddRange(kv.Value);
  44. this.timeId.Remove(kv.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.timers.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. }
  66. public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
  67. {
  68. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  69. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  70. this.timers[timer.Id] = timer;
  71. this.timeId.Add(timer.Time, timer.Id);
  72. cancellationToken.Register(() => { this.Remove(timer.Id); });
  73. return tcs.Task;
  74. }
  75. public Task WaitTillAsync(long tillTime)
  76. {
  77. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  78. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  79. this.timers[timer.Id] = timer;
  80. this.timeId.Add(timer.Time, timer.Id);
  81. return tcs.Task;
  82. }
  83. public Task WaitAsync(long time, CancellationToken cancellationToken)
  84. {
  85. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  86. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  87. this.timers[timer.Id] = timer;
  88. this.timeId.Add(timer.Time, timer.Id);
  89. cancellationToken.Register(() => { this.Remove(timer.Id); });
  90. return tcs.Task;
  91. }
  92. public Task WaitAsync(long time)
  93. {
  94. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  95. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  96. this.timers[timer.Id] = timer;
  97. this.timeId.Add(timer.Time, timer.Id);
  98. return tcs.Task;
  99. }
  100. }
  101. }