Fix cancellation issues when connecting.

This commit is contained in:
Christian
2021-01-01 15:56:38 +01:00
parent 74e952bb03
commit 75845d6416
4 changed files with 17 additions and 5 deletions
+1 -1
View File
@@ -26,7 +26,7 @@
* [Server] Improved message processing performance (+ ~5%). * [Server] Improved message processing performance (+ ~5%).
* [Server] Fix wrong usage of client session items for undelivered messages. * [Server] Fix wrong usage of client session items for undelivered messages.
Git commit: $gitCommit Git commit: $gitCommit
</releaseNotes> </releaseNotes>
<copyright>Copyright Christian Kratky 2016-2020</copyright> <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> <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) public MqttCommunicationException(string message, Exception innerException = null)
: base(message) : base(message, innerException)
{ {
} }
} }
@@ -4,6 +4,7 @@ using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MQTTnet.Exceptions;
namespace MQTTnet.Implementations namespace MQTTnet.Implementations
{ {
@@ -123,9 +124,19 @@ namespace MQTTnet.Implementations
_networkStream = new NetworkStream(_socket, true); _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) catch (ObjectDisposedException)
{ {
// This will happen when _socket.EndConnect gets called by Task library but the socket is already disposed. // 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] [TestMethod]
[ExpectedException(typeof(OperationCanceledException))]
public async Task Try_Connect_Invalid_Host() public async Task Try_Connect_Invalid_Host()
{ {
var crossPlatformSocket = new CrossPlatformSocket(); var crossPlatformSocket = new CrossPlatformSocket();
var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(3)); var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(5));
cancellationToken.Token.Register(() => crossPlatformSocket.Dispose()); cancellationToken.Token.Register(() => crossPlatformSocket.Dispose());
await crossPlatformSocket.ConnectAsync("www.google.de", 1234, CancellationToken.None); await crossPlatformSocket.ConnectAsync("www.google.de", 54321, cancellationToken.Token);
} }
//[TestMethod] //[TestMethod]