NetMainLoop.cs 4.4 KB

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