LoginClient.cs 1.2 KB

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