MasterComponentSystem.cs 1.2 KB

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