WIP: Make the uPNP device search more reliable for LDN #641
@ -110,24 +110,9 @@ namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.LdnRyu.Proxy
|
|||||||
_broadcastAddress = config.ProxyIp | (~config.ProxySubnetMask);
|
_broadcastAddress = config.ProxyIp | (~config.ProxySubnetMask);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ushort> NatPunch()
|
private async Task<ushort> NatPunchForDevice(NatDevice device)
|
||||||
{
|
{
|
||||||
NatDiscoverer discoverer = new();
|
Logger.Trace?.PrintMsg(LogClass.ServiceLdn, $"Attempting to map port using {device.ToString()}");
|
||||||
CancellationTokenSource cts = new(5000);
|
|
||||||
|
|
||||||
NatDevice device;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
device = await discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cts);
|
|
||||||
}
|
|
||||||
catch (NatDeviceNotFoundException)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
_publicPort = PublicPortBase;
|
|
||||||
|
|
||||||
for (int i = 0; i < PublicPortRange; i++)
|
for (int i = 0; i < PublicPortRange; i++)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -138,12 +123,14 @@ namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.LdnRyu.Proxy
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
catch (MappingException)
|
catch (MappingException ex)
|
||||||
{
|
{
|
||||||
|
Logger.Trace?.PrintMsg(LogClass.ServiceLdn, $"Failed to map port {_publicPort}: {ex.Message}");
|
||||||
_publicPort++;
|
_publicPort++;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Logger.Trace?.PrintMsg(LogClass.ServiceLdn, $"Failed to map port {_publicPort}: {ex.GetType().Name}: {ex.Message}");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,17 +142,52 @@ namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.LdnRyu.Proxy
|
|||||||
|
|
||||||
if (_publicPort != 0)
|
if (_publicPort != 0)
|
||||||
{
|
{
|
||||||
|
_natDevice = device;
|
||||||
_ = Executor.ExecuteAfterDelayAsync(
|
_ = Executor.ExecuteAfterDelayAsync(
|
||||||
PortLeaseRenew.Seconds(),
|
PortLeaseRenew.Seconds(),
|
||||||
_disposedCancellation.Token,
|
_disposedCancellation.Token,
|
||||||
RefreshLease);
|
RefreshLease);
|
||||||
}
|
}
|
||||||
|
|
||||||
_natDevice = device;
|
|
||||||
|
|
||||||
return _publicPort;
|
return _publicPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<ushort> NatPunch()
|
||||||
|
{
|
||||||
|
NatDiscoverer discoverer = new();
|
||||||
|
CancellationTokenSource cts = new(5000);
|
||||||
|
|
||||||
|
NatDevice[] devices;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
devices = (await discoverer.DiscoverDevicesAsync(PortMapper.Upnp, cts)).ToArray();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.Error?.PrintMsg(LogClass.ServiceLdn, $"Failed to discover UPnP devices: {ex.Message}");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (devices.Length == 0)
|
||||||
|
{
|
||||||
|
Logger.Error?.PrintMsg(LogClass.ServiceLdn, "No UPnP devices found.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var device in devices)
|
||||||
|
{
|
||||||
|
_publicPort = PublicPortBase;
|
||||||
|
ushort port = await NatPunchForDevice(device);
|
||||||
|
if (port != 0)
|
||||||
|
{
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Logger.Info?.PrintMsg(LogClass.ServiceLdn, $"Failed to map port using any device");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Proxy handlers
|
// Proxy handlers
|
||||||
|
|
||||||
private void RouteMessage(P2pProxySession sender, ref ProxyInfo info, Action<P2pProxySession> action)
|
private void RouteMessage(P2pProxySession sender, ref ProxyInfo info, Action<P2pProxySession> action)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user