public async Task <IEnumerable <byte> > TransceiveAsync(byte[] request, CancellationToken cancellationToken) { try { return(await _serialPortTransceiverImplementation.TransceiveAsync(request, cancellationToken)); } catch (Exception first) { try { return(await _serialPortTransceiverImplementation.TransceiveAsync(request, cancellationToken)); } catch (Exception second) { throw new AggregateException(first, second); } } }
public async Task <IEnumerable <byte> > TransceiveAsync(byte[] request, CancellationToken cancellationToken) { var differenceWithStamp = _lastRequestStamp + _minimalTime - DateTime.Now; if (differenceWithStamp > TimeSpan.Zero) { await Task.Delay(differenceWithStamp, cancellationToken); } try { return(await _serialPortTransceiverImplementation.TransceiveAsync(request, cancellationToken)); } finally { _lastRequestStamp = DateTime.Now; } }
public async Task <IEnumerable <byte> > TransceiveAsync(byte[] request, CancellationToken cancellationToken) { var wasOpenBefore = _inner.IsOpen; if (!wasOpenBefore) { _inner.Open(); } var result = await _inner.TransceiveAsync(request, cancellationToken); if (PortMustBeClosed(wasOpenBefore)) { _inner.Close(); } return(result); }
public async Task <IEnumerable <byte> > TransceiveAsync(byte[] request, CancellationToken cancellationToken) { var transceivingTaskTokenSource = new CancellationTokenSource(); cancellationToken.Register(transceivingTaskTokenSource.Cancel); var cancelTask = Task .Delay(_maxResponseTime, transceivingTaskTokenSource.Token) .ContinueWith(_ => Enumerable.Empty <byte>(), transceivingTaskTokenSource.Token); var transceivingTask = _transceiver.TransceiveAsync(request, transceivingTaskTokenSource.Token); var completed = await Task.WhenAny(cancelTask, transceivingTask); if (completed == cancelTask && !transceivingTask.IsCompleted) { transceivingTaskTokenSource.Cancel(); } return(await completed); }
public async Task <IEnumerable <byte> > TransceiveAsync(byte[] request, CancellationToken cancellationToken) => await _inner.TransceiveAsync(request, cancellationToken);