LocationComponentSystem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. namespace ET.Server
  2. {
  3. [ObjectSystem]
  4. public class LockInfoAwakeSystem: AwakeSystem<LockInfo, long, CoroutineLock>
  5. {
  6. protected override void Awake(LockInfo self, long lockInstanceId, CoroutineLock coroutineLock)
  7. {
  8. self.CoroutineLock = coroutineLock;
  9. self.LockInstanceId = lockInstanceId;
  10. }
  11. }
  12. [ObjectSystem]
  13. public class LockInfoDestroySystem: DestroySystem<LockInfo>
  14. {
  15. protected override void Destroy(LockInfo self)
  16. {
  17. self.CoroutineLock.Dispose();
  18. self.LockInstanceId = 0;
  19. }
  20. }
  21. [FriendOf(typeof(LocationComponent))]
  22. [FriendOf(typeof(LockInfo))]
  23. public static class LocationComponentSystem
  24. {
  25. public static async ETTask Add(this LocationComponent self, long key, long instanceId)
  26. {
  27. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  28. {
  29. self.locations[key] = instanceId;
  30. Log.Info($"location add key: {key} instanceId: {instanceId}");
  31. }
  32. }
  33. public static async ETTask Remove(this LocationComponent self, long key)
  34. {
  35. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  36. {
  37. self.locations.Remove(key);
  38. Log.Info($"location remove key: {key}");
  39. }
  40. }
  41. public static async ETTask Lock(this LocationComponent self, long key, long instanceId, int time = 0)
  42. {
  43. CoroutineLock coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key);
  44. LockInfo lockInfo = self.AddChild<LockInfo, long, CoroutineLock>(instanceId, coroutineLock);
  45. self.lockInfos.Add(key, lockInfo);
  46. Log.Info($"location lock key: {key} instanceId: {instanceId}");
  47. if (time > 0)
  48. {
  49. async ETTask TimeWaitAsync()
  50. {
  51. long lockInfoInstanceId = lockInfo.InstanceId;
  52. await TimerComponent.Instance.WaitAsync(time);
  53. if (lockInfo.InstanceId != lockInfoInstanceId)
  54. {
  55. return;
  56. }
  57. Log.Info($"location timeout unlock key: {key} instanceId: {instanceId} newInstanceId: {instanceId}");
  58. self.UnLock(key, instanceId, instanceId);
  59. }
  60. TimeWaitAsync().Coroutine();
  61. }
  62. }
  63. public static void UnLock(this LocationComponent self, long key, long oldInstanceId, long newInstanceId)
  64. {
  65. if (!self.lockInfos.TryGetValue(key, out LockInfo lockInfo))
  66. {
  67. Log.Error($"location unlock not found key: {key} {oldInstanceId}");
  68. return;
  69. }
  70. if (oldInstanceId != lockInfo.LockInstanceId)
  71. {
  72. Log.Error($"location unlock oldInstanceId is different: {key} {oldInstanceId}");
  73. return;
  74. }
  75. Log.Info($"location unlock key: {key} instanceId: {oldInstanceId} newInstanceId: {newInstanceId}");
  76. self.locations[key] = newInstanceId;
  77. self.lockInfos.Remove(key);
  78. // 解锁
  79. lockInfo.Dispose();
  80. }
  81. public static async ETTask<long> Get(this LocationComponent self, long key)
  82. {
  83. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  84. {
  85. self.locations.TryGetValue(key, out long instanceId);
  86. Log.Info($"location get key: {key} instanceId: {instanceId}");
  87. return instanceId;
  88. }
  89. }
  90. }
  91. }