LoginClient.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Sockets;
  4. using System.Threading.Tasks;
  5. using ENet;
  6. using Log;
  7. namespace LoginClient
  8. {
  9. public class LoginClient : IDisposable
  10. {
  11. private int sessionId;
  12. private readonly ClientHost clientHost = new ClientHost();
  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 Task<List<Realm_List_Gate>> LoginRealm(
  26. string hostName, ushort port, string account, string password)
  27. {
  28. using (var tcpClient = new TcpClient())
  29. {
  30. await tcpClient.ConnectAsync(hostName, port);
  31. using (var session = new RealmSession(
  32. tcpClient.GetStream()) { ID = ++this.sessionId })
  33. {
  34. var gateList = await session.Login(account, password);
  35. Logger.Trace("session: {0}, login success!", session.ID);
  36. return gateList;
  37. }
  38. }
  39. }
  40. public async void LoginGate(string hostName, ushort port)
  41. {
  42. Peer peer = await this.clientHost.ConnectAsync(hostName, port);
  43. using (var session = new GateSession(peer) { ID = ++sessionId })
  44. {
  45. session.Login();
  46. }
  47. }
  48. }
  49. }