BsdContext.cs 3.5 KB

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