P2pProxySession.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using NetCoreServer;
  2. using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.LdnRyu.Types;
  3. using System;
  4. namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.LdnRyu.Proxy
  5. {
  6. class P2pProxySession : TcpSession
  7. {
  8. public uint VirtualIpAddress { get; private set; }
  9. public RyuLdnProtocol Protocol { get; }
  10. private readonly P2pProxyServer _parent;
  11. private bool _masterClosed;
  12. public P2pProxySession(P2pProxyServer server) : base(server)
  13. {
  14. _parent = server;
  15. Protocol = new RyuLdnProtocol();
  16. Protocol.ProxyDisconnect += HandleProxyDisconnect;
  17. Protocol.ProxyData += HandleProxyData;
  18. Protocol.ProxyConnectReply += HandleProxyConnectReply;
  19. Protocol.ProxyConnect += HandleProxyConnect;
  20. Protocol.ExternalProxy += HandleAuthentication;
  21. }
  22. private void HandleAuthentication(LdnHeader header, ExternalProxyConfig token)
  23. {
  24. if (!_parent.TryRegisterUser(this, token))
  25. {
  26. Disconnect();
  27. }
  28. }
  29. public void SetIpv4(uint ip)
  30. {
  31. VirtualIpAddress = ip;
  32. }
  33. public void DisconnectAndStop()
  34. {
  35. _masterClosed = true;
  36. Disconnect();
  37. }
  38. protected override void OnDisconnected()
  39. {
  40. if (!_masterClosed)
  41. {
  42. _parent.DisconnectProxyClient(this);
  43. }
  44. }
  45. protected override void OnReceived(byte[] buffer, long offset, long size)
  46. {
  47. try
  48. {
  49. Protocol.Read(buffer, (int)offset, (int)size);
  50. }
  51. catch (Exception)
  52. {
  53. Disconnect();
  54. }
  55. }
  56. private void HandleProxyDisconnect(LdnHeader header, ProxyDisconnectMessage message)
  57. {
  58. _parent.HandleProxyDisconnect(this, header, message);
  59. }
  60. private void HandleProxyData(LdnHeader header, ProxyDataHeader message, byte[] data)
  61. {
  62. _parent.HandleProxyData(this, header, message, data);
  63. }
  64. private void HandleProxyConnectReply(LdnHeader header, ProxyConnectResponse data)
  65. {
  66. _parent.HandleProxyConnectReply(this, header, data);
  67. }
  68. private void HandleProxyConnect(LdnHeader header, ProxyConnectRequest message)
  69. {
  70. _parent.HandleProxyConnect(this, header, message);
  71. }
  72. }
  73. }