GateSession.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Helper;
  5. using Log;
  6. namespace BossClient
  7. {
  8. public class GateSession: IDisposable
  9. {
  10. public int ID { get; set; }
  11. public IMessageChannel IMessageChannel { get; set; }
  12. public readonly Dictionary<ushort, Action<byte[]>> messageHandlers =
  13. new Dictionary<ushort, Action<byte[]>>();
  14. public bool IsStop { get; set; }
  15. public GateSession(int id, IMessageChannel eNetChannel)
  16. {
  17. this.ID = id;
  18. this.IMessageChannel = eNetChannel;
  19. this.IsStop = false;
  20. }
  21. public void Dispose()
  22. {
  23. this.IMessageChannel.Dispose();
  24. }
  25. public async Task HandleMessages()
  26. {
  27. while (!this.IsStop)
  28. {
  29. var result = await this.IMessageChannel.RecvMessage();
  30. ushort opcode = result.Item1;
  31. byte[] message = result.Item2;
  32. if (!messageHandlers.ContainsKey(opcode))
  33. {
  34. Logger.Debug("not found opcode: {0}", opcode);
  35. continue;
  36. }
  37. messageHandlers[opcode](message);
  38. }
  39. }
  40. public void SendMessage<T>(ushort opcode, T message, byte channelID = 0)
  41. {
  42. this.IMessageChannel.SendMessage(opcode, message, channelID);
  43. }
  44. public async Task Login(SRP6Client srp6Client)
  45. {
  46. var smsgAuthChallenge = await this.Handle_SMSG_Auth_Challenge();
  47. var clientSeed = (uint)TimeHelper.EpochTimeSecond();
  48. byte[] digest = srp6Client.CalculateGateDigest(clientSeed, smsgAuthChallenge.Seed);
  49. var cmsgAuthSession = new CMSG_Auth_Session
  50. {
  51. ClientBuild = 11723,
  52. ClientSeed = clientSeed,
  53. Digest = digest,
  54. Hd = new byte[0],
  55. Mac = new byte[0],
  56. Unk2 = 0,
  57. Unk3 = 0,
  58. Unk4 = 0,
  59. Username = srp6Client.Account
  60. };
  61. this.IMessageChannel.SendMessage(MessageOpcode.CMSG_AUTH_SESSION, cmsgAuthSession);
  62. var smsgAuthResponse = await Handle_SMSG_Auth_Response();
  63. if (smsgAuthResponse.ErrorCode != ErrorCode.AUTH_OK)
  64. {
  65. throw new BossException(string.Format(
  66. "session: {0}, SMSG_Auth_Response: {1}",
  67. this.ID, JsonHelper.ToString(smsgAuthResponse)));
  68. }
  69. Logger.Trace("session: {0}, login gate OK!", this.ID);
  70. }
  71. public async Task<SMSG_Auth_Challenge> Handle_SMSG_Auth_Challenge()
  72. {
  73. var result = await this.IMessageChannel.RecvMessage();
  74. ushort opcode = result.Item1;
  75. byte[] message = result.Item2;
  76. Logger.Debug("message: {0}", message.ToHex());
  77. if (opcode != MessageOpcode.SMSG_AUTH_CHALLENGE)
  78. {
  79. throw new BossException(string.Format(
  80. "session: {0}, opcode: {1}", this.ID, opcode));
  81. }
  82. var smsgAuthChallenge = ProtobufHelper.FromBytes<SMSG_Auth_Challenge>(message);
  83. return smsgAuthChallenge;
  84. }
  85. public async Task<SMSG_Auth_Response> Handle_SMSG_Auth_Response()
  86. {
  87. var result = await this.IMessageChannel.RecvMessage();
  88. ushort opcode = result.Item1;
  89. byte[] message = result.Item2;
  90. if (opcode != MessageOpcode.SMSG_AUTH_RESPONSE)
  91. {
  92. throw new BossException(string.Format(
  93. "session: {0}, opcode: {1}", this.ID, opcode));
  94. }
  95. var smsgAuthResponse = ProtobufHelper.FromBytes<SMSG_Auth_Response>(message);
  96. return smsgAuthResponse;
  97. }
  98. }
  99. }