コード例 #1
0
 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);
         }
     }
 }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: SemaphoreTransceiver.cs プロジェクト: esandre/Lib
 public async Task <IEnumerable <byte> > TransceiveAsync(byte[] request, CancellationToken cancellationToken)
 => await _inner.TransceiveAsync(request, cancellationToken);