TimerComponent.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace ETModel
  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. [ObjectSystem]
  13. public class TimerComponentUpdateSystem : UpdateSystem<TimerComponent>
  14. {
  15. public override void Update(TimerComponent self)
  16. {
  17. self.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> timeOutTime = new List<long>();
  28. // 记录最小时间,不用每次都去MultiMap取第一个值
  29. private long minTime;
  30. public void Update()
  31. {
  32. if (this.timeId.Count == 0)
  33. {
  34. return;
  35. }
  36. long timeNow = TimeHelper.Now();
  37. if (timeNow < this.minTime)
  38. {
  39. return;
  40. }
  41. this.timeOutTime.Clear();
  42. foreach (KeyValuePair<long,List<long>> kv in this.timeId.GetDictionary())
  43. {
  44. long k = kv.Key;
  45. if (k > timeNow)
  46. {
  47. minTime = k;
  48. break;
  49. }
  50. this.timeOutTime.Add(k);
  51. }
  52. foreach (long k in this.timeOutTime)
  53. {
  54. foreach (long v in this.timeId[k])
  55. {
  56. Timer timer;
  57. if (!this.timers.TryGetValue(v, out timer))
  58. {
  59. continue;
  60. }
  61. this.timers.Remove(v);
  62. timer.tcs.SetResult(true);
  63. }
  64. this.timeId.Remove(k);
  65. }
  66. }
  67. private void Remove(long id)
  68. {
  69. Timer timer;
  70. if (!this.timers.TryGetValue(id, out timer))
  71. {
  72. return;
  73. }
  74. this.timers.Remove(id);
  75. }
  76. public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
  77. {
  78. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  79. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  80. this.timers[timer.Id] = timer;
  81. this.timeId.Add(timer.Time, timer.Id);
  82. if (timer.Time < this.minTime)
  83. {
  84. this.minTime = timer.Time;
  85. }
  86. cancellationToken.Register(() => { this.Remove(timer.Id); });
  87. return tcs.Task;
  88. }
  89. public Task WaitTillAsync(long tillTime)
  90. {
  91. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  92. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs };
  93. this.timers[timer.Id] = timer;
  94. this.timeId.Add(timer.Time, timer.Id);
  95. if (timer.Time < this.minTime)
  96. {
  97. this.minTime = timer.Time;
  98. }
  99. return tcs.Task;
  100. }
  101. public Task WaitAsync(long time, CancellationToken cancellationToken)
  102. {
  103. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  104. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  105. this.timers[timer.Id] = timer;
  106. this.timeId.Add(timer.Time, timer.Id);
  107. if (timer.Time < this.minTime)
  108. {
  109. this.minTime = timer.Time;
  110. }
  111. cancellationToken.Register(() => { this.Remove(timer.Id); });
  112. return tcs.Task;
  113. }
  114. public Task WaitAsync(long time)
  115. {
  116. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  117. Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs };
  118. this.timers[timer.Id] = timer;
  119. this.timeId.Add(timer.Time, timer.Id);
  120. if (timer.Time < this.minTime)
  121. {
  122. this.minTime = timer.Time;
  123. }
  124. return tcs.Task;
  125. }
  126. }
  127. }