aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Impl/ManagedSocketPollManager.cs
blob: d0db440863c01ab257e06f4c1752f9ce0583b947 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Types;
using System.Collections.Generic;
using System.Net.Sockets;

namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl
{
    class ManagedSocketPollManager : IPollManager
    {
        private static ManagedSocketPollManager _instance;

        public static ManagedSocketPollManager Instance
        {
            get
            {
                _instance ??= new ManagedSocketPollManager();

                return _instance;
            }
        }

        public bool IsCompatible(PollEvent evnt)
        {
            return evnt.FileDescriptor is ManagedSocket;
        }

        public LinuxError Poll(List<PollEvent> events, int timeoutMilliseconds, out int updatedCount)
        {
            List<Socket> readEvents = new();
            List<Socket> writeEvents = new();
            List<Socket> errorEvents = new();

            updatedCount = 0;

            foreach (PollEvent evnt in events)
            {
                ManagedSocket socket = (ManagedSocket)evnt.FileDescriptor;

                bool isValidEvent = evnt.Data.InputEvents == 0;

                errorEvents.Add(socket.Socket);

                if ((evnt.Data.InputEvents & PollEventTypeMask.Input) != 0)
                {
                    readEvents.Add(socket.Socket);

                    isValidEvent = true;
                }

                if ((evnt.Data.InputEvents & PollEventTypeMask.UrgentInput) != 0)
                {
                    readEvents.Add(socket.Socket);

                    isValidEvent = true;
                }

                if ((evnt.Data.InputEvents & PollEventTypeMask.Output) != 0)
                {
                    writeEvents.Add(socket.Socket);

                    isValidEvent = true;
                }

                if (!isValidEvent)
                {
                    Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported Poll input event type: {evnt.Data.InputEvents}");
                    return LinuxError.EINVAL;
                }
            }

            try
            {
                int actualTimeoutMicroseconds = timeoutMilliseconds == -1 ? -1 : timeoutMilliseconds * 1000;

                Socket.Select(readEvents, writeEvents, errorEvents, actualTimeoutMicroseconds);
            }
            catch (SocketException exception)
            {
                return WinSockHelper.ConvertError((WsaError)exception.ErrorCode);
            }

            foreach (PollEvent evnt in events)
            {
                Socket socket = ((ManagedSocket)evnt.FileDescriptor).Socket;

                PollEventTypeMask outputEvents = evnt.Data.OutputEvents & ~evnt.Data.InputEvents;

                if (errorEvents.Contains(socket))
                {
                    outputEvents |= PollEventTypeMask.Error;

                    if (!socket.Connected || !socket.IsBound)
                    {
                        outputEvents |= PollEventTypeMask.Disconnected;
                    }
                }

                if (readEvents.Contains(socket))
                {
                    if ((evnt.Data.InputEvents & PollEventTypeMask.Input) != 0)
                    {
                        outputEvents |= PollEventTypeMask.Input;
                    }
                }

                if (writeEvents.Contains(socket))
                {
                    outputEvents |= PollEventTypeMask.Output;
                }

                evnt.Data.OutputEvents = outputEvents;
            }

            updatedCount = readEvents.Count + writeEvents.Count + errorEvents.Count;

            return LinuxError.SUCCESS;
        }

        public LinuxError Select(List<PollEvent> events, int timeout, out int updatedCount)
        {
            List<Socket> readEvents = new();
            List<Socket> writeEvents = new();
            List<Socket> errorEvents = new();

            updatedCount = 0;

            foreach (PollEvent pollEvent in events)
            {
                ManagedSocket socket = (ManagedSocket)pollEvent.FileDescriptor;

                if (pollEvent.Data.InputEvents.HasFlag(PollEventTypeMask.Input))
                {
                    readEvents.Add(socket.Socket);
                }

                if (pollEvent.Data.InputEvents.HasFlag(PollEventTypeMask.Output))
                {
                    writeEvents.Add(socket.Socket);
                }

                if (pollEvent.Data.InputEvents.HasFlag(PollEventTypeMask.Error))
                {
                    errorEvents.Add(socket.Socket);
                }
            }

            Socket.Select(readEvents, writeEvents, errorEvents, timeout);

            updatedCount = readEvents.Count + writeEvents.Count + errorEvents.Count;

            foreach (PollEvent pollEvent in events)
            {
                ManagedSocket socket = (ManagedSocket)pollEvent.FileDescriptor;

                if (readEvents.Contains(socket.Socket))
                {
                    pollEvent.Data.OutputEvents |= PollEventTypeMask.Input;
                }

                if (writeEvents.Contains(socket.Socket))
                {
                    pollEvent.Data.OutputEvents |= PollEventTypeMask.Output;
                }

                if (errorEvents.Contains(socket.Socket))
                {
                    pollEvent.Data.OutputEvents |= PollEventTypeMask.Error;
                }
            }

            return LinuxError.SUCCESS;
        }
    }
}