NetMainLoop.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 _inputHandlerTokenSource = new ();
  16. private readonly BlockingCollection<NetEvents.InputResult> _resultQueue = new (new ConcurrentQueue<NetEvents.InputResult> ());
  17. private readonly CancellationTokenSource _eventReadyTokenSource = 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. _netEvents = new (consoleDriver);
  27. }
  28. void IMainLoopDriver.Setup (MainLoop mainLoop)
  29. {
  30. _mainLoop = mainLoop;
  31. if (ConsoleDriver.RunningUnitTests)
  32. {
  33. return;
  34. }
  35. Task.Run (NetInputHandler, _inputHandlerTokenSource.Token);
  36. }
  37. void IMainLoopDriver.Wakeup () { _eventReady.Set (); }
  38. bool IMainLoopDriver.EventsPending ()
  39. {
  40. if (_resultQueue.Count > 0 || _mainLoop!.CheckTimersAndIdleHandlers (out int waitTimeout))
  41. {
  42. return true;
  43. }
  44. try
  45. {
  46. if (!_eventReadyTokenSource.IsCancellationRequested)
  47. {
  48. // Note: ManualResetEventSlim.Wait will wait indefinitely if the timeout is -1. The timeout is -1 when there
  49. // are no timers, but there IS an idle handler waiting.
  50. _eventReady.Wait (waitTimeout, _eventReadyTokenSource.Token);
  51. }
  52. }
  53. catch (OperationCanceledException)
  54. {
  55. return true;
  56. }
  57. finally
  58. {
  59. _eventReady.Reset ();
  60. }
  61. _eventReadyTokenSource.Token.ThrowIfCancellationRequested ();
  62. if (!_eventReadyTokenSource.IsCancellationRequested)
  63. {
  64. return _resultQueue.Count > 0 || _mainLoop.CheckTimersAndIdleHandlers (out _);
  65. }
  66. // If cancellation was requested then always return true
  67. return true;
  68. }
  69. void IMainLoopDriver.Iteration ()
  70. {
  71. while (_resultQueue.Count > 0)
  72. {
  73. // Always dequeue even if it's null and invoke if isn't null
  74. if (_resultQueue.TryTake (out NetEvents.InputResult dequeueResult))
  75. {
  76. ProcessInput?.Invoke (dequeueResult);
  77. }
  78. }
  79. }
  80. void IMainLoopDriver.TearDown ()
  81. {
  82. _inputHandlerTokenSource.Cancel ();
  83. _inputHandlerTokenSource.Dispose ();
  84. _eventReadyTokenSource.Cancel ();
  85. _eventReadyTokenSource.Dispose ();
  86. _eventReady.Dispose ();
  87. _resultQueue.Dispose();
  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)
  99. {
  100. return;
  101. }
  102. if (_resultQueue?.Count == 0 || _netEvents!._forceRead)
  103. {
  104. NetEvents.InputResult? result = _netEvents!.DequeueInput ();
  105. if (result.HasValue)
  106. {
  107. _resultQueue?.Add (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. }
  120. }
  121. }