mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Fix cancellation issues when connecting.
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
* [Server] Improved message processing performance (+ ~5%).
|
||||
* [Server] Fix wrong usage of client session items for undelivered messages.
|
||||
|
||||
Git commit: $gitCommit
|
||||
Git commit: $gitCommit
|
||||
</releaseNotes>
|
||||
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
||||
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor</tags>
|
||||
|
||||
@@ -13,8 +13,8 @@ namespace MQTTnet.Exceptions
|
||||
{
|
||||
}
|
||||
|
||||
public MqttCommunicationException(string message)
|
||||
: base(message)
|
||||
public MqttCommunicationException(string message, Exception innerException = null)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Exceptions;
|
||||
|
||||
namespace MQTTnet.Implementations
|
||||
{
|
||||
@@ -123,9 +124,19 @@ namespace MQTTnet.Implementations
|
||||
_networkStream = new NetworkStream(_socket, true);
|
||||
}
|
||||
}
|
||||
catch (SocketException socketException)
|
||||
{
|
||||
if (socketException.SocketErrorCode == SocketError.OperationAborted)
|
||||
{
|
||||
throw new OperationCanceledException();
|
||||
}
|
||||
|
||||
throw new MqttCommunicationException($"Error while connecting with host '{host}:{port}'.", socketException);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// This will happen when _socket.EndConnect gets called by Task library but the socket is already disposed.
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,14 +29,15 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(OperationCanceledException))]
|
||||
public async Task Try_Connect_Invalid_Host()
|
||||
{
|
||||
var crossPlatformSocket = new CrossPlatformSocket();
|
||||
|
||||
var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(3));
|
||||
var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
cancellationToken.Token.Register(() => crossPlatformSocket.Dispose());
|
||||
|
||||
await crossPlatformSocket.ConnectAsync("www.google.de", 1234, CancellationToken.None);
|
||||
await crossPlatformSocket.ConnectAsync("www.google.de", 54321, cancellationToken.Token);
|
||||
}
|
||||
|
||||
//[TestMethod]
|
||||
|
||||
Reference in New Issue
Block a user