BsdContext.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Types;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Numerics;
  6. namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
  7. {
  8. class BsdContext
  9. {
  10. private static ConcurrentDictionary<ulong, BsdContext> _registry = new ConcurrentDictionary<ulong, BsdContext>();
  11. private readonly object _lock = new object();
  12. private List<IFileDescriptor> _fds;
  13. private BsdContext()
  14. {
  15. _fds = new List<IFileDescriptor>();
  16. }
  17. public ISocket RetrieveSocket(int socketFd)
  18. {
  19. IFileDescriptor file = RetrieveFileDescriptor(socketFd);
  20. if (file is ISocket socket)
  21. {
  22. return socket;
  23. }
  24. return null;
  25. }
  26. public IFileDescriptor RetrieveFileDescriptor(int fd)
  27. {
  28. lock (_lock)
  29. {
  30. if (fd >= 0 && _fds.Count > fd)
  31. {
  32. return _fds[fd];
  33. }
  34. }
  35. return null;
  36. }
  37. public List<IFileDescriptor> RetrieveFileDescriptorsFromMask(ReadOnlySpan<byte> mask)
  38. {
  39. List<IFileDescriptor> fds = new();
  40. for (int i = 0; i < mask.Length; i++)
  41. {
  42. byte current = mask[i];
  43. while (current != 0)
  44. {
  45. int bit = BitOperations.TrailingZeroCount(current);
  46. current &= (byte)~(1 << bit);
  47. int fd = i * 8 + bit;
  48. fds.Add(RetrieveFileDescriptor(fd));
  49. }
  50. }
  51. return fds;
  52. }
  53. public int RegisterFileDescriptor(IFileDescriptor file)
  54. {
  55. lock (_lock)
  56. {
  57. for (int fd = 0; fd < _fds.Count; fd++)
  58. {
  59. if (_fds[fd] == null)
  60. {
  61. _fds[fd] = file;
  62. return fd;
  63. }
  64. }
  65. _fds.Add(file);
  66. return _fds.Count - 1;
  67. }
  68. }
  69. public void BuildMask(List<IFileDescriptor> fds, Span<byte> mask)
  70. {
  71. foreach (IFileDescriptor descriptor in fds)
  72. {
  73. int fd = _fds.IndexOf(descriptor);
  74. mask[fd >> 3] |= (byte)(1 << (fd & 7));
  75. }
  76. }
  77. public int DuplicateFileDescriptor(int fd)
  78. {
  79. IFileDescriptor oldFile = RetrieveFileDescriptor(fd);
  80. if (oldFile != null)
  81. {
  82. lock (_lock)
  83. {
  84. oldFile.Refcount++;
  85. return RegisterFileDescriptor(oldFile);
  86. }
  87. }
  88. return -1;
  89. }
  90. public bool CloseFileDescriptor(int fd)
  91. {
  92. IFileDescriptor file = RetrieveFileDescriptor(fd);
  93. if (file != null)
  94. {
  95. file.Refcount--;
  96. if (file.Refcount <= 0)
  97. {
  98. file.Dispose();
  99. }
  100. lock (_lock)
  101. {
  102. _fds[fd] = null;
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. public LinuxError ShutdownAllSockets(BsdSocketShutdownFlags how)
  109. {
  110. lock (_lock)
  111. {
  112. foreach (IFileDescriptor file in _fds)
  113. {
  114. if (file is ISocket socket)
  115. {
  116. LinuxError errno = socket.Shutdown(how);
  117. if (errno != LinuxError.SUCCESS)
  118. {
  119. return errno;
  120. }
  121. }
  122. }
  123. }
  124. return LinuxError.SUCCESS;
  125. }
  126. public static BsdContext GetOrRegister(ulong processId)
  127. {
  128. BsdContext context = GetContext(processId);
  129. if (context == null)
  130. {
  131. context = new BsdContext();
  132. _registry.TryAdd(processId, context);
  133. }
  134. return context;
  135. }
  136. public static BsdContext GetContext(ulong processId)
  137. {
  138. if (!_registry.TryGetValue(processId, out BsdContext processContext))
  139. {
  140. return null;
  141. }
  142. return processContext;
  143. }
  144. }
  145. }