RPGClient.Gate.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using DeepCore;
  2. using DeepCore.FuckPomeloClient;
  3. using DeepCore.IO;
  4. using DeepMMO.Protocol.Client;
  5. using System;
  6. namespace DeepMMO.Client
  7. {
  8. public partial class RPGClient
  9. {
  10. public ClientEnterGateResponse last_EnterGateResponse { get; private set; }
  11. public ClientEnterGateRequest last_EnterGateRequest { get; private set; }
  12. public string AccountName
  13. {
  14. get { return last_EnterGateRequest != null ? last_EnterGateRequest.c2s_account : ""; }
  15. }
  16. protected virtual void Gate_Init()
  17. {
  18. gate_client.Listen<ClientEnterGateInQueueNotify>(Gate_OnClientEnterGateInQueueNotify);
  19. }
  20. protected virtual void Gate_OnClientEnterGateInQueueNotify(ClientEnterGateInQueueNotify notify)
  21. {
  22. event_OnGateQueueUpdated?.Invoke(notify);
  23. if (notify.IsEnetered)
  24. {
  25. this.last_EnterGateResponse.s2c_code = ClientEnterGateResponse.CODE_OK;
  26. this.last_EnterGateResponse.s2c_connectHost = notify.s2c_connectHost;
  27. this.last_EnterGateResponse.s2c_connectPort = notify.s2c_connectPort;
  28. this.last_EnterGateResponse.s2c_connectToken = notify.s2c_connectToken;
  29. this.last_EnterGateResponse.s2c_lastLoginToken = notify.s2c_lastLoginToken;
  30. this.event_OnGateEntered?.Invoke(last_EnterGateResponse);
  31. if (this.gate_client.IsConnected) gate_client.Disconnect();
  32. }
  33. }
  34. public virtual void Gate_Connect(string host, int port, string account, string token, string serverID, Action<ClientEnterGateResponse> callback)
  35. {
  36. this.last_EnterGateRequest = new ClientEnterGateRequest()
  37. {
  38. c2s_account = account,
  39. c2s_token = CUtils.ToBase64(token),
  40. c2s_clientInfo = clientInfo,
  41. c2s_serverID = serverID,
  42. };
  43. if (this.gate_client.IsConnected)
  44. {
  45. this.gate_client.Disconnect();
  46. }
  47. this.gate_client.Connect(host, port, this.ConnectTimeOut, last_EnterGateRequest, (response) =>
  48. {
  49. var rsp = response as ClientEnterGateResponse;
  50. this.last_EnterGateResponse = rsp;
  51. if (rsp.s2c_code == ClientEnterGateResponse.CODE_OK_IN_QUEUE)
  52. {
  53. callback(rsp);
  54. }
  55. else
  56. {
  57. callback(rsp);
  58. event_OnGateEntered?.Invoke(rsp);
  59. if (this.gate_client.IsConnected) gate_client.Disconnect();
  60. }
  61. });
  62. }
  63. protected virtual void Gate_Disposing()
  64. {
  65. gate_client.Dispose();
  66. }
  67. private Action<ClientEnterGateResponse> event_OnGateEntered;
  68. private Action<ClientEnterGateInQueueNotify> event_OnGateQueueUpdated;
  69. public event Action<ClientEnterGateResponse> OnGateEntered { add { event_OnGateEntered += value; } remove { event_OnGateEntered -= value; } }
  70. public event Action<ClientEnterGateInQueueNotify> OnGateQueueUpdated { add { event_OnGateQueueUpdated += value; } remove { event_OnGateQueueUpdated -= value; } }
  71. }
  72. }