LoginClient.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. var realmSession = new RealmSession(this.sessionId, new TcpChannel(tcpClient));
  31. try
  32. {
  33. realmInfo = await realmSession.Login(account, password);
  34. Logger.Trace("session: {0}, login success!", realmSession.ID);
  35. }
  36. finally
  37. {
  38. realmSession.Dispose();
  39. }
  40. // 登录gate
  41. Peer peer = await this.clientHost.ConnectAsync(realmInfo.Item1, realmInfo.Item2);
  42. var gateSession = new GateSession(this.sessionId, new ENetChannel(peer));
  43. try
  44. {
  45. gateSession.Login(realmInfo.Item3);
  46. }
  47. finally
  48. {
  49. gateSession.Dispose();
  50. }
  51. }
  52. }
  53. }