LockComponent.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. namespace Model
  5. {
  6. public enum LockStatus
  7. {
  8. LockedNot,
  9. LockRequesting,
  10. Locked,
  11. }
  12. [ObjectEvent]
  13. public class LockComponentEvent : ObjectEvent<LockComponent>, IAwake<string>
  14. {
  15. public void Awake(string a)
  16. {
  17. this.Get().Awake(a);
  18. }
  19. }
  20. /// <summary>
  21. /// 分布式锁组件,Unit对象可能在不同进程上有镜像,访问该对象的时候需要对他加锁
  22. /// </summary>
  23. public class LockComponent: Component
  24. {
  25. private LockStatus status = LockStatus.LockedNot;
  26. private string address;
  27. private int lockCount;
  28. private readonly EQueue<TaskCompletionSource<bool>> queue = new EQueue<TaskCompletionSource<bool>>();
  29. public void Awake(string addr)
  30. {
  31. this.address = addr;
  32. }
  33. public async Task Lock()
  34. {
  35. ++this.lockCount;
  36. if (this.status == LockStatus.Locked)
  37. {
  38. return;
  39. }
  40. if (this.status == LockStatus.LockRequesting)
  41. {
  42. await WaitLock();
  43. return;
  44. }
  45. this.status = LockStatus.LockRequesting;
  46. // 真身直接本地请求锁,镜像需要调用Rpc获取锁
  47. MasterComponent masterComponent = this.GetComponent<MasterComponent>();
  48. if (masterComponent != null)
  49. {
  50. await masterComponent.Lock(this.address);
  51. }
  52. else
  53. {
  54. RequestLock();
  55. await WaitLock();
  56. }
  57. }
  58. private Task<bool> WaitLock()
  59. {
  60. if (this.status == LockStatus.Locked)
  61. {
  62. return Task.FromResult(true);
  63. }
  64. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  65. this.queue.Enqueue(tcs);
  66. return tcs.Task;
  67. }
  68. private async void RequestLock()
  69. {
  70. try
  71. {
  72. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.address);
  73. string serverAddress = Game.Scene.GetComponent<StartConfigComponent>().StartConfig.ServerIP;
  74. G2G_LockRequest request = new G2G_LockRequest { Id = this.Entity.Id, Address = serverAddress };
  75. await session.Call<G2G_LockResponse>(request);
  76. this.status = LockStatus.Locked;
  77. foreach (TaskCompletionSource<bool> taskCompletionSource in this.queue)
  78. {
  79. taskCompletionSource.SetResult(true);
  80. }
  81. this.queue.Clear();
  82. }
  83. catch (Exception e)
  84. {
  85. Log.Error($"获取锁失败: {this.address} {this.Entity.Id} {e}");
  86. }
  87. }
  88. public async Task Release()
  89. {
  90. --this.lockCount;
  91. if (this.lockCount != 0)
  92. {
  93. return;
  94. }
  95. this.status = LockStatus.LockedNot;
  96. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.address);
  97. G2G_LockReleaseRequest request = new G2G_LockReleaseRequest();
  98. await session.Call<G2G_LockReleaseResponse>(request);
  99. }
  100. }
  101. }