NetMainLoop.cs 4.8 KB

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