EventFileDescriptorPollManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Types;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl
  6. {
  7. class EventFileDescriptorPollManager : IPollManager
  8. {
  9. private static EventFileDescriptorPollManager _instance;
  10. public static EventFileDescriptorPollManager Instance
  11. {
  12. get
  13. {
  14. if (_instance == null)
  15. {
  16. _instance = new EventFileDescriptorPollManager();
  17. }
  18. return _instance;
  19. }
  20. }
  21. public bool IsCompatible(PollEvent evnt)
  22. {
  23. return evnt.FileDescriptor is EventFileDescriptor;
  24. }
  25. public LinuxError Poll(List<PollEvent> events, int timeoutMilliseconds, out int updatedCount)
  26. {
  27. updatedCount = 0;
  28. List<ManualResetEvent> waiters = new List<ManualResetEvent>();
  29. for (int i = 0; i < events.Count; i++)
  30. {
  31. PollEvent evnt = events[i];
  32. EventFileDescriptor socket = (EventFileDescriptor)evnt.FileDescriptor;
  33. bool isValidEvent = false;
  34. if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Input) ||
  35. evnt.Data.InputEvents.HasFlag(PollEventTypeMask.UrgentInput))
  36. {
  37. waiters.Add(socket.ReadEvent);
  38. isValidEvent = true;
  39. }
  40. if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Output))
  41. {
  42. waiters.Add(socket.WriteEvent);
  43. isValidEvent = true;
  44. }
  45. if (!isValidEvent)
  46. {
  47. Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported Poll input event type: {evnt.Data.InputEvents}");
  48. return LinuxError.EINVAL;
  49. }
  50. }
  51. int index = WaitHandle.WaitAny(waiters.ToArray(), timeoutMilliseconds);
  52. if (index != WaitHandle.WaitTimeout)
  53. {
  54. for (int i = 0; i < events.Count; i++)
  55. {
  56. PollEventTypeMask outputEvents = 0;
  57. PollEvent evnt = events[i];
  58. EventFileDescriptor socket = (EventFileDescriptor)evnt.FileDescriptor;
  59. if (socket.ReadEvent.WaitOne(0))
  60. {
  61. if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Input))
  62. {
  63. outputEvents |= PollEventTypeMask.Input;
  64. }
  65. if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.UrgentInput))
  66. {
  67. outputEvents |= PollEventTypeMask.UrgentInput;
  68. }
  69. }
  70. if ((evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Output))
  71. && socket.WriteEvent.WaitOne(0))
  72. {
  73. outputEvents |= PollEventTypeMask.Output;
  74. }
  75. if (outputEvents != 0)
  76. {
  77. evnt.Data.OutputEvents = outputEvents;
  78. updatedCount++;
  79. }
  80. }
  81. }
  82. else
  83. {
  84. return LinuxError.ETIMEDOUT;
  85. }
  86. return LinuxError.SUCCESS;
  87. }
  88. public LinuxError Select(List<PollEvent> events, int timeout, out int updatedCount)
  89. {
  90. // TODO: Implement Select for event file descriptors
  91. updatedCount = 0;
  92. return LinuxError.EOPNOTSUPP;
  93. }
  94. }
  95. }