LocationComponent.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ETModel
  4. {
  5. public abstract class LocationTask: Component
  6. {
  7. public abstract void Run();
  8. }
  9. [ObjectSystem]
  10. public class LocationQueryTaskAwakeSystem : AwakeSystem<LocationQueryTask, long>
  11. {
  12. public override void Awake(LocationQueryTask self, long key)
  13. {
  14. self.Key = key;
  15. self.Tcs = new ETTaskCompletionSource<long>();
  16. }
  17. }
  18. public sealed class LocationQueryTask : LocationTask
  19. {
  20. public long Key;
  21. public ETTaskCompletionSource<long> Tcs;
  22. public ETTask<long> Task
  23. {
  24. get
  25. {
  26. return this.Tcs.Task;
  27. }
  28. }
  29. public override void Run()
  30. {
  31. try
  32. {
  33. LocationComponent locationComponent = this.GetParent<LocationComponent>();
  34. long location = locationComponent.Get(this.Key);
  35. this.Tcs.SetResult(location);
  36. }
  37. catch (Exception e)
  38. {
  39. this.Tcs.SetException(e);
  40. }
  41. }
  42. }
  43. public class LocationComponent : Component
  44. {
  45. private readonly Dictionary<long, long> locations = new Dictionary<long, long>();
  46. private readonly Dictionary<long, long> lockDict = new Dictionary<long, long>();
  47. private readonly Dictionary<long, Queue<LocationTask>> taskQueues = new Dictionary<long, Queue<LocationTask>>();
  48. public void Add(long key, long instanceId)
  49. {
  50. this.locations[key] = instanceId;
  51. Log.Info($"location add key: {key} instanceId: {instanceId}");
  52. // 更新db
  53. //await Game.Scene.GetComponent<DBProxyComponent>().Save(new Location(key, address));
  54. }
  55. public void Remove(long key)
  56. {
  57. Log.Info($"location remove key: {key}");
  58. this.locations.Remove(key);
  59. }
  60. public long Get(long key)
  61. {
  62. this.locations.TryGetValue(key, out long instanceId);
  63. return instanceId;
  64. }
  65. public async ETVoid Lock(long key, long instanceId, int time = 0)
  66. {
  67. if (this.lockDict.ContainsKey(key))
  68. {
  69. Log.Error($"不可能同时存在两次lock, key: {key} InstanceId: {instanceId}");
  70. return;
  71. }
  72. Log.Info($"location lock key: {key} InstanceId: {instanceId}");
  73. if (!this.locations.TryGetValue(key, out long saveInstanceId))
  74. {
  75. Log.Error($"actor没有注册, key: {key} InstanceId: {instanceId}");
  76. return;
  77. }
  78. if (saveInstanceId != instanceId)
  79. {
  80. Log.Error($"actor注册的instanceId与lock的不一致, key: {key} InstanceId: {instanceId} saveInstanceId: {saveInstanceId}");
  81. return;
  82. }
  83. this.lockDict.Add(key, instanceId);
  84. // 超时则解锁
  85. if (time > 0)
  86. {
  87. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(time);
  88. if (!this.lockDict.ContainsKey(key))
  89. {
  90. return;
  91. }
  92. Log.Info($"location timeout unlock key: {key} time: {time}");
  93. this.UnLock(key);
  94. }
  95. }
  96. public void UnLockAndUpdate(long key, long oldInstanceId, long instanceId)
  97. {
  98. this.lockDict.TryGetValue(key, out long lockInstanceId);
  99. if (lockInstanceId != oldInstanceId)
  100. {
  101. Log.Error($"unlock appid is different {lockInstanceId} {oldInstanceId}" );
  102. }
  103. Log.Info($"location unlock key: {key} oldInstanceId: {oldInstanceId} new: {instanceId}");
  104. this.locations[key] = instanceId;
  105. this.UnLock(key);
  106. }
  107. private void UnLock(long key)
  108. {
  109. this.lockDict.Remove(key);
  110. if (!this.taskQueues.TryGetValue(key, out Queue<LocationTask> tasks))
  111. {
  112. return;
  113. }
  114. while (true)
  115. {
  116. if (tasks.Count <= 0)
  117. {
  118. this.taskQueues.Remove(key);
  119. return;
  120. }
  121. if (this.lockDict.ContainsKey(key))
  122. {
  123. return;
  124. }
  125. LocationTask task = tasks.Dequeue();
  126. try
  127. {
  128. task.Run();
  129. }
  130. catch (Exception e)
  131. {
  132. Log.Error(e);
  133. }
  134. task.Dispose();
  135. }
  136. }
  137. public ETTask<long> GetAsync(long key)
  138. {
  139. if (!this.lockDict.ContainsKey(key))
  140. {
  141. this.locations.TryGetValue(key, out long instanceId);
  142. Log.Info($"location get key: {key} {instanceId}");
  143. return ETTask.FromResult(instanceId);
  144. }
  145. LocationQueryTask task = ComponentFactory.CreateWithParent<LocationQueryTask, long>(this, key);
  146. this.AddTask(key, task);
  147. return task.Task;
  148. }
  149. public void AddTask(long key, LocationTask task)
  150. {
  151. if (!this.taskQueues.TryGetValue(key, out Queue<LocationTask> tasks))
  152. {
  153. tasks = new Queue<LocationTask>();
  154. this.taskQueues[key] = tasks;
  155. }
  156. tasks.Enqueue(task);
  157. }
  158. public override void Dispose()
  159. {
  160. if (this.IsDisposed)
  161. {
  162. return;
  163. }
  164. base.Dispose();
  165. this.locations.Clear();
  166. this.lockDict.Clear();
  167. this.taskQueues.Clear();
  168. }
  169. }
  170. }