Exemplo n.º 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);
         }
     }
 }
Exemplo n.º 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;
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 5
0
 public async Task <IEnumerable <byte> > TransceiveAsync(byte[] request, CancellationToken cancellationToken)
 => await _inner.TransceiveAsync(request, cancellationToken);