GateSession.cs 3.1 KB

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