diff --git a/nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Controller.cs b/nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Controller.cs index efe394e8..97b9be7f 100644 --- a/nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Controller.cs +++ b/nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Controller.cs @@ -14,6 +14,7 @@ public class Controller : IControllerLocal { private ushort lastOutboundMessage; private readonly Semaphore _sendSemaphore = new Semaphore(1, 1); + private readonly Semaphore _receiveSemaphore = new Semaphore(1, 1); private readonly int nextEndpointId; private readonly object incrementLock = new object(); @@ -46,7 +47,10 @@ private void ProcessExit() public async Task SendAsync(MessageRaw raw, CancellationToken cancellationToken) { - _sendSemaphore.WaitOne(); + if(!_sendSemaphore.WaitOne()) + { + return false; + } // check for cancellation request if (cancellationToken.IsCancellationRequested) @@ -149,6 +153,11 @@ private async Task SendRawBufferAsync(byte[] buffer, TimeSpan waiTimeout, internal async Task ReadBufferAsync(byte[] buffer, int offset, int bytesToRead, TimeSpan waitTimeout, CancellationToken cancellationToken) { + if (!_receiveSemaphore.WaitOne()) + { + return 0; + } + // check for cancellation request if (cancellationToken.IsCancellationRequested) { @@ -194,6 +203,10 @@ internal async Task ReadBufferAsync(byte[] buffer, int offset, int bytesToR // catch everything else, doesn't matter return 0; } + finally + { + _receiveSemaphore.Release(); + } return bytesToReadRequested - bytesToRead; }