LocationProxyComponent.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Net;
  2. using System.Threading.Tasks;
  3. namespace Model
  4. {
  5. [ObjectSystem]
  6. public class LocationProxyComponentSystem : ObjectSystem<LocationProxyComponent>, IAwake
  7. {
  8. public void Awake()
  9. {
  10. this.Get().Awake();
  11. }
  12. }
  13. public class LocationProxyComponent : Component
  14. {
  15. public IPEndPoint LocationAddress;
  16. public int AppId;
  17. public void Awake()
  18. {
  19. StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
  20. this.AppId = startConfigComponent.StartConfig.AppId;
  21. StartConfig startConfig = startConfigComponent.LocationConfig;
  22. this.LocationAddress = startConfig.GetComponent<InnerConfig>().IPEndPoint;
  23. }
  24. public async Task Add(long key)
  25. {
  26. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  27. await session.Call(new ObjectAddRequest() { Key = key, AppId = this.AppId });
  28. }
  29. public async Task Lock(long key, int time = 1000)
  30. {
  31. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  32. await session.Call(new ObjectLockRequest() { Key = key, LockAppId = this.AppId, Time = time });
  33. }
  34. public async Task UnLock(long key, int value)
  35. {
  36. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  37. await session.Call(new ObjectUnLockRequest() { Key = key, UnLockAppId = this.AppId, AppId = value});
  38. }
  39. public async Task Remove(long key)
  40. {
  41. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  42. await session.Call(new ObjectRemoveRequest() { Key = key });
  43. }
  44. public async Task<int> Get(long key)
  45. {
  46. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
  47. ObjectGetResponse response = (ObjectGetResponse)await session.Call(new ObjectGetRequest() { Key = key });
  48. return response.AppId;
  49. }
  50. public override void Dispose()
  51. {
  52. if (this.IsDisposed)
  53. {
  54. return;
  55. }
  56. base.Dispose();
  57. }
  58. }
  59. }