LoginClient.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. private GateSession gateSession;
  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 async void Login(
  25. string hostName, ushort port, string account, string password)
  26. {
  27. int loginSessionId = ++this.sessionId;
  28. try
  29. {
  30. // 登录realm
  31. var tcpClient = new TcpClient();
  32. await tcpClient.ConnectAsync(hostName, port);
  33. Tuple<string, ushort, SRP6Client> realmInfo = null; // ip, port, K
  34. using (var realmSession = new RealmSession(loginSessionId, new TcpChannel(tcpClient)))
  35. {
  36. realmInfo = await realmSession.Login(account, password);
  37. Logger.Trace("session: {0}, login success!", realmSession.ID);
  38. }
  39. // 登录gate
  40. Peer peer = await this.clientHost.ConnectAsync(realmInfo.Item1, realmInfo.Item2);
  41. gateSession = new GateSession(loginSessionId, new ENetChannel(peer));
  42. await gateSession.Login(realmInfo.Item3);
  43. await gateSession.HandleMessages();
  44. }
  45. catch (Exception e)
  46. {
  47. Logger.Trace("session: {0}, exception: {1}", loginSessionId, e.ToString());
  48. }
  49. }
  50. public void SendCommand(string command)
  51. {
  52. var cmsgBossGm = new CMSG_Boss_Gm
  53. {
  54. Message = command
  55. };
  56. this.gateSession.SendMessage(MessageOpcode.CMSG_BOSS_GM, cmsgBossGm);
  57. }
  58. }
  59. }