NetMainLoop.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Mainloop intended to be used with the .NET System.Console API, and can be used on Windows and Unix, it is
  6. /// cross-platform but lacks things like file descriptor monitoring.
  7. /// </summary>
  8. /// <remarks>This implementation is used for NetDriver.</remarks>
  9. internal class NetMainLoop : IMainLoopDriver
  10. {
  11. internal NetEvents? _netEvents;
  12. /// <summary>Invoked when a Key is pressed.</summary>
  13. internal Action<NetEvents.InputResult>? ProcessInput;
  14. private readonly ManualResetEventSlim _eventReady = new (false);
  15. internal readonly ManualResetEventSlim _waitForProbe = new (false);
  16. private readonly CancellationTokenSource _eventReadyTokenSource = new ();
  17. private readonly CancellationTokenSource _inputHandlerTokenSource = new ();
  18. private readonly ConcurrentQueue<NetEvents.InputResult> _resultQueue = new ();
  19. private MainLoop? _mainLoop;
  20. bool IMainLoopDriver._forceRead { get; set; }
  21. ManualResetEventSlim IMainLoopDriver._waitForInput { get; set; } = new (false);
  22. /// <summary>Initializes the class with the console driver.</summary>
  23. /// <remarks>Passing a consoleDriver is provided to capture windows resizing.</remarks>
  24. /// <param name="consoleDriver">The console driver used by this Net main loop.</param>
  25. /// <exception cref="ArgumentNullException"></exception>
  26. public NetMainLoop (ConsoleDriver consoleDriver)
  27. {
  28. ArgumentNullException.ThrowIfNull (consoleDriver);
  29. if (!ConsoleDriver.RunningUnitTests)
  30. {
  31. _netEvents = new (consoleDriver);
  32. }
  33. }
  34. void IMainLoopDriver.Setup (MainLoop mainLoop)
  35. {
  36. _mainLoop = mainLoop;
  37. if (!ConsoleDriver.RunningUnitTests)
  38. {
  39. Task.Run (NetInputHandler, _inputHandlerTokenSource.Token);
  40. }
  41. }
  42. void IMainLoopDriver.Wakeup () { _eventReady.Set (); }
  43. bool IMainLoopDriver.EventsPending ()
  44. {
  45. _waitForProbe.Set ();
  46. if (_resultQueue.Count > 0 || _mainLoop!.CheckTimersAndIdleHandlers (out int waitTimeout))
  47. {
  48. return true;
  49. }
  50. try
  51. {
  52. if (!_eventReadyTokenSource.IsCancellationRequested)
  53. {
  54. // Note: ManualResetEventSlim.Wait will wait indefinitely if the timeout is -1. The timeout is -1 when there
  55. // are no timers, but there IS an idle handler waiting.
  56. _eventReady.Wait (waitTimeout, _eventReadyTokenSource.Token);
  57. _eventReadyTokenSource.Token.ThrowIfCancellationRequested ();
  58. if (!_eventReadyTokenSource.IsCancellationRequested)
  59. {
  60. return _resultQueue.Count > 0 || _mainLoop.CheckTimersAndIdleHandlers (out _);
  61. }
  62. }
  63. }
  64. catch (OperationCanceledException)
  65. {
  66. return true;
  67. }
  68. finally
  69. {
  70. _eventReady.Reset ();
  71. }
  72. // If cancellation was requested then always return true
  73. return true;
  74. }
  75. void IMainLoopDriver.Iteration ()
  76. {
  77. while (_resultQueue.TryDequeue (out NetEvents.InputResult inputRecords))
  78. {
  79. ProcessInput?.Invoke (inputRecords);
  80. }
  81. }
  82. void IMainLoopDriver.TearDown ()
  83. {
  84. _inputHandlerTokenSource.Cancel ();
  85. _inputHandlerTokenSource.Dispose ();
  86. _eventReadyTokenSource.Cancel ();
  87. _eventReadyTokenSource.Dispose ();
  88. _eventReady.Dispose ();
  89. _waitForProbe.Dispose ();
  90. _resultQueue.Clear ();
  91. _netEvents?.Dispose ();
  92. _netEvents = null;
  93. _mainLoop = null;
  94. }
  95. private void NetInputHandler ()
  96. {
  97. while (_mainLoop is { })
  98. {
  99. try
  100. {
  101. if (!_inputHandlerTokenSource.IsCancellationRequested && !((IMainLoopDriver)this)._forceRead)
  102. {
  103. try
  104. {
  105. _waitForProbe.Wait (_inputHandlerTokenSource.Token);
  106. }
  107. catch (OperationCanceledException)
  108. {
  109. return;
  110. }
  111. _waitForProbe.Reset ();
  112. }
  113. ProcessInputQueue ();
  114. }
  115. catch (OperationCanceledException)
  116. {
  117. return;
  118. }
  119. }
  120. }
  121. private void ProcessInputQueue ()
  122. {
  123. if (_resultQueue?.Count == 0 || ((IMainLoopDriver)this)._forceRead)
  124. {
  125. NetEvents.InputResult? result = _netEvents!.DequeueInput ();
  126. if (result.HasValue)
  127. {
  128. _resultQueue?.Enqueue (result.Value);
  129. _eventReady.Set ();
  130. }
  131. }
  132. }
  133. }