BossClient.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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(
  30. string hostName, ushort port, string account, string password)
  31. {
  32. int loginSessionId = ++this.sessionId;
  33. // 登录realm
  34. var tcpClient = new TcpClient();
  35. await tcpClient.ConnectAsync(hostName, port);
  36. Tuple<string, ushort, SRP6Client> realmInfo = null; // ip, port, K
  37. using (var realmSession = new RealmSession(loginSessionId, new TcpChannel(tcpClient)))
  38. {
  39. realmInfo = await realmSession.Login(account, password);
  40. Log.Trace("session: {0}, login success!", realmSession.ID);
  41. }
  42. // 登录gate
  43. var eSocket = new ESocket(this.ioService);
  44. await eSocket.ConnectAsync(realmInfo.Item1, realmInfo.Item2);
  45. this.GateSession = new GateSession(loginSessionId, new ENetChannel(eSocket));
  46. await this.GateSession.Login(realmInfo.Item3);
  47. }
  48. }
  49. }