Browse Source

Translate timeout exception

release/3.x.x
Israel Lot 6 years ago
parent
commit
9bc34e4406
2 changed files with 24 additions and 2 deletions
  1. +18
    -1
      Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs
  2. +6
    -1
      Frameworks/MQTTnet.NetStandard/Exceptions/MqttCommunicationTimedOutException.cs

+ 18
- 1
Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -111,7 +112,23 @@ namespace MQTTnet.Adapter
var timeoutCts = new CancellationTokenSource(timeout); var timeoutCts = new CancellationTokenSource(timeout);
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token); 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 else
{ {


+ 6
- 1
Frameworks/MQTTnet.NetStandard/Exceptions/MqttCommunicationTimedOutException.cs View File

@@ -1,6 +1,11 @@
namespace MQTTnet.Exceptions
using System;

namespace MQTTnet.Exceptions
{ {
public sealed class MqttCommunicationTimedOutException : MqttCommunicationException public sealed class MqttCommunicationTimedOutException : MqttCommunicationException
{ {
public MqttCommunicationTimedOutException() : base() { }
public MqttCommunicationTimedOutException(Exception innerException) : base(innerException) { }

} }
} }

Loading…
Cancel
Save