LocationComponent.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 string key;
  19. private readonly TaskCompletionSource<bool> tcs;
  20. public LocationLockTask(string key)
  21. {
  22. this.key = key;
  23. this.tcs = new TaskCompletionSource<bool>();
  24. }
  25. public Task<bool> Task
  26. {
  27. get
  28. {
  29. return this.tcs.Task;
  30. }
  31. }
  32. public override void Run()
  33. {
  34. try
  35. {
  36. Scene.GetComponent<LocationComponent>().Lock(this.key);
  37. this.tcs.SetResult(true);
  38. }
  39. catch (Exception e)
  40. {
  41. this.tcs.SetException(e);
  42. }
  43. }
  44. }
  45. public sealed class LocationQueryTask : LocationTask
  46. {
  47. private readonly string key;
  48. private readonly TaskCompletionSource<string> tcs;
  49. public LocationQueryTask(string key)
  50. {
  51. this.key = key;
  52. this.tcs = new TaskCompletionSource<string>();
  53. }
  54. public Task<string> Task
  55. {
  56. get
  57. {
  58. return this.tcs.Task;
  59. }
  60. }
  61. public override void Run()
  62. {
  63. try
  64. {
  65. string location = Scene.GetComponent<LocationComponent>().Get(key);
  66. this.tcs.SetResult(location);
  67. }
  68. catch (Exception e)
  69. {
  70. this.tcs.SetException(e);
  71. }
  72. }
  73. }
  74. public class LocationComponent : Component
  75. {
  76. private readonly Dictionary<string, string> locations = new Dictionary<string, string>();
  77. private readonly HashSet<string> lockSet = new HashSet<string>();
  78. private readonly Dictionary<string, Queue<LocationTask>> taskQueues = new Dictionary<string,Queue<LocationTask>>();
  79. public void Add(string key, string address)
  80. {
  81. this.locations[key] = address;
  82. }
  83. public void Remove(string key)
  84. {
  85. this.locations.Remove(key);
  86. }
  87. public string Get(string key)
  88. {
  89. this.locations.TryGetValue(key, out string location);
  90. return location;
  91. }
  92. public void Lock(string key)
  93. {
  94. if (this.lockSet.Contains(key))
  95. {
  96. return;
  97. }
  98. this.lockSet.Add(key);
  99. }
  100. public void UnLock(string key)
  101. {
  102. this.lockSet.Remove(key);
  103. if (!this.taskQueues.TryGetValue(key, out Queue<LocationTask> tasks))
  104. {
  105. return;
  106. }
  107. while (true)
  108. {
  109. if (tasks.Count <= 0)
  110. {
  111. this.taskQueues.Remove(key);
  112. return;
  113. }
  114. if (this.lockSet.Contains(key))
  115. {
  116. return;
  117. }
  118. LocationTask task = tasks.Dequeue();
  119. task.Run();
  120. }
  121. }
  122. public Task<bool> LockAsync(string key)
  123. {
  124. if (!this.lockSet.Contains(key))
  125. {
  126. this.Lock(key);
  127. return Task.FromResult(true);
  128. }
  129. LocationLockTask task = new LocationLockTask(key);
  130. this.AddTask(key, task);
  131. return task.Task;
  132. }
  133. public Task<string> GetAsync(string key)
  134. {
  135. if (!this.lockSet.Contains(key))
  136. {
  137. this.locations.TryGetValue(key, out string location);
  138. return Task.FromResult(location);
  139. }
  140. LocationQueryTask task = new LocationQueryTask(key);
  141. this.AddTask(key, task);
  142. return task.Task;
  143. }
  144. public void AddTask(string key, LocationTask task)
  145. {
  146. if (!this.taskQueues.TryGetValue(key, out Queue<LocationTask> tasks))
  147. {
  148. tasks = new Queue<LocationTask>();
  149. this.taskQueues[key] = tasks;
  150. }
  151. task.Scene = this.GetOwner<Scene>();
  152. tasks.Enqueue(task);
  153. }
  154. public override void Dispose()
  155. {
  156. if (this.Id == 0)
  157. {
  158. return;
  159. }
  160. base.Dispose();
  161. }
  162. }
  163. }