diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index 6ea060d4..a843b524 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -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 Copyright Christian Kratky 2016-2020 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 diff --git a/Source/MQTTnet/Exceptions/MqttCommunicationException.cs b/Source/MQTTnet/Exceptions/MqttCommunicationException.cs index 4631de83..302107ae 100644 --- a/Source/MQTTnet/Exceptions/MqttCommunicationException.cs +++ b/Source/MQTTnet/Exceptions/MqttCommunicationException.cs @@ -13,8 +13,8 @@ namespace MQTTnet.Exceptions { } - public MqttCommunicationException(string message) - : base(message) + public MqttCommunicationException(string message, Exception innerException = null) + : base(message, innerException) { } } diff --git a/Source/MQTTnet/Implementations/CrossPlatformSocket.cs b/Source/MQTTnet/Implementations/CrossPlatformSocket.cs index 5f3fb333..18d2d892 100644 --- a/Source/MQTTnet/Implementations/CrossPlatformSocket.cs +++ b/Source/MQTTnet/Implementations/CrossPlatformSocket.cs @@ -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(); } } diff --git a/Tests/MQTTnet.Core.Tests/CrossPlatformSocket_Tests.cs b/Tests/MQTTnet.Core.Tests/CrossPlatformSocket_Tests.cs index 27f38e5c..c59e40c3 100644 --- a/Tests/MQTTnet.Core.Tests/CrossPlatformSocket_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/CrossPlatformSocket_Tests.cs @@ -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]