NetMainLoop.cs 4.6 KB

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