NetMainLoop.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. private readonly CancellationTokenSource _eventReadyTokenSource = new ();
  16. private readonly CancellationTokenSource _inputHandlerTokenSource = new ();
  17. private readonly ManualResetEventSlim _waitForProbe = new (false);
  18. private readonly ConcurrentQueue<NetEvents.InputResult> _resultQueue = new ();
  19. private MainLoop? _mainLoop;
  20. /// <summary>Initializes the class with the console driver.</summary>
  21. /// <remarks>Passing a IConsoleDriver is provided to capture windows resizing.</remarks>
  22. /// <param name="consoleDriver">The console driver used by this Net main loop.</param>
  23. /// <exception cref="ArgumentNullException"></exception>
  24. public NetMainLoop (IConsoleDriver consoleDriver)
  25. {
  26. ArgumentNullException.ThrowIfNull (consoleDriver);
  27. if (!ConsoleDriver.RunningUnitTests)
  28. {
  29. _netEvents = new (consoleDriver);
  30. }
  31. }
  32. void IMainLoopDriver.Setup (MainLoop mainLoop)
  33. {
  34. _mainLoop = mainLoop;
  35. if (!ConsoleDriver.RunningUnitTests)
  36. {
  37. Task.Run (NetInputHandler, _inputHandlerTokenSource.Token);
  38. }
  39. }
  40. void IMainLoopDriver.Wakeup () { _eventReady.Set (); }
  41. bool IMainLoopDriver.EventsPending ()
  42. {
  43. if (ConsoleDriver.RunningUnitTests)
  44. {
  45. return true;
  46. }
  47. _waitForProbe.Set ();
  48. if (_resultQueue.Count > 0 || _mainLoop!.CheckTimersAndIdleHandlers (out int waitTimeout))
  49. {
  50. return true;
  51. }
  52. try
  53. {
  54. if (!_eventReadyTokenSource.IsCancellationRequested)
  55. {
  56. // Note: ManualResetEventSlim.Wait will wait indefinitely if the timeout is -1. The timeout is -1 when there
  57. // are no timers, but there IS an idle handler waiting.
  58. _eventReady.Wait (waitTimeout, _eventReadyTokenSource.Token);
  59. }
  60. }
  61. catch (OperationCanceledException)
  62. {
  63. return true;
  64. }
  65. finally
  66. {
  67. _eventReady.Reset ();
  68. }
  69. _eventReadyTokenSource.Token.ThrowIfCancellationRequested ();
  70. if (!_eventReadyTokenSource.IsCancellationRequested)
  71. {
  72. return _resultQueue.Count > 0 || _mainLoop.CheckTimersAndIdleHandlers (out _);
  73. }
  74. // If cancellation was requested then always return true
  75. return true;
  76. }
  77. void IMainLoopDriver.Iteration ()
  78. {
  79. while (!ConsoleDriver.RunningUnitTests && _resultQueue.TryDequeue (out NetEvents.InputResult inputRecords))
  80. {
  81. ProcessInput?.Invoke (inputRecords);
  82. }
  83. }
  84. void IMainLoopDriver.TearDown ()
  85. {
  86. _inputHandlerTokenSource.Cancel ();
  87. _inputHandlerTokenSource.Dispose ();
  88. _eventReadyTokenSource.Cancel ();
  89. _eventReadyTokenSource.Dispose ();
  90. _eventReady.Dispose ();
  91. _waitForProbe.Dispose ();
  92. _resultQueue.Clear ();
  93. _netEvents?.Dispose ();
  94. _netEvents = null;
  95. _mainLoop = null;
  96. }
  97. private void NetInputHandler ()
  98. {
  99. while (_mainLoop is { })
  100. {
  101. try
  102. {
  103. if (!_inputHandlerTokenSource.IsCancellationRequested)
  104. {
  105. try
  106. {
  107. _waitForProbe.Wait (_inputHandlerTokenSource.Token);
  108. }
  109. catch (Exception ex)
  110. {
  111. if (ex is OperationCanceledException or ObjectDisposedException)
  112. {
  113. return;
  114. }
  115. throw;
  116. }
  117. _waitForProbe.Reset ();
  118. }
  119. ProcessInputQueue ();
  120. }
  121. catch (OperationCanceledException)
  122. {
  123. return;
  124. }
  125. }
  126. }
  127. private void ProcessInputQueue ()
  128. {
  129. if (_resultQueue.Count == 0)
  130. {
  131. NetEvents.InputResult? result = _netEvents!.DequeueInput ();
  132. if (result.HasValue)
  133. {
  134. _resultQueue.Enqueue (result.Value);
  135. _eventReady.Set ();
  136. }
  137. }
  138. }
  139. }