ManagedSocketPollManager.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Types;
  3. using System.Collections.Generic;
  4. using System.Net.Sockets;
  5. namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl
  6. {
  7. class ManagedSocketPollManager : IPollManager
  8. {
  9. private static ManagedSocketPollManager _instance;
  10. public static ManagedSocketPollManager Instance
  11. {
  12. get
  13. {
  14. if (_instance == null)
  15. {
  16. _instance = new ManagedSocketPollManager();
  17. }
  18. return _instance;
  19. }
  20. }
  21. public bool IsCompatible(PollEvent evnt)
  22. {
  23. return evnt.FileDescriptor is ManagedSocket;
  24. }
  25. public LinuxError Poll(List<PollEvent> events, int timeoutMilliseconds, out int updatedCount)
  26. {
  27. List<Socket> readEvents = new List<Socket>();
  28. List<Socket> writeEvents = new List<Socket>();
  29. List<Socket> errorEvents = new List<Socket>();
  30. updatedCount = 0;
  31. foreach (PollEvent evnt in events)
  32. {
  33. ManagedSocket socket = (ManagedSocket)evnt.FileDescriptor;
  34. bool isValidEvent = evnt.Data.InputEvents == 0;
  35. errorEvents.Add(socket.Socket);
  36. if ((evnt.Data.InputEvents & PollEventTypeMask.Input) != 0)
  37. {
  38. readEvents.Add(socket.Socket);
  39. isValidEvent = true;
  40. }
  41. if ((evnt.Data.InputEvents & PollEventTypeMask.UrgentInput) != 0)
  42. {
  43. readEvents.Add(socket.Socket);
  44. isValidEvent = true;
  45. }
  46. if ((evnt.Data.InputEvents & PollEventTypeMask.Output) != 0)
  47. {
  48. writeEvents.Add(socket.Socket);
  49. isValidEvent = true;
  50. }
  51. if (!isValidEvent)
  52. {
  53. Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported Poll input event type: {evnt.Data.InputEvents}");
  54. return LinuxError.EINVAL;
  55. }
  56. }
  57. try
  58. {
  59. int actualTimeoutMicroseconds = timeoutMilliseconds == -1 ? -1 : timeoutMilliseconds * 1000;
  60. Socket.Select(readEvents, writeEvents, errorEvents, actualTimeoutMicroseconds);
  61. }
  62. catch (SocketException exception)
  63. {
  64. return WinSockHelper.ConvertError((WsaError)exception.ErrorCode);
  65. }
  66. foreach (PollEvent evnt in events)
  67. {
  68. Socket socket = ((ManagedSocket)evnt.FileDescriptor).Socket;
  69. PollEventTypeMask outputEvents = evnt.Data.OutputEvents & ~evnt.Data.InputEvents;
  70. if (errorEvents.Contains(socket))
  71. {
  72. outputEvents |= PollEventTypeMask.Error;
  73. if (!socket.Connected || !socket.IsBound)
  74. {
  75. outputEvents |= PollEventTypeMask.Disconnected;
  76. }
  77. }
  78. if (readEvents.Contains(socket))
  79. {
  80. if ((evnt.Data.InputEvents & PollEventTypeMask.Input) != 0)
  81. {
  82. outputEvents |= PollEventTypeMask.Input;
  83. }
  84. }
  85. if (writeEvents.Contains(socket))
  86. {
  87. outputEvents |= PollEventTypeMask.Output;
  88. }
  89. evnt.Data.OutputEvents = outputEvents;
  90. }
  91. updatedCount = readEvents.Count + writeEvents.Count + errorEvents.Count;
  92. return LinuxError.SUCCESS;
  93. }
  94. public LinuxError Select(List<PollEvent> events, int timeout, out int updatedCount)
  95. {
  96. List<Socket> readEvents = new();
  97. List<Socket> writeEvents = new();
  98. List<Socket> errorEvents = new();
  99. updatedCount = 0;
  100. foreach (PollEvent pollEvent in events)
  101. {
  102. ManagedSocket socket = (ManagedSocket)pollEvent.FileDescriptor;
  103. if (pollEvent.Data.InputEvents.HasFlag(PollEventTypeMask.Input))
  104. {
  105. readEvents.Add(socket.Socket);
  106. }
  107. if (pollEvent.Data.InputEvents.HasFlag(PollEventTypeMask.Output))
  108. {
  109. writeEvents.Add(socket.Socket);
  110. }
  111. if (pollEvent.Data.InputEvents.HasFlag(PollEventTypeMask.Error))
  112. {
  113. errorEvents.Add(socket.Socket);
  114. }
  115. }
  116. Socket.Select(readEvents, writeEvents, errorEvents, timeout);
  117. updatedCount = readEvents.Count + writeEvents.Count + errorEvents.Count;
  118. foreach (PollEvent pollEvent in events)
  119. {
  120. ManagedSocket socket = (ManagedSocket)pollEvent.FileDescriptor;
  121. if (readEvents.Contains(socket.Socket))
  122. {
  123. pollEvent.Data.OutputEvents |= PollEventTypeMask.Input;
  124. }
  125. if (writeEvents.Contains(socket.Socket))
  126. {
  127. pollEvent.Data.OutputEvents |= PollEventTypeMask.Output;
  128. }
  129. if (errorEvents.Contains(socket.Socket))
  130. {
  131. pollEvent.Data.OutputEvents |= PollEventTypeMask.Error;
  132. }
  133. }
  134. return LinuxError.SUCCESS;
  135. }
  136. }
  137. }