LocationComponent.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. namespace Model
  5. {
  6. public abstract class LocationTask : SceneEntity
  7. {
  8. protected LocationTask()
  9. {
  10. }
  11. protected LocationTask(long id) : base(id)
  12. {
  13. }
  14. public abstract void Run();
  15. }
  16. public sealed class LocationLockTask : LocationTask
  17. {
  18. private readonly long key;
  19. private readonly int lockAppId;
  20. private readonly int time;
  21. private readonly TaskCompletionSource<bool> tcs;
  22. public LocationLockTask(long key, int lockAppId, int time)
  23. {
  24. this.key = key;
  25. this.lockAppId = lockAppId;
  26. this.time = time;
  27. this.tcs = new TaskCompletionSource<bool>();
  28. }
  29. public Task<bool> Task
  30. {
  31. get
  32. {
  33. return this.tcs.Task;
  34. }
  35. }
  36. public override void Run()
  37. {
  38. try
  39. {
  40. Scene.GetComponent<LocationComponent>().Lock(this.key, this.lockAppId, this.time);
  41. this.tcs.SetResult(true);
  42. }
  43. catch (Exception e)
  44. {
  45. this.tcs.SetException(e);
  46. }
  47. }
  48. }
  49. public sealed class LocationQueryTask : LocationTask
  50. {
  51. private readonly long key;
  52. private readonly TaskCompletionSource<int> tcs;
  53. public LocationQueryTask(long key)
  54. {
  55. this.key = key;
  56. this.tcs = new TaskCompletionSource<int>();
  57. }
  58. public Task<int> Task
  59. {
  60. get
  61. {
  62. return this.tcs.Task;
  63. }
  64. }
  65. public override void Run()
  66. {
  67. try
  68. {
  69. int location = Scene.GetComponent<LocationComponent>().Get(key);
  70. this.tcs.SetResult(location);
  71. }
  72. catch (Exception e)
  73. {
  74. this.tcs.SetException(e);
  75. }
  76. }
  77. }
  78. public class LocationComponent : Component
  79. {
  80. private readonly Dictionary<long, int> locations = new Dictionary<long, int>();
  81. private readonly Dictionary<long, int> lockDict = new Dictionary<long, int>();
  82. private readonly Dictionary<long, Queue<LocationTask>> taskQueues = new Dictionary<long, Queue<LocationTask>>();
  83. public void Add(long key, int appId)
  84. {
  85. this.locations[key] = appId;
  86. Log.Info($"location add key: {key} appid: {appId}");
  87. // 更新db
  88. //await Game.Scene.GetComponent<DBProxyComponent>().Save(new Location(key, address));
  89. }
  90. public void Remove(long key)
  91. {
  92. Log.Info($"location remove key: {key}");
  93. this.locations.Remove(key);
  94. }
  95. public int Get(long key)
  96. {
  97. this.locations.TryGetValue(key, out int location);
  98. return location;
  99. }
  100. public async void Lock(long key, int lockAppId, int time = 0)
  101. {
  102. if (this.lockDict.ContainsKey(key))
  103. {
  104. Log.Error($"不可能同时存在两次lock, key: {key} lockAppId: {lockAppId}");
  105. return;
  106. }
  107. Log.Info($"location lock key: {key} lockAppId: {lockAppId}");
  108. this.lockDict.Add(key, lockAppId);
  109. // 超时则解锁
  110. if (time > 0)
  111. {
  112. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(time);
  113. int saveAppId = 0;
  114. if (!this.lockDict.TryGetValue(key, out saveAppId))
  115. {
  116. return;
  117. }
  118. if (saveAppId != lockAppId)
  119. {
  120. Log.Error($"timeout unlock appid is different {saveAppId} {lockAppId}");
  121. return;
  122. }
  123. Log.Info($"location timeout unlock key: {key} time: {time}");
  124. this.UnLock(key);
  125. }
  126. }
  127. public void UpdateAndUnLock(long key, int unLockAppId, int value)
  128. {
  129. int lockAppId = 0;
  130. this.lockDict.TryGetValue(key, out lockAppId);
  131. if (lockAppId != unLockAppId)
  132. {
  133. Log.Error($"unlock appid is different {lockAppId} {unLockAppId}" );
  134. }
  135. Log.Info($"location unlock key: {key} unLockAppId: {unLockAppId} new: {value}");
  136. this.locations[key] = value;
  137. this.UnLock(key);
  138. }
  139. private void UnLock(long key)
  140. {
  141. this.lockDict.Remove(key);
  142. if (!this.taskQueues.TryGetValue(key, out Queue<LocationTask> tasks))
  143. {
  144. return;
  145. }
  146. while (true)
  147. {
  148. if (tasks.Count <= 0)
  149. {
  150. this.taskQueues.Remove(key);
  151. return;
  152. }
  153. if (this.lockDict.ContainsKey(key))
  154. {
  155. return;
  156. }
  157. LocationTask task = tasks.Dequeue();
  158. task.Run();
  159. }
  160. }
  161. public Task<bool> LockAsync(long key, int appId, int time)
  162. {
  163. if (!this.lockDict.ContainsKey(key))
  164. {
  165. this.Lock(key, appId, time);
  166. return Task.FromResult(true);
  167. }
  168. LocationLockTask task = new LocationLockTask(key, appId, time);
  169. this.AddTask(key, task);
  170. return task.Task;
  171. }
  172. public Task<int> GetAsync(long key)
  173. {
  174. if (!this.lockDict.ContainsKey(key))
  175. {
  176. this.locations.TryGetValue(key, out int location);
  177. Log.Info($"location get key: {key} {location}");
  178. return Task.FromResult(location);
  179. }
  180. LocationQueryTask task = new LocationQueryTask(key);
  181. this.AddTask(key, task);
  182. return task.Task;
  183. }
  184. public void AddTask(long key, LocationTask task)
  185. {
  186. if (!this.taskQueues.TryGetValue(key, out Queue<LocationTask> tasks))
  187. {
  188. tasks = new Queue<LocationTask>();
  189. this.taskQueues[key] = tasks;
  190. }
  191. task.Scene = this.GetParent<Scene>();
  192. tasks.Enqueue(task);
  193. }
  194. public override void Dispose()
  195. {
  196. if (this.IsDisposed)
  197. {
  198. return;
  199. }
  200. base.Dispose();
  201. }
  202. }
  203. }