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