aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Ldn/UserServiceCreator/Station.cs
blob: c190d6ed182062da7dc6a298fec112de1bec041b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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));
        }
    }
}