BossClient.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 BossClient()
  13. {
  14. this.clientHost.EnableCrc();
  15. }
  16. public void Dispose()
  17. {
  18. this.clientHost.Dispose();
  19. }
  20. public void RunOnce()
  21. {
  22. this.clientHost.RunOnce();
  23. }
  24. public void Start(int timeout)
  25. {
  26. this.clientHost.Start(timeout);
  27. }
  28. public GateSession GateSession { get; private set; }
  29. public async Task Login(
  30. string hostName, ushort port, string account, string password)
  31. {
  32. int loginSessionId = ++this.sessionId;
  33. // 登录realm
  34. var tcpClient = new TcpClient();
  35. await tcpClient.ConnectAsync(hostName, port);
  36. Tuple<string, ushort, SRP6Client> realmInfo = null; // ip, port, K
  37. using (var realmSession = new RealmSession(loginSessionId, new TcpChannel(tcpClient)))
  38. {
  39. realmInfo = await realmSession.Login(account, password);
  40. Logger.Trace("session: {0}, login success!", realmSession.ID);
  41. }
  42. // 登录gate
  43. Peer peer = await this.clientHost.ConnectAsync(realmInfo.Item1, realmInfo.Item2);
  44. this.GateSession = new GateSession(loginSessionId, new ENetChannel(peer));
  45. await this.GateSession.Login(realmInfo.Item3);
  46. }
  47. }
  48. }