diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index 5c093744..b8635cbe 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1,2 +1,3 @@ * [Core] Add validation of maximum string lengths (#1718). +* [Client] Added overloads for setting packet payload and will payload (#1720). * [Server] Improved performance by changing internal locking strategy for subscriptions (#1716, thanks to @zeheng). diff --git a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs index a7a0099f..3f9f3adb 100644 --- a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -421,6 +421,18 @@ namespace MQTTnet.Client return this; } + public MqttClientOptionsBuilder WithWillPayload(ArraySegment willPayload) + { + if (willPayload.Count == 0) + { + _options.WillPayload = null; + return this; + } + + _options.WillPayload = willPayload.ToArray(); + return this; + } + public MqttClientOptionsBuilder WithWillPayload(string willPayload) { if (string.IsNullOrEmpty(willPayload)) diff --git a/Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs b/Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs index c9859d1b..e36ce75e 100644 --- a/Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs +++ b/Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs @@ -34,7 +34,7 @@ namespace MQTTnet.Client /// Gets or sets the reason code. /// MQTT 5.0.0+ feature. /// - public MqttClientPublishReasonCode ReasonCode { get; } = MqttClientPublishReasonCode.Success; + public MqttClientPublishReasonCode ReasonCode { get; } /// /// Gets or sets the reason string. diff --git a/Source/MQTTnet/MqttApplicationMessageBuilder.cs b/Source/MQTTnet/MqttApplicationMessageBuilder.cs index a8a740a0..8d65bd31 100644 --- a/Source/MQTTnet/MqttApplicationMessageBuilder.cs +++ b/Source/MQTTnet/MqttApplicationMessageBuilder.cs @@ -2,16 +2,16 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using MQTTnet.Exceptions; -using MQTTnet.Internal; -using MQTTnet.Packets; -using MQTTnet.Protocol; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; +using MQTTnet.Exceptions; +using MQTTnet.Internal; +using MQTTnet.Packets; +using MQTTnet.Protocol; namespace MQTTnet { @@ -20,9 +20,9 @@ namespace MQTTnet string _contentType; byte[] _correlationData; uint _messageExpiryInterval; - ArraySegment _payloadSegment; MqttPayloadFormatIndicator _payloadFormatIndicator; + ArraySegment _payloadSegment; MqttQualityOfServiceLevel _qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce; string _responseTopic; bool _retain; @@ -89,13 +89,11 @@ namespace MQTTnet public MqttApplicationMessageBuilder WithPayload(byte[] payload) { - _payloadSegment = payload == null || payload.Length == 0 - ? EmptyBuffer.ArraySegment - : new ArraySegment(payload); + _payloadSegment = payload == null || payload.Length == 0 ? EmptyBuffer.ArraySegment : new ArraySegment(payload); return this; } - public MqttApplicationMessageBuilder WithPayloadSegment(ArraySegment payloadSegment) + public MqttApplicationMessageBuilder WithPayload(ArraySegment payloadSegment) { _payloadSegment = payloadSegment; return this; @@ -123,9 +121,7 @@ namespace MQTTnet public MqttApplicationMessageBuilder WithPayload(Stream payload) { - return payload == null - ? WithPayload(default(byte[])) - : WithPayload(payload, payload.Length - payload.Position); + return payload == null ? WithPayload(default(byte[])) : WithPayload(payload, payload.Length - payload.Position); } public MqttApplicationMessageBuilder WithPayload(Stream payload, long length) @@ -162,16 +158,6 @@ namespace MQTTnet return WithPayload(payloadBuffer); } - -#if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1 - public MqttApplicationMessageBuilder WithPayloadSegment(ReadOnlyMemory payloadSegment) - { - return MemoryMarshal.TryGetArray(payloadSegment, out var segment) - ? WithPayloadSegment(segment) - : WithPayload(payloadSegment.ToArray()); - } -#endif - /// /// Adds the payload format indicator to the message. /// MQTT 5.0.0+ feature. @@ -182,6 +168,20 @@ namespace MQTTnet return this; } + public MqttApplicationMessageBuilder WithPayloadSegment(ArraySegment payloadSegment) + { + _payloadSegment = payloadSegment; + return this; + } + + +#if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1 + public MqttApplicationMessageBuilder WithPayloadSegment(ReadOnlyMemory payloadSegment) + { + return MemoryMarshal.TryGetArray(payloadSegment, out var segment) ? WithPayloadSegment(segment) : WithPayload(payloadSegment.ToArray()); + } +#endif + /// /// The quality of service level. /// The Quality of Service (QoS) level is an agreement between the sender of a message and the receiver of a message