0
0
Fork 0
This repository has been archived on 2024-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ryujinx-final/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Impl/EventFileDescriptor.cs
Mary 366fe2dbb2
bsd: Revamp API and make socket abstract (#2960)
* bsd: Revamp API and make socket abstract

This part of the code was really ancient and needed some love.
As such this commit aims at separating the socket core logic from the IClient class and make it uses more modern APIs to read/write/parse data.

* Address gdkchan's comment

* Move TryConvertSocketOption to WinSockHelper

* Allow reusing old fds and add missing locks around SocketInternal and ShutdownAllSockets

* bsd: ton of changes

- Make sockets per process
- Implement eventfds
- Rework Poll for support of eventfds
- Handle protocol auto selection by type (used by gRPC)
- Handle IPv6 socket creation

* Address most of gdkchan comments

* Fix inverted read logic for BSD socket read

* bsd: Make Poll abstract via IBsdSocketPollManager

* bsd: Improve naming of everything

* Fix build issue from last commit (missed to save on VC)

* Switch BsdContext registry to a concurrent dictionary

* bsd: Implement socket creation flags logic and the non blocking flag

* Remove unused enum from previous commit

* bsd: Fix poll logic when 0 fds are present for a given poll manager and when timeout is very small (or 0)

* Address gdkchan's comment
2022-01-12 19:31:08 +01:00

130 lines
3.2 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
{
class EventFileDescriptor : IFileDescriptor
{
private ulong _value;
private readonly EventFdFlags _flags;
private AutoResetEvent _event;
private object _lock = new object();
public bool Blocking { get => !_flags.HasFlag(EventFdFlags.NonBlocking); set => throw new NotSupportedException(); }
public ManualResetEvent WriteEvent { get; }
public ManualResetEvent ReadEvent { get; }
public EventFileDescriptor(ulong value, EventFdFlags flags)
{
_value = value;
_flags = flags;
_event = new AutoResetEvent(false);
WriteEvent = new ManualResetEvent(true);
ReadEvent = new ManualResetEvent(true);
}
public int Refcount { get; set; }
public void Dispose()
{
_event.Dispose();
WriteEvent.Dispose();
ReadEvent.Dispose();
}
public LinuxError Read(out int readSize, Span<byte> buffer)
{
if (buffer.Length < sizeof(ulong))
{
readSize = 0;
return LinuxError.EINVAL;
}
ReadEvent.Reset();
lock (_lock)
{
ref ulong count = ref MemoryMarshal.Cast<byte, ulong>(buffer)[0];
if (_value == 0)
{
if (Blocking)
{
while (_value == 0)
{
_event.WaitOne();
}
}
else
{
readSize = 0;
return LinuxError.EAGAIN;
}
}
readSize = sizeof(ulong);
if (_flags.HasFlag(EventFdFlags.Semaphore))
{
--_value;
count = 1;
}
else
{
count = _value;
_value = 0;
}
ReadEvent.Set();
return LinuxError.SUCCESS;
}
}
public LinuxError Write(out int writeSize, ReadOnlySpan<byte> buffer)
{
if (!MemoryMarshal.TryRead(buffer, out ulong count) || count == ulong.MaxValue)
{
writeSize = 0;
return LinuxError.EINVAL;
}
WriteEvent.Reset();
lock (_lock)
{
if (_value > _value + count)
{
if (Blocking)
{
_event.WaitOne();
}
else
{
writeSize = 0;
return LinuxError.EAGAIN;
}
}
writeSize = sizeof(ulong);
_value += count;
_event.Set();
WriteEvent.Set();
return LinuxError.SUCCESS;
}
}
}
}