LoginClient.cs 1.5 KB

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