mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Add new overloads for packet payload and will payload (#1721)
This commit is contained in:
@@ -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<byte> 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))
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace MQTTnet.Client
|
||||
/// Gets or sets the reason code.
|
||||
/// <remarks>MQTT 5.0.0+ feature.</remarks>
|
||||
/// </summary>
|
||||
public MqttClientPublishReasonCode ReasonCode { get; } = MqttClientPublishReasonCode.Success;
|
||||
public MqttClientPublishReasonCode ReasonCode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the reason string.
|
||||
|
||||
@@ -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<byte> _payloadSegment;
|
||||
|
||||
MqttPayloadFormatIndicator _payloadFormatIndicator;
|
||||
ArraySegment<byte> _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<byte>(payload);
|
||||
_payloadSegment = payload == null || payload.Length == 0 ? EmptyBuffer.ArraySegment : new ArraySegment<byte>(payload);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttApplicationMessageBuilder WithPayloadSegment(ArraySegment<byte> payloadSegment)
|
||||
public MqttApplicationMessageBuilder WithPayload(ArraySegment<byte> 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<byte> payloadSegment)
|
||||
{
|
||||
return MemoryMarshal.TryGetArray(payloadSegment, out var segment)
|
||||
? WithPayloadSegment(segment)
|
||||
: WithPayload(payloadSegment.ToArray());
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Adds the payload format indicator to the message.
|
||||
/// <remarks>MQTT 5.0.0+ feature.</remarks>
|
||||
@@ -182,6 +168,20 @@ namespace MQTTnet
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttApplicationMessageBuilder WithPayloadSegment(ArraySegment<byte> payloadSegment)
|
||||
{
|
||||
_payloadSegment = payloadSegment;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
#if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1
|
||||
public MqttApplicationMessageBuilder WithPayloadSegment(ReadOnlyMemory<byte> payloadSegment)
|
||||
{
|
||||
return MemoryMarshal.TryGetArray(payloadSegment, out var segment) ? WithPayloadSegment(segment) : WithPayload(payloadSegment.ToArray());
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
|
||||
Reference in New Issue
Block a user