ClientSenderComponentSystem.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Threading.Tasks;
  2. namespace ET.Client
  3. {
  4. [EntitySystemOf(typeof(ClientSenderComponent))]
  5. public static partial class ClientSenderComponentSystem
  6. {
  7. [EntitySystem]
  8. private static void Awake(this ClientSenderComponent self)
  9. {
  10. }
  11. [EntitySystem]
  12. private static void Destroy(this ClientSenderComponent self)
  13. {
  14. self.RemoveFiberAsync().NoContext();
  15. }
  16. private static async ETTask RemoveFiberAsync(this ClientSenderComponent self)
  17. {
  18. if (self.fiberId == 0)
  19. {
  20. return;
  21. }
  22. int fiberId = self.fiberId;
  23. self.fiberId = 0;
  24. await FiberManager.Instance.Remove(fiberId);
  25. }
  26. public static async ETTask DisposeAsync(this ClientSenderComponent self)
  27. {
  28. await self.RemoveFiberAsync();
  29. self.Dispose();
  30. }
  31. public static async ETTask<long> LoginAsync(this ClientSenderComponent self, string address, string account, string password)
  32. {
  33. self.fiberId = await FiberManager.Instance.Create(SchedulerType.ThreadPool, 0, SceneType.NetClient, "");
  34. self.netClientActorId = new ActorId(self.Fiber().Process, self.fiberId);
  35. Main2NetClient_Login main2NetClientLogin = Main2NetClient_Login.Create();
  36. main2NetClientLogin.OwnerFiberId = self.Fiber().Id;
  37. main2NetClientLogin.Account = account;
  38. main2NetClientLogin.Password = password;
  39. main2NetClientLogin.Address = address;
  40. NetClient2Main_Login response = await self.Root().GetComponent<ProcessInnerSender>().Call(self.netClientActorId, main2NetClientLogin) as NetClient2Main_Login;
  41. return response.PlayerId;
  42. }
  43. public static void Send(this ClientSenderComponent self, IMessage message)
  44. {
  45. A2NetClient_Message a2NetClientMessage = A2NetClient_Message.Create();
  46. a2NetClientMessage.MessageObject = message;
  47. self.Root().GetComponent<ProcessInnerSender>().Send(self.netClientActorId, a2NetClientMessage);
  48. }
  49. public static async ETTask<IResponse> Call(this ClientSenderComponent self, IRequest request, bool needException = true)
  50. {
  51. A2NetClient_Request a2NetClientRequest = A2NetClient_Request.Create();
  52. a2NetClientRequest.MessageObject = request;
  53. using A2NetClient_Response a2NetClientResponse = await self.Root().GetComponent<ProcessInnerSender>().Call(self.netClientActorId, a2NetClientRequest) as A2NetClient_Response;
  54. IResponse response = a2NetClientResponse.MessageObject;
  55. if (response.Error == ErrorCode.ERR_MessageTimeout)
  56. {
  57. throw new RpcException(response.Error, $"Rpc error: request, 注意Actor消息超时,请注意查看是否死锁或者没有reply: {request}, response: {response}");
  58. }
  59. if (needException && ErrorCode.IsRpcNeedThrowException(response.Error))
  60. {
  61. throw new RpcException(response.Error, $"Rpc error: {request}, response: {response}");
  62. }
  63. return response;
  64. }
  65. }
  66. }