TimerComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Model;
  5. namespace Hotfix
  6. {
  7. public class Timer
  8. {
  9. public long Id { get; set; }
  10. public long Time { get; set; }
  11. public TaskCompletionSource<bool> tcs;
  12. }
  13. [ObjectEvent(EntityEventId.TimerComponent)]
  14. public class TimerComponent: Component, IUpdate
  15. {
  16. private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
  17. /// <summary>
  18. /// key: time, value: timer id
  19. /// </summary>
  20. private readonly MultiMap<long, long> timeId = new MultiMap<long, long>();
  21. private readonly Queue<long> timeoutTimer = new Queue<long>();
  22. public void Update()
  23. {
  24. long timeNow = TimeHelper.Now();
  25. foreach (long time in this.timeId.Keys)
  26. {
  27. if (time > timeNow)
  28. {
  29. break;
  30. }
  31. this.timeoutTimer.Enqueue(time);
  32. }
  33. while (this.timeoutTimer.Count > 0)
  34. {
  35. long key = this.timeoutTimer.Dequeue();
  36. long[] timeOutId = this.timeId.GetAll(key);
  37. foreach (long id in timeOutId)
  38. {
  39. if (!this.timers.TryGetValue(id, out Timer timer))
  40. {
  41. continue;
  42. }
  43. this.Remove(id);
  44. timer.tcs.SetResult(true);
  45. }
  46. }
  47. }
  48. private void Remove(long id)
  49. {
  50. if (!this.timers.TryGetValue(id, out Timer timer))
  51. {
  52. return;
  53. }
  54. this.timers.Remove(id);
  55. this.timeId.Remove(timer.Time, timer.Id);
  56. }
  57. public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
  58. {
  59. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  60. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  61. this.timers[timer.Id] = timer;
  62. this.timeId.Add(timer.Time, timer.Id);
  63. cancellationToken.Register(() => { this.Remove(timer.Id); });
  64. return tcs.Task;
  65. }
  66. public Task WaitTillAsync(long tillTime)
  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. return tcs.Task;
  73. }
  74. public Task WaitAsync(long time, CancellationToken cancellationToken)
  75. {
  76. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  77. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  78. this.timers[timer.Id] = timer;
  79. this.timeId.Add(timer.Time, timer.Id);
  80. cancellationToken.Register(() => { this.Remove(timer.Id); });
  81. return tcs.Task;
  82. }
  83. public Task WaitAsync(long time)
  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. return tcs.Task;
  90. }
  91. }
  92. }