From 49acca8cf7464c1ef8db34bea59877af93ec0e06 Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:27:02 +0200 Subject: [PATCH] Add ProxyManager --- .../HOS/Services/Sockets/Bsd/IClient.cs | 7 ++- .../Sockets/Bsd/Proxy/ProxyManager.cs | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Proxy/ProxyManager.cs diff --git a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs index 21d48288e..8616c4ceb 100644 --- a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs +++ b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs @@ -1,6 +1,7 @@ using Ryujinx.Common; using Ryujinx.Common.Logging; using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl; +using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Proxy; using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Types; using Ryujinx.Memory; using System; @@ -95,10 +96,8 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd } } - ISocket newBsdSocket = new ManagedSocket(netDomain, (SocketType)type, protocol) - { - Blocking = !creationFlags.HasFlag(BsdSocketCreationFlags.NonBlocking), - }; + ISocket newBsdSocket = ProxyManager.GetSocket(netDomain, (SocketType)type, protocol); + newBsdSocket.Blocking = !creationFlags.HasFlag(BsdSocketCreationFlags.NonBlocking); LinuxError errno = LinuxError.SUCCESS; diff --git a/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Proxy/ProxyManager.cs b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Proxy/ProxyManager.cs new file mode 100644 index 000000000..a9a219273 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Proxy/ProxyManager.cs @@ -0,0 +1,49 @@ +using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Runtime.CompilerServices; + +namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Proxy +{ + public static class ProxyManager + { + private static readonly Dictionary _proxyEndpoints = new(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static string GetKey(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) + { + return string.Join("-", new[] { (int)addressFamily, (int)socketType, (int)protocolType }); + } + + internal static ISocket GetSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) + { + // TODO: Return proxy socket if AddressFamily, SocketType and ProtocolType match. + + return new ManagedSocket(addressFamily, socketType, protocolType); + } + + public static void AddOrUpdate(EndPoint endPoint, + AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) + { + _proxyEndpoints[GetKey(addressFamily, socketType, protocolType)] = endPoint; + } + + public static void AddOrUpdate(IPAddress address, int port, + AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) + { + _proxyEndpoints[GetKey(addressFamily, socketType, protocolType)] = new IPEndPoint(address, port); + } + + public static void AddOrUpdate(string host, int port, + AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) + { + _proxyEndpoints[GetKey(addressFamily, socketType, protocolType)] = new DnsEndPoint(host, port); + } + + public static bool Remove(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) + { + return _proxyEndpoints.Remove(GetKey(addressFamily, socketType, protocolType)); + } + } +}