MasterComponentSystem.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Net;
  2. using ETModel;
  3. namespace ETHotfix
  4. {
  5. public static class MasterComponentEx
  6. {
  7. public static void AddGhost(this MasterComponent self, IPEndPoint address)
  8. {
  9. self.ghostsAddress.Add(address);
  10. }
  11. public static void RemoveGhost(this MasterComponent self, IPEndPoint address)
  12. {
  13. self.ghostsAddress.Remove(address);
  14. }
  15. public static ETTask Lock(this MasterComponent self, IPEndPoint address)
  16. {
  17. if (self.lockedAddress == null)
  18. {
  19. self.lockedAddress = address;
  20. return ETTask.FromResult(true);
  21. }
  22. ETTaskCompletionSource tcs = new ETTaskCompletionSource();
  23. LockInfo lockInfo = new LockInfo(address, tcs);
  24. self.queue.Enqueue(lockInfo);
  25. return tcs.Task;
  26. }
  27. public static void Release(this MasterComponent self, IPEndPoint address)
  28. {
  29. if (!self.lockedAddress.Equals(address))
  30. {
  31. Log.Error($"解锁地址与锁地址不匹配! {self.lockedAddress} {address}");
  32. return;
  33. }
  34. if (self.queue.Count == 0)
  35. {
  36. self.lockedAddress = null;
  37. return;
  38. }
  39. LockInfo lockInfo = self.queue.Dequeue();
  40. self.lockedAddress = lockInfo.Address;
  41. lockInfo.Tcs.SetResult();
  42. }
  43. }
  44. }