Add ldn:u implementation, INetworkClient interface and DisabledLdnClient (#5652)

* Impl first attempt to LDN

* Make this work.

- Endianness swap on all IPs.
- Use local network IP for connections, rather than 127.0.0.1. This is to be changed when tunnelling or whatever.
- Mac addresses are now randomly assigned on the server. (fixes joining lobbies)
- Fixed the "connected" handler for stations to actually find a
- Added info retrieval when connected to a station.
- Users that disconnect are now removed from rooms they were in. (still need to broadcast tho)
- The communication service does a bit better with being closed now.
- Some locking around the game instance dictionary.

* We may just be "initialized". Ignore this for now.

* Lots of WIP

* Add Disconnect packet

* Improve signalling of internal events.

* Fix scan.

* Fix some more stupid things.

* Enable NoDelay on all sockets.

* Add station accept policy, disconnect function.

* Limit max number of games.

* Split out networking stuff from HLE, so it can be swapped.

* Update logging calls.

* Missed a spot.

* Call SignalDisconnect instead of SetState

* Add comment to GetNetworkInfo

* Update configuration + UI

Now has its own tab, more options.

* Refactoring IUserLocalCommunicationService

( Expected new issues :'( )

* some cleanup

* More fix

* Correctly handle errors when connecting.

* Disable *Private call and clean symbols

* Structs cleanup

* Big cleanup

* Fix InvalidHandle (in MK8D and other games)

* Add Reject and Private Network support (v1)

RyuLdn Version bumped to 1.

* Add Initialize Packet

Allows users to keep Mac Addresses assigned by the server.

* Add SetWirelessControllerRestriction and some cleanup

* LDN-2 Initial Rebase

Make this work.

- Endianness swap on all IPs.
- Use local network IP for connections, rather than 127.0.0.1. This is to be changed when tunnelling or whatever.
- Fixed the "connected" handler for stations to actually find a
- The communication service does a bit better with being closed now.
- Some locking around the game instance dictionary.

We may just be "initialized". Ignore this for now.

Lots of WIP

Implement scan filter.

Improve signalling of internal events.

Fix scan.

Fix 0 width data, scan reply end delay removed.

Fix some more stupid things.

Enable NoDelay on all sockets.

Add station accept policy, disconnect function.

Limit max number of games.

Split out networking stuff from HLE, so it can be swapped.

Update logging calls.

Missed a spot.

SetAdvertiseData when open, don't return games that have accept policy 1

Update configuration + UI

Now has its own tab, more options.

Don't Keepalive, it causes problems.

Refactoring IUserLocalCommunicationService

( Expected new issues :'( )

some cleanup

More fix

Correctly handle errors when connecting.

Disable *Private call and clean symbols

Structs cleanup

Big cleanup

Fix InvalidHandle (in MK8D and other games)

Add Reject and Private Network support (v1)

Disable TcpNoDelay option on linux.

Add SetWirelessControllerRestriction and some cleanup

Misc cleanup, implement broadcast flag.

* Misc Changes

* Fix GetNetworkInfo

* Fix some small issues

* Implement GetNetworkInfoLatestUpdate

* Hotfix when LocalCommunicationId = 0xFFFFFFFFFFFFFFFF

* Fix ARMS Scan (and other games using wrong LocalCommunicationId

* Fix latest update when host leaves

* Revert "Fix ARMS Scan (and other games using wrong LocalCommunicationId"

This reverts commit 519c283d3993e2fdfafb8ac6b4e0a98231f6fb75.

* Fix the localCommunicationId = -1

* Don't set Connect flag for nodes already in the room before joining.

* Make IUserLocalCommunicationService disposable

* Don't dispose if there's no client.

* LDN-2-2 Rebase

Make this work.

- Endianness swap on all IPs.
- Use local network IP for connections, rather than 127.0.0.1. This is to be changed when tunnelling or whatever.
- Fixed the "connected" handler for stations to actually find a
- The communication service does a bit better with being closed now.
- Some locking around the game instance dictionary.

We may just be "initialized". Ignore this for now.

Put sockets behind an interface, so that they can be swapped for something proxyable

Lots of WIP

Implement scan filter.

Improve signalling of internal events.

Fix scan.

Fix 0 width data, scan reply end delay removed.

Fix some more stupid things.

Enable NoDelay on all sockets.

Add station accept policy, disconnect function.

Limit max number of games.

Split out networking stuff from HLE, so it can be swapped.

Update logging calls.

Missed a spot.

SetAdvertiseData when open, don't return games that have accept policy 1

Update configuration + UI

Now has its own tab, more options.

Don't Keepalive, it causes problems.

Refactoring IUserLocalCommunicationService

( Expected new issues :'( )

some cleanup

More fix

Correctly handle errors when connecting.

Disable *Private call and clean symbols

Structs cleanup

Big cleanup

Fix InvalidHandle (in MK8D and other games)

Add Reject and Private Network support (v1)

Disable TcpNoDelay option on linux.

Add SetWirelessControllerRestriction and some cleanup

Misc cleanup, implement broadcast flag.

Misc Changes

Fix GetNetworkInfo

Fix some small issues

Disable LAN by default til the config is added.

Fix Splatoon 2

- Stub nfp IUser::StartDetection / IUser::StopDetection.
- Stub ntc IEnsureNetworkClockAvailabilityService and needed calls.

Cleanup previous fixes

Stub IAudioInManager/IAudioIn for Splatoon 2 LAN

Add LAN settings to multiplayer tab

LAN Play > LAN Mode

Implement GetNetworkInfoLatestUpdate

Hotfix when LocalCommunicationId = 0xFFFFFFFFFFFFFFFF

Fix ARMS Scan (and other games using wrong LocalCommunicationId

Fix latest update when host leaves

Revert "Fix ARMS Scan (and other games using wrong LocalCommunicationId"

This reverts commit 519c283d3993e2fdfafb8ac6b4e0a98231f6fb75.

Fix the localCommunicationId = -1

Don't set Connect flag for nodes already in the room before joining.

Make IUserLocalCommunicationService disposable

Fix crash when using LAN mode on linux.

Actually use that call

Don't dispose if there's no client.

Fix the settings window crash

Fix configurationFileUpdated

* Make LDN compatible with Ryujinx/Ryujinx#3805

* Ava: Add Ldn options to SettingsNetworkTab

* Ava: Add update events for multiplayer options

* Apply formatting

* Remove LdnHelper

* ldn: Fix hardcoded /24 subnet mask

* Fix naming rule violations

* Add missing summary doc tag

* Remove NetCoreServer dependency

* Address code style issues and typos

Co-authored-by: gdkchan <gab.dark.100@gmail.com>

* Call CloseStation/CloseAccessPoint to reduce code duplication

* Fix typo

Co-authored-by: gdkchan <gab.dark.100@gmail.com>

* Fix missing trailing commas

* Extract AddressList from AddressEntry

* Use AcceptPolicy as a type for LdnNetworkInfo.StationAcceptPolicy

* Add Flags attribute to ScanFilterFlag

* Rename struct members for LdnNetworkInfo

* Remove extra line

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Extract NetworkErrorMessage from NetworkError

* Fix missing trailing commas

---------

Co-authored-by: Ac_K <Acoustik666@gmail.com>
Co-authored-by: riperiperi <rhy3756547@hotmail.com>
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
This commit is contained in:
TSRBerry
2023-09-25 23:50:43 +02:00
committed by GitHub
parent eca8808649
commit 53bd4c9f60
54 changed files with 2096 additions and 69 deletions

View File

@ -0,0 +1,104 @@
using Ryujinx.Common.Memory;
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.Network.Types;
using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn.Types;
using System;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator
{
class AccessPoint : IDisposable
{
private byte[] _advertiseData;
private readonly IUserLocalCommunicationService _parent;
public NetworkInfo NetworkInfo;
public Array8<NodeLatestUpdate> LatestUpdates = new();
public bool Connected { get; private set; }
public AccessPoint(IUserLocalCommunicationService parent)
{
_parent = parent;
_parent.NetworkClient.NetworkChange += NetworkChanged;
}
public void Dispose()
{
_parent.NetworkClient.DisconnectNetwork();
_parent.NetworkClient.NetworkChange -= NetworkChanged;
}
private void NetworkChanged(object sender, RyuLdn.NetworkChangeEventArgs e)
{
LatestUpdates.CalculateLatestUpdate(NetworkInfo.Ldn.Nodes, e.Info.Ldn.Nodes);
NetworkInfo = e.Info;
if (Connected != e.Connected)
{
Connected = e.Connected;
if (Connected)
{
_parent.SetState(NetworkState.AccessPointCreated);
}
else
{
_parent.SetDisconnectReason(e.DisconnectReasonOrDefault(DisconnectReason.DestroyedBySystem));
}
}
else
{
_parent.SetState();
}
}
public ResultCode SetAdvertiseData(byte[] advertiseData)
{
_advertiseData = advertiseData;
_parent.NetworkClient.SetAdvertiseData(_advertiseData);
return ResultCode.Success;
}
public ResultCode SetStationAcceptPolicy(AcceptPolicy acceptPolicy)
{
_parent.NetworkClient.SetStationAcceptPolicy(acceptPolicy);
return ResultCode.Success;
}
public ResultCode CreateNetwork(SecurityConfig securityConfig, UserConfig userConfig, NetworkConfig networkConfig)
{
CreateAccessPointRequest request = new()
{
SecurityConfig = securityConfig,
UserConfig = userConfig,
NetworkConfig = networkConfig,
};
bool success = _parent.NetworkClient.CreateNetwork(request, _advertiseData ?? Array.Empty<byte>());
return success ? ResultCode.Success : ResultCode.InvalidState;
}
public ResultCode CreateNetworkPrivate(SecurityConfig securityConfig, SecurityParameter securityParameter, UserConfig userConfig, NetworkConfig networkConfig, AddressList addressList)
{
CreateAccessPointPrivateRequest request = new()
{
SecurityConfig = securityConfig,
SecurityParameter = securityParameter,
UserConfig = userConfig,
NetworkConfig = networkConfig,
AddressList = addressList,
};
bool success = _parent.NetworkClient.CreateNetworkPrivate(request, _advertiseData);
return success ? ResultCode.Success : ResultCode.InvalidState;
}
}
}

View File

@ -0,0 +1,15 @@
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.Network.Types
{
[StructLayout(LayoutKind.Sequential, Size = 0x4FC)]
struct ConnectRequest
{
public SecurityConfig SecurityConfig;
public UserConfig UserConfig;
public uint LocalCommunicationVersion;
public uint OptionUnknown;
public NetworkInfo NetworkInfo;
}
}

View File

@ -0,0 +1,16 @@
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.Network.Types
{
/// <remarks>
/// Advertise data is appended separately (remaining data in the buffer).
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 0x94, CharSet = CharSet.Ansi)]
struct CreateAccessPointRequest
{
public SecurityConfig SecurityConfig;
public UserConfig UserConfig;
public NetworkConfig NetworkConfig;
}
}

View File

@ -0,0 +1,62 @@
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.Network.Types;
using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn.Types;
using System;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn
{
class DisabledLdnClient : INetworkClient
{
public event EventHandler<NetworkChangeEventArgs> NetworkChange;
public NetworkError Connect(ConnectRequest request)
{
NetworkChange?.Invoke(this, new NetworkChangeEventArgs(new NetworkInfo(), false));
return NetworkError.None;
}
public NetworkError ConnectPrivate(ConnectPrivateRequest request)
{
NetworkChange?.Invoke(this, new NetworkChangeEventArgs(new NetworkInfo(), false));
return NetworkError.None;
}
public bool CreateNetwork(CreateAccessPointRequest request, byte[] advertiseData)
{
NetworkChange?.Invoke(this, new NetworkChangeEventArgs(new NetworkInfo(), false));
return true;
}
public bool CreateNetworkPrivate(CreateAccessPointPrivateRequest request, byte[] advertiseData)
{
NetworkChange?.Invoke(this, new NetworkChangeEventArgs(new NetworkInfo(), false));
return true;
}
public void DisconnectAndStop() { }
public void DisconnectNetwork() { }
public ResultCode Reject(DisconnectReason disconnectReason, uint nodeId)
{
return ResultCode.Success;
}
public NetworkInfo[] Scan(ushort channel, ScanFilter scanFilter)
{
return Array.Empty<NetworkInfo>();
}
public void SetAdvertiseData(byte[] data) { }
public void SetGameVersion(byte[] versionString) { }
public void SetStationAcceptPolicy(AcceptPolicy acceptPolicy) { }
public void Dispose() { }
}
}

View File

@ -0,0 +1,24 @@
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.Network.Types;
using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn.Types;
using System;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn
{
interface INetworkClient : IDisposable
{
event EventHandler<NetworkChangeEventArgs> NetworkChange;
void DisconnectNetwork();
void DisconnectAndStop();
NetworkError Connect(ConnectRequest request);
NetworkError ConnectPrivate(ConnectPrivateRequest request);
ResultCode Reject(DisconnectReason disconnectReason, uint nodeId);
NetworkInfo[] Scan(ushort channel, ScanFilter scanFilter);
void SetGameVersion(byte[] versionString);
void SetStationAcceptPolicy(AcceptPolicy acceptPolicy);
void SetAdvertiseData(byte[] data);
bool CreateNetwork(CreateAccessPointRequest request, byte[] advertiseData);
bool CreateNetworkPrivate(CreateAccessPointPrivateRequest request, byte[] advertiseData);
}
}

View File

@ -0,0 +1,24 @@
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using System;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn
{
class NetworkChangeEventArgs : EventArgs
{
public NetworkInfo Info;
public bool Connected;
public DisconnectReason DisconnectReason;
public NetworkChangeEventArgs(NetworkInfo info, bool connected, DisconnectReason disconnectReason = DisconnectReason.None)
{
Info = info;
Connected = connected;
DisconnectReason = disconnectReason;
}
public DisconnectReason DisconnectReasonOrDefault(DisconnectReason defaultReason)
{
return DisconnectReason == DisconnectReason.None ? defaultReason : DisconnectReason;
}
}
}

View File

@ -0,0 +1,16 @@
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn.Types
{
[StructLayout(LayoutKind.Sequential, Size = 0xBC)]
struct ConnectPrivateRequest
{
public SecurityConfig SecurityConfig;
public SecurityParameter SecurityParameter;
public UserConfig UserConfig;
public uint LocalCommunicationVersion;
public uint OptionUnknown;
public NetworkConfig NetworkConfig;
}
}

View File

@ -0,0 +1,18 @@
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn.Types
{
/// <remarks>
/// Advertise data is appended separately (remaining data in the buffer).
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 0x13C, Pack = 1)]
struct CreateAccessPointPrivateRequest
{
public SecurityConfig SecurityConfig;
public SecurityParameter SecurityParameter;
public UserConfig UserConfig;
public NetworkConfig NetworkConfig;
public AddressList AddressList;
}
}

View File

@ -0,0 +1,22 @@
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn.Types
{
enum NetworkError : int
{
None,
PortUnreachable,
TooManyPlayers,
VersionTooLow,
VersionTooHigh,
ConnectFailure,
ConnectNotFound,
ConnectTimeout,
ConnectRejected,
RejectFailed,
Unknown = -1,
}
}

View File

@ -0,0 +1,10 @@
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn.Types
{
[StructLayout(LayoutKind.Sequential, Size = 0x4)]
struct NetworkErrorMessage
{
public NetworkError Error;
}
}

View File

@ -0,0 +1,115 @@
using Ryujinx.Common.Memory;
using Ryujinx.HLE.HOS.Services.Ldn.Types;
using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.Network.Types;
using Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator.RyuLdn.Types;
using System;
namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator
{
class Station : IDisposable
{
public NetworkInfo NetworkInfo;
public Array8<NodeLatestUpdate> LatestUpdates = new();
private readonly IUserLocalCommunicationService _parent;
public bool Connected { get; private set; }
public Station(IUserLocalCommunicationService parent)
{
_parent = parent;
_parent.NetworkClient.NetworkChange += NetworkChanged;
}
private void NetworkChanged(object sender, RyuLdn.NetworkChangeEventArgs e)
{
LatestUpdates.CalculateLatestUpdate(NetworkInfo.Ldn.Nodes, e.Info.Ldn.Nodes);
NetworkInfo = e.Info;
if (Connected != e.Connected)
{
Connected = e.Connected;
if (Connected)
{
_parent.SetState(NetworkState.StationConnected);
}
else
{
_parent.SetDisconnectReason(e.DisconnectReasonOrDefault(DisconnectReason.DestroyedByUser));
}
}
else
{
_parent.SetState();
}
}
public void Dispose()
{
_parent.NetworkClient.DisconnectNetwork();
_parent.NetworkClient.NetworkChange -= NetworkChanged;
}
private ResultCode NetworkErrorToResult(NetworkError error)
{
return error switch
{
NetworkError.None => ResultCode.Success,
NetworkError.VersionTooLow => ResultCode.VersionTooLow,
NetworkError.VersionTooHigh => ResultCode.VersionTooHigh,
NetworkError.TooManyPlayers => ResultCode.TooManyPlayers,
NetworkError.ConnectFailure => ResultCode.ConnectFailure,
NetworkError.ConnectNotFound => ResultCode.ConnectNotFound,
NetworkError.ConnectTimeout => ResultCode.ConnectTimeout,
NetworkError.ConnectRejected => ResultCode.ConnectRejected,
_ => ResultCode.DeviceNotAvailable,
};
}
public ResultCode Connect(
SecurityConfig securityConfig,
UserConfig userConfig,
uint localCommunicationVersion,
uint optionUnknown,
NetworkInfo networkInfo)
{
ConnectRequest request = new()
{
SecurityConfig = securityConfig,
UserConfig = userConfig,
LocalCommunicationVersion = localCommunicationVersion,
OptionUnknown = optionUnknown,
NetworkInfo = networkInfo,
};
return NetworkErrorToResult(_parent.NetworkClient.Connect(request));
}
public ResultCode ConnectPrivate(
SecurityConfig securityConfig,
SecurityParameter securityParameter,
UserConfig userConfig,
uint localCommunicationVersion,
uint optionUnknown,
NetworkConfig networkConfig)
{
ConnectPrivateRequest request = new()
{
SecurityConfig = securityConfig,
SecurityParameter = securityParameter,
UserConfig = userConfig,
LocalCommunicationVersion = localCommunicationVersion,
OptionUnknown = optionUnknown,
NetworkConfig = networkConfig,
};
return NetworkErrorToResult(_parent.NetworkClient.ConnectPrivate(request));
}
}
}