BossClient.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. try
  30. {
  31. // 登录realm
  32. var tcpClient = new TcpClient();
  33. await tcpClient.ConnectAsync(hostName, port);
  34. Tuple<string, ushort, SRP6Client> realmInfo = null; // ip, port, K
  35. using (var realmSession = new RealmSession(loginSessionId, new TcpChannel(tcpClient)))
  36. {
  37. realmInfo = await realmSession.Login(account, password);
  38. Logger.Trace("session: {0}, login success!", realmSession.ID);
  39. }
  40. // 登录gate
  41. Peer peer = await this.clientHost.ConnectAsync(realmInfo.Item1, realmInfo.Item2);
  42. this.GateSession = new GateSession(loginSessionId, new ENetChannel(peer));
  43. await this.GateSession.Login(realmInfo.Item3);
  44. }
  45. catch (Exception e)
  46. {
  47. Logger.Trace("session: {0}, exception: {1}", loginSessionId, e.ToString());
  48. }
  49. }
  50. }
  51. }