From 9bc34e44065550f8a433186df7cc7c2a58e57596 Mon Sep 17 00:00:00 2001 From: Israel Lot Date: Mon, 16 Apr 2018 13:07:19 -0300 Subject: [PATCH] Translate timeout exception --- .../Adapter/MqttChannelAdapter.cs | 19 ++++++++++++++++++- .../MqttCommunicationTimedOutException.cs | 7 ++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs index 8adac69..e853d6b 100644 --- a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Sockets; +using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -111,7 +112,23 @@ namespace MQTTnet.Adapter var timeoutCts = new CancellationTokenSource(timeout); var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token); - receivedMqttPacket = await ReceiveAsync(_channel.ReceiveStream, linkedCts.Token).ConfigureAwait(false); + try + { + receivedMqttPacket = await ReceiveAsync(_channel.ReceiveStream, linkedCts.Token).ConfigureAwait(false); + } + catch(OperationCanceledException ex) + { + //check if timed out + if(linkedCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested) + { + //only timeout token was cancelled + throw new MqttCommunicationTimedOutException(ex); + } + else + { + throw; + } + } } else { diff --git a/Frameworks/MQTTnet.NetStandard/Exceptions/MqttCommunicationTimedOutException.cs b/Frameworks/MQTTnet.NetStandard/Exceptions/MqttCommunicationTimedOutException.cs index 7d0adcd..744035e 100644 --- a/Frameworks/MQTTnet.NetStandard/Exceptions/MqttCommunicationTimedOutException.cs +++ b/Frameworks/MQTTnet.NetStandard/Exceptions/MqttCommunicationTimedOutException.cs @@ -1,6 +1,11 @@ -namespace MQTTnet.Exceptions +using System; + +namespace MQTTnet.Exceptions { public sealed class MqttCommunicationTimedOutException : MqttCommunicationException { + public MqttCommunicationTimedOutException() : base() { } + public MqttCommunicationTimedOutException(Exception innerException) : base(innerException) { } + } }