CoroutineLockComponent.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Collections.Generic;
  2. using Fort23.Core;
  3. namespace Fort23.UTool
  4. {
  5. public class CoroutineLockComponent : Entity
  6. {
  7. public static CoroutineLockComponent Instance { get; set; }
  8. public Dictionary<long, Queue<CoroutineLockTimer>> dictionary = new Dictionary<long, Queue<CoroutineLockTimer>>();
  9. public Queue<CoroutineLock> nextFrameRun = new Queue<CoroutineLock>();
  10. public MultiMap<long, CoroutineLock> timers = new MultiMap<long, CoroutineLock>();
  11. public Queue<long> timeOutIds = new Queue<long>();
  12. public Queue<CoroutineLock> timerOutTimer = new Queue<CoroutineLock>();
  13. public long idGenerator;
  14. public long minTime;
  15. [CustomMethod(CustomMethodType.Awake)]
  16. public void Awake()
  17. {
  18. Instance = this;
  19. }
  20. [CustomMethod(CustomMethodType.Destroy)]
  21. public void Destroy( )
  22. {
  23. this.dictionary.Clear();
  24. this.nextFrameRun.Clear();
  25. this.timers.Clear();
  26. this.timeOutIds.Clear();
  27. this.timerOutTimer.Clear();
  28. this.idGenerator = 0;
  29. this.minTime = 0;
  30. }
  31. [CustomMethod(CustomMethodType.Update)]
  32. public void Update()
  33. {
  34. CoroutineLockComponent self = this;
  35. // 检测超时的CoroutineLock
  36. TimeoutCheck(self);
  37. int count = self.nextFrameRun.Count;
  38. // 注意这里不能将this.nextFrameRun.Count 放到for循环中,因为循环过程中会有对象继续加入队列
  39. for (int i = 0; i < count; ++i)
  40. {
  41. CoroutineLock key = self.nextFrameRun.Dequeue();
  42. self.Notify(key.key, key.keyName,0);
  43. }
  44. }
  45. public void TimeoutCheck(CoroutineLockComponent self)
  46. {
  47. // 超时的锁
  48. if (self.timers.Count == 0)
  49. {
  50. return;
  51. }
  52. long timeNow = TimeHelper.ClientNow();
  53. if (timeNow < self.minTime)
  54. {
  55. return;
  56. }
  57. foreach (KeyValuePair<long, List<CoroutineLock>> kv in self.timers)
  58. {
  59. long k = kv.Key;
  60. if (k > timeNow)
  61. {
  62. self.minTime = k;
  63. break;
  64. }
  65. self.timeOutIds.Enqueue(k);
  66. }
  67. self.timerOutTimer.Clear();
  68. while (self.timeOutIds.Count > 0)
  69. {
  70. long time = self.timeOutIds.Dequeue();
  71. foreach (CoroutineLock coroutineLock in self.timers[time])
  72. {
  73. self.timerOutTimer.Enqueue(coroutineLock);
  74. }
  75. self.timers.Remove(time);
  76. }
  77. while (self.timerOutTimer.Count > 0)
  78. {
  79. CoroutineLock coroutineLock = self.timerOutTimer.Dequeue();
  80. // 超时直接调用下一个锁
  81. self.NextFrameRun(coroutineLock);
  82. coroutineLock.isTimeout = true;
  83. }
  84. }
  85. public async CTask<CoroutineLock> Wait(string keyName, int time = 10000)
  86. {
  87. long key = keyName.GetHashCode();
  88. if (!dictionary.TryGetValue(key, out Queue<CoroutineLockTimer> queue))
  89. {
  90. // 首次进入,不需要await锁。
  91. dictionary.Add(key, new Queue<CoroutineLockTimer>());
  92. return CreateCoroutineLock(key, keyName,time, 1);
  93. }
  94. // 60秒的默认超时间隔期间有新的协程访问同样的资源则需要等待前面的锁解除才可以继续。
  95. CTask<CoroutineLock> tcs = CTask<CoroutineLock>.Create(false);
  96. queue.Enqueue(new CoroutineLockTimer() {tcs = tcs, duration = time});
  97. return await tcs;
  98. }
  99. public CoroutineLock CreateCoroutineLock(long key,string keyName, int time, int count)
  100. {
  101. CoroutineLock coroutineLock = AddChildWithId<CoroutineLock, long,string, int>(++idGenerator, key, keyName,count, true);
  102. if (time > 0)
  103. {
  104. AddTimer(TimeHelper.ClientNow() + time, coroutineLock);
  105. }
  106. return coroutineLock;
  107. }
  108. public void AddTimer(long tillTime, CoroutineLock coroutineLock)
  109. {
  110. timers.Add(tillTime, coroutineLock);
  111. if (tillTime < minTime)
  112. {
  113. minTime = tillTime;
  114. }
  115. }
  116. public void NextFrameRun(CoroutineLock key)
  117. {
  118. nextFrameRun.Enqueue(key);
  119. }
  120. public void Notify(long key, string keyName,int count)
  121. {
  122. if (!dictionary.TryGetValue(key, out Queue<CoroutineLockTimer> queue))
  123. {
  124. return;
  125. }
  126. if (queue.Count == 0)
  127. {
  128. dictionary.Remove(key);
  129. return;
  130. }
  131. // TODO 这个地方暂时不是很清楚作用,猜测是和复用或者是重置计数有关的。
  132. // const int frameCoroutineCount = 10;
  133. // if (count > frameCoroutineCount)
  134. // {
  135. // NextFrameRun(key);
  136. // return;
  137. // }
  138. CoroutineLockTimer coroutineLockTimer = queue.Dequeue();
  139. coroutineLockTimer.tcs.SetResult(CreateCoroutineLock(key,keyName, coroutineLockTimer.duration, count));
  140. }
  141. }
  142. }