From 9d9076ba77b127629ab4b32b82785477faa37657 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat, 29 Apr 2023 13:47:38 +0200 Subject: [PATCH] Fix build --- .../MqttClientAuthenticationHandler.cs | 21 ++++++++++++++--- .../Internal/MqttClientKeepAliveHandler.cs | 2 +- Source/MQTTnet/Client/MqttClient.cs | 22 +++++++++--------- .../Client/Options/MqttClientOptions.cs | 23 +++++++++++-------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/Source/MQTTnet/Client/Internal/MqttClientAuthenticationHandler.cs b/Source/MQTTnet/Client/Internal/MqttClientAuthenticationHandler.cs index 27846d00..1d6fb4a5 100644 --- a/Source/MQTTnet/Client/Internal/MqttClientAuthenticationHandler.cs +++ b/Source/MQTTnet/Client/Internal/MqttClientAuthenticationHandler.cs @@ -36,6 +36,16 @@ namespace MQTTnet.Client.Internal public async Task Authenticate(IMqttChannelAdapter channelAdapter, MqttClientOptions options, CancellationToken cancellationToken) { + if (channelAdapter == null) + { + throw new ArgumentNullException(nameof(channelAdapter)); + } + + if (options == null) + { + throw new ArgumentNullException(nameof(options)); + } + // From the RFC: // The CONNACK packet is the packet sent by the Server in response to a CONNECT packet received from a Client. // The Server MUST send a CONNACK with a 0x00 (Success) Reason Code before sending any Packet other than AUTH [MQTT-3.2.0-1]. @@ -83,10 +93,15 @@ namespace MQTTnet.Client.Internal throw new MqttConnectingFailedException($"Error while authenticating. {exception.Message}", exception, null); } - // TODO: Remove because it is wrong. - if (result.ResultCode != MqttClientConnectResultCode.Success) + // This is no feature. It is basically a backward compatibility option and should be removed in the future. + // The client should not throw any exception if the transport layer connection was successful and the server + // did send a proper ACK packet. + if (options.ThrowOnNonSuccessfulResponseFromServer) { - throw new MqttConnectingFailedException($"Connecting with MQTT server failed ({result.ResultCode}).", null, result); + if (result.ResultCode != MqttClientConnectResultCode.Success) + { + throw new MqttConnectingFailedException($"Connecting with MQTT server failed ({result.ResultCode}).", null, result); + } } _logger.Verbose("Authenticated MQTT connection with server established."); diff --git a/Source/MQTTnet/Client/Internal/MqttClientKeepAliveHandler.cs b/Source/MQTTnet/Client/Internal/MqttClientKeepAliveHandler.cs index 3e3018a8..24ef16f0 100644 --- a/Source/MQTTnet/Client/Internal/MqttClientKeepAliveHandler.cs +++ b/Source/MQTTnet/Client/Internal/MqttClientKeepAliveHandler.cs @@ -97,7 +97,7 @@ namespace MQTTnet.Client.Internal } catch (Exception exception) { - if (_cleanDisconnectInitiated) + if (_client._cleanDisconnectInitiated) { return; } diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 317ef0a1..a85105b1 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -43,7 +43,7 @@ namespace MQTTnet.Client IMqttChannelAdapter _adapter; - bool _cleanDisconnectInitiated; + internal bool _cleanDisconnectInitiated; CancellationTokenSource _clientAlive; volatile int _connectionStatus; @@ -234,13 +234,13 @@ namespace MQTTnet.Client { if (cancellationToken.CanBeCanceled) { - await SendAndReceiveAsync(MqttPingReqPacket.Instance, cancellationToken).ConfigureAwait(false); + await Request(MqttPingReqPacket.Instance, cancellationToken).ConfigureAwait(false); } else { using (var timeout = new CancellationTokenSource(Options.Timeout)) { - await SendAndReceiveAsync(MqttPingReqPacket.Instance, timeout.Token).ConfigureAwait(false); + await Request(MqttPingReqPacket.Instance, timeout.Token).ConfigureAwait(false); } } } @@ -330,13 +330,13 @@ namespace MQTTnet.Client MqttSubAckPacket subAckPacket; if (cancellationToken.CanBeCanceled) { - subAckPacket = await SendAndReceiveAsync(subscribePacket, cancellationToken).ConfigureAwait(false); + subAckPacket = await Request(subscribePacket, cancellationToken).ConfigureAwait(false); } else { using (var timeout = new CancellationTokenSource(Options.Timeout)) { - subAckPacket = await SendAndReceiveAsync(subscribePacket, timeout.Token).ConfigureAwait(false); + subAckPacket = await Request(subscribePacket, timeout.Token).ConfigureAwait(false); } } @@ -369,13 +369,13 @@ namespace MQTTnet.Client MqttUnsubAckPacket unsubAckPacket; if (cancellationToken.CanBeCanceled) { - unsubAckPacket = await SendAndReceiveAsync(unsubscribePacket, cancellationToken).ConfigureAwait(false); + unsubAckPacket = await Request(unsubscribePacket, cancellationToken).ConfigureAwait(false); } else { using (var timeout = new CancellationTokenSource(Options.Timeout)) { - unsubAckPacket = await SendAndReceiveAsync(unsubscribePacket, timeout.Token).ConfigureAwait(false); + unsubAckPacket = await Request(unsubscribePacket, timeout.Token).ConfigureAwait(false); } } @@ -701,7 +701,7 @@ namespace MQTTnet.Client { publishPacket.PacketIdentifier = _packetIdentifierProvider.GetNextPacketIdentifier(); - var pubAckPacket = await SendAndReceiveAsync(publishPacket, cancellationToken).ConfigureAwait(false); + var pubAckPacket = await Request(publishPacket, cancellationToken).ConfigureAwait(false); return _clientPublishResultFactory.Create(pubAckPacket); } @@ -717,16 +717,16 @@ namespace MQTTnet.Client { publishPacket.PacketIdentifier = _packetIdentifierProvider.GetNextPacketIdentifier(); - var pubRecPacket = await SendAndReceiveAsync(publishPacket, cancellationToken).ConfigureAwait(false); + var pubRecPacket = await Request(publishPacket, cancellationToken).ConfigureAwait(false); var pubRelPacket = MqttPacketFactories.PubRel.Create(pubRecPacket, MqttApplicationMessageReceivedReasonCode.Success); - var pubCompPacket = await SendAndReceiveAsync(pubRelPacket, cancellationToken).ConfigureAwait(false); + var pubCompPacket = await Request(pubRelPacket, cancellationToken).ConfigureAwait(false); return _clientPublishResultFactory.Create(pubRecPacket, pubCompPacket); } - async Task SendAndReceiveAsync(MqttPacket requestPacket, CancellationToken cancellationToken) where TResponsePacket : MqttPacket + async Task Request(MqttPacket requestPacket, CancellationToken cancellationToken) where TResponsePacket : MqttPacket { cancellationToken.ThrowIfCancellationRequested(); diff --git a/Source/MQTTnet/Client/Options/MqttClientOptions.cs b/Source/MQTTnet/Client/Options/MqttClientOptions.cs index f296777f..e7842bbe 100644 --- a/Source/MQTTnet/Client/Options/MqttClientOptions.cs +++ b/Source/MQTTnet/Client/Options/MqttClientOptions.cs @@ -12,6 +12,14 @@ namespace MQTTnet.Client { public sealed class MqttClientOptions { + /// + /// Usually the MQTT packets can be send partially. This is done by using multiple TCP packets + /// or WebSocket frames etc. Unfortunately not all brokers (like Amazon Web Services (AWS)) do support this feature and + /// will close the connection when receiving such packets. If such a service is used this flag must + /// be set to _false_. + /// + public bool AllowPacketFragmentation { get; set; } = true; + /// /// Gets or sets the authentication data. /// MQTT 5.0.0+ feature. @@ -24,14 +32,6 @@ namespace MQTTnet.Client /// public string AuthenticationMethod { get; set; } - /// - /// Usually the MQTT packets can be send partially. This is done by using multiple TCP packets - /// or WebSocket frames etc. Unfortunately not all brokers (like Amazon Web Services (AWS)) do support this feature and - /// will close the connection when receiving such packets. If such a service is used this flag must - /// be set to _false_. - /// - public bool AllowPacketFragmentation { get; set; } = true; - public IMqttClientChannelOptions ChannelOptions { get; set; } /// @@ -100,6 +100,11 @@ namespace MQTTnet.Client /// public uint SessionExpiryInterval { get; set; } + /// + /// Gets or sets whether an exception should be thrown when the server has sent a non success ACK packet. + /// + public bool ThrowOnNonSuccessfulResponseFromServer { get; set; } = true; + /// /// Gets or sets the timeout which will be applied at socket level and internal operations. /// The default value is the same as for sockets in .NET in general. @@ -222,4 +227,4 @@ namespace MQTTnet.Client /// public int WriterBufferSizeMax { get; set; } = 65535; } -} +} \ No newline at end of file