LoginClient.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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; // 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. var gateSession = new GateSession(this.sessionId, new ENetChannel(peer));
  38. gateSession.Login(realmInfo.Item3);
  39. }
  40. }
  41. }