ManagedSocketPollManager.cs 3.7 KB

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