BossClient.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Threading.Tasks;
  4. using ENet;
  5. using Logger;
  6. namespace BossClient
  7. {
  8. public class BossClient: IDisposable
  9. {
  10. private int sessionId;
  11. private readonly EService ioService = new EService();
  12. public BossClient()
  13. {
  14. this.ioService.EnableCrc();
  15. }
  16. public void Dispose()
  17. {
  18. this.ioService.Dispose();
  19. }
  20. public void RunOnce()
  21. {
  22. this.ioService.RunOnce();
  23. }
  24. public void Start(int timeout)
  25. {
  26. this.ioService.Start(timeout);
  27. }
  28. public GateSession GateSession { get; private set; }
  29. public async Task Login(string hostName, ushort port, string account, string password)
  30. {
  31. int loginSessionId = ++this.sessionId;
  32. // 登录realm
  33. var tcpClient = new TcpClient();
  34. await tcpClient.ConnectAsync(hostName, port);
  35. Tuple<string, ushort, SRP6Client> realmInfo = null; // ip, port, K
  36. using (var realmSession = new RealmSession(loginSessionId, new TcpChannel(tcpClient)))
  37. {
  38. realmInfo = await realmSession.Login(account, password);
  39. Log.Trace("session: {0}, login success!", realmSession.ID);
  40. }
  41. // 登录gate
  42. var eSocket = new ESocket(this.ioService);
  43. await eSocket.ConnectAsync(realmInfo.Item1, realmInfo.Item2);
  44. this.GateSession = new GateSession(loginSessionId, new ENetChannel(eSocket));
  45. await this.GateSession.Login(realmInfo.Item3);
  46. }
  47. }
  48. }