2022-01-12 13:31:08 -05:00
|
|
|
|
using Ryujinx.Common.Logging;
|
2022-12-12 08:59:31 -05:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Types;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2022-12-12 08:59:31 -05:00
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl
|
2022-01-12 13:31:08 -05:00
|
|
|
|
{
|
|
|
|
|
class EventFileDescriptorPollManager : IPollManager
|
|
|
|
|
{
|
|
|
|
|
private static EventFileDescriptorPollManager _instance;
|
|
|
|
|
|
|
|
|
|
public static EventFileDescriptorPollManager Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
_instance = new EventFileDescriptorPollManager();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsCompatible(PollEvent evnt)
|
|
|
|
|
{
|
|
|
|
|
return evnt.FileDescriptor is EventFileDescriptor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LinuxError Poll(List<PollEvent> events, int timeoutMilliseconds, out int updatedCount)
|
|
|
|
|
{
|
|
|
|
|
updatedCount = 0;
|
|
|
|
|
|
|
|
|
|
List<ManualResetEvent> waiters = new List<ManualResetEvent>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < events.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
PollEvent evnt = events[i];
|
|
|
|
|
|
|
|
|
|
EventFileDescriptor socket = (EventFileDescriptor)evnt.FileDescriptor;
|
|
|
|
|
|
|
|
|
|
bool isValidEvent = false;
|
|
|
|
|
|
|
|
|
|
if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Input) ||
|
|
|
|
|
evnt.Data.InputEvents.HasFlag(PollEventTypeMask.UrgentInput))
|
|
|
|
|
{
|
|
|
|
|
waiters.Add(socket.ReadEvent);
|
|
|
|
|
|
|
|
|
|
isValidEvent = true;
|
|
|
|
|
}
|
|
|
|
|
if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Output))
|
|
|
|
|
{
|
|
|
|
|
waiters.Add(socket.WriteEvent);
|
|
|
|
|
|
|
|
|
|
isValidEvent = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isValidEvent)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported Poll input event type: {evnt.Data.InputEvents}");
|
|
|
|
|
|
|
|
|
|
return LinuxError.EINVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int index = WaitHandle.WaitAny(waiters.ToArray(), timeoutMilliseconds);
|
|
|
|
|
|
|
|
|
|
if (index != WaitHandle.WaitTimeout)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < events.Count; i++)
|
|
|
|
|
{
|
2022-11-27 15:18:05 -05:00
|
|
|
|
PollEventTypeMask outputEvents = 0;
|
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
PollEvent evnt = events[i];
|
|
|
|
|
|
|
|
|
|
EventFileDescriptor socket = (EventFileDescriptor)evnt.FileDescriptor;
|
|
|
|
|
|
2022-11-27 15:18:05 -05:00
|
|
|
|
if (socket.ReadEvent.WaitOne(0))
|
2022-01-12 13:31:08 -05:00
|
|
|
|
{
|
2022-11-27 15:18:05 -05:00
|
|
|
|
if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Input))
|
|
|
|
|
{
|
|
|
|
|
outputEvents |= PollEventTypeMask.Input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.UrgentInput))
|
|
|
|
|
{
|
|
|
|
|
outputEvents |= PollEventTypeMask.UrgentInput;
|
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
}
|
2022-11-27 15:18:05 -05:00
|
|
|
|
|
2022-01-12 13:31:08 -05:00
|
|
|
|
if ((evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Output))
|
|
|
|
|
&& socket.WriteEvent.WaitOne(0))
|
|
|
|
|
{
|
2022-11-27 15:18:05 -05:00
|
|
|
|
outputEvents |= PollEventTypeMask.Output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (outputEvents != 0)
|
|
|
|
|
{
|
|
|
|
|
evnt.Data.OutputEvents = outputEvents;
|
|
|
|
|
|
|
|
|
|
updatedCount++;
|
2022-01-12 13:31:08 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return LinuxError.ETIMEDOUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return LinuxError.SUCCESS;
|
|
|
|
|
}
|
2022-12-12 08:59:31 -05:00
|
|
|
|
|
|
|
|
|
public LinuxError Select(List<PollEvent> events, int timeout, out int updatedCount)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement Select for event file descriptors
|
|
|
|
|
updatedCount = 0;
|
|
|
|
|
|
|
|
|
|
return LinuxError.EOPNOTSUPP;
|
|
|
|
|
}
|
2022-01-12 13:31:08 -05:00
|
|
|
|
}
|
2022-12-12 08:59:31 -05:00
|
|
|
|
}
|