BossClient.cs 1.3 KB

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