Browse Source

Change NetEvents to BlockingCollection

tznind 9 months ago
parent
commit
792170a029
1 changed files with 9 additions and 43 deletions
  1. 9 43
      Terminal.Gui/ConsoleDrivers/NetDriver.cs

+ 9 - 43
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -135,13 +135,12 @@ internal class NetWinVTConsole
 
 
 internal class NetEvents : IDisposable
 internal class NetEvents : IDisposable
 {
 {
-    private readonly ManualResetEventSlim _inputReady = new (false);
     private CancellationTokenSource _inputReadyCancellationTokenSource;
     private CancellationTokenSource _inputReadyCancellationTokenSource;
     private readonly ManualResetEventSlim _waitForStart = new (false);
     private readonly ManualResetEventSlim _waitForStart = new (false);
 
 
     //CancellationTokenSource _waitForStartCancellationTokenSource;
     //CancellationTokenSource _waitForStartCancellationTokenSource;
     private readonly ManualResetEventSlim _winChange = new (false);
     private readonly ManualResetEventSlim _winChange = new (false);
-    private readonly Queue<InputResult?> _inputQueue = new ();
+    private readonly BlockingCollection<InputResult?> _inputQueue = new (new ConcurrentQueue<InputResult?> ());
     private readonly ConsoleDriver _consoleDriver;
     private readonly ConsoleDriver _consoleDriver;
     private ConsoleKeyInfo [] _cki;
     private ConsoleKeyInfo [] _cki;
     private bool _isEscSeq;
     private bool _isEscSeq;
@@ -173,31 +172,9 @@ internal class NetEvents : IDisposable
             _waitForStart.Set ();
             _waitForStart.Set ();
             _winChange.Set ();
             _winChange.Set ();
 
 
-            try
-            {
-                if (!_inputReadyCancellationTokenSource.Token.IsCancellationRequested)
-                {
-                    if (_inputQueue.Count == 0)
-                    {
-                        _inputReady.Wait (_inputReadyCancellationTokenSource.Token);
-                    }
-                }
-            }
-            catch (OperationCanceledException)
-            {
-                return null;
-            }
-            finally
-            {
-                _inputReady.Reset ();
-            }
-
-#if PROCESS_REQUEST
-            _neededProcessRequest = false;
-#endif
-            if (_inputQueue.Count > 0)
+            if (_inputQueue.TryTake (out var item,-1,_inputReadyCancellationTokenSource.Token))
             {
             {
-                return _inputQueue.Dequeue ();
+                return item;
             }
             }
         }
         }
 
 
@@ -220,7 +197,6 @@ internal class NetEvents : IDisposable
             foreach (var k in ShouldRelease ())
             foreach (var k in ShouldRelease ())
             {
             {
                 ProcessMapConsoleKeyInfo (k);
                 ProcessMapConsoleKeyInfo (k);
-                _inputReady.Set ();
             }
             }
 
 
             if (Console.KeyAvailable)
             if (Console.KeyAvailable)
@@ -282,7 +258,6 @@ internal class NetEvents : IDisposable
                     }
                     }
                 }
                 }
             }
             }
-            _inputReady.Set ();
         }
         }
 
 
     }
     }
@@ -345,7 +320,7 @@ internal class NetEvents : IDisposable
 
 
     void ProcessMapConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
     void ProcessMapConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
     {
     {
-        _inputQueue.Enqueue (
+        _inputQueue.Add (
                              new InputResult
                              new InputResult
                              {
                              {
                                  EventType = EventType.Key, ConsoleKeyInfo = EscSeqUtils.MapConsoleKeyInfo (consoleKeyInfo)
                                  EventType = EventType.Key, ConsoleKeyInfo = EscSeqUtils.MapConsoleKeyInfo (consoleKeyInfo)
@@ -403,8 +378,6 @@ internal class NetEvents : IDisposable
             {
             {
                 return;
                 return;
             }
             }
-
-            _inputReady.Set ();
         }
         }
     }
     }
 
 
@@ -424,7 +397,7 @@ internal class NetEvents : IDisposable
         int w = Math.Max (winWidth, 0);
         int w = Math.Max (winWidth, 0);
         int h = Math.Max (winHeight, 0);
         int h = Math.Max (winHeight, 0);
 
 
-        _inputQueue.Enqueue (
+        _inputQueue.Add (
                              new InputResult
                              new InputResult
                              {
                              {
                                  EventType = EventType.WindowSize, WindowSizeEvent = new WindowSizeEvent { Size = new (w, h) }
                                  EventType = EventType.WindowSize, WindowSizeEvent = new WindowSizeEvent { Size = new (w, h) }
@@ -443,9 +416,6 @@ internal class NetEvents : IDisposable
 
 
         ProcessRequestResponse (ref newConsoleKeyInfo, ref key, obj.Select (v=>v.Item2).ToArray (),ref mod);
         ProcessRequestResponse (ref newConsoleKeyInfo, ref key, obj.Select (v=>v.Item2).ToArray (),ref mod);
 
 
-        // Probably
-        _inputReady.Set ();
-
         // Handled
         // Handled
         return true;
         return true;
     }
     }
@@ -647,7 +617,7 @@ internal class NetEvents : IDisposable
                     var eventType = EventType.WindowPosition;
                     var eventType = EventType.WindowPosition;
                     var winPositionEv = new WindowPositionEvent { CursorPosition = point };
                     var winPositionEv = new WindowPositionEvent { CursorPosition = point };
 
 
-                    _inputQueue.Enqueue (
+                    _inputQueue.Add (
                                          new InputResult { EventType = eventType, WindowPositionEvent = winPositionEv }
                                          new InputResult { EventType = eventType, WindowPositionEvent = winPositionEv }
                                         );
                                         );
                 }
                 }
@@ -682,8 +652,6 @@ internal class NetEvents : IDisposable
 
 
                 break;
                 break;
         }
         }
-
-        _inputReady.Set ();
     }
     }
 
 
     private void EnqueueRequestResponseEvent (string c1Control, string code, string [] values, string terminating)
     private void EnqueueRequestResponseEvent (string c1Control, string code, string [] values, string terminating)
@@ -691,7 +659,7 @@ internal class NetEvents : IDisposable
         var eventType = EventType.RequestResponse;
         var eventType = EventType.RequestResponse;
         var requestRespEv = new RequestResponseEvent { ResultTuple = (c1Control, code, values, terminating) };
         var requestRespEv = new RequestResponseEvent { ResultTuple = (c1Control, code, values, terminating) };
 
 
-        _inputQueue.Enqueue (
+        _inputQueue.Add (
                              new InputResult { EventType = eventType, RequestResponseEvent = requestRespEv }
                              new InputResult { EventType = eventType, RequestResponseEvent = requestRespEv }
                             );
                             );
     }
     }
@@ -700,11 +668,9 @@ internal class NetEvents : IDisposable
     {
     {
         var mouseEvent = new MouseEvent { Position = pos, ButtonState = buttonState };
         var mouseEvent = new MouseEvent { Position = pos, ButtonState = buttonState };
 
 
-        _inputQueue.Enqueue (
+        _inputQueue.Add  (
                              new InputResult { EventType = EventType.Mouse, MouseEvent = mouseEvent }
                              new InputResult { EventType = EventType.Mouse, MouseEvent = mouseEvent }
                             );
                             );
-
-        _inputReady.Set ();
     }
     }
 
 
     public enum EventType
     public enum EventType
@@ -817,7 +783,7 @@ internal class NetEvents : IDisposable
     {
     {
         var inputResult = new InputResult { EventType = EventType.Key, ConsoleKeyInfo = cki };
         var inputResult = new InputResult { EventType = EventType.Key, ConsoleKeyInfo = cki };
 
 
-        _inputQueue.Enqueue (inputResult);
+        _inputQueue.Add (inputResult);
     }
     }
 
 
     public void Dispose ()
     public void Dispose ()