@@ -12,6 +12,7 @@ | |||
<description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</description> | |||
<releaseNotes> | |||
* [Core] Fixed issues in MQTTv5 message encoding and decoding. | |||
* [Client] Added support for extended authentication exchange. | |||
</releaseNotes> | |||
<copyright>Copyright Christian Kratky 2016-2019</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</tags> | |||
@@ -0,0 +1,9 @@ | |||
using System.Threading.Tasks; | |||
namespace MQTTnet.Client.ExtendedAuthenticationExchange | |||
{ | |||
public interface IMqttExtendedAuthenticationExchangeHandler | |||
{ | |||
Task HandleRequestAsync(MqttExtendedAuthenticationExchangeContext context); | |||
} | |||
} |
@@ -0,0 +1,35 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using MQTTnet.Packets; | |||
using MQTTnet.Protocol; | |||
namespace MQTTnet.Client.ExtendedAuthenticationExchange | |||
{ | |||
public class MqttExtendedAuthenticationExchangeContext | |||
{ | |||
public MqttExtendedAuthenticationExchangeContext(MqttAuthPacket authPacket, IMqttClient client) | |||
{ | |||
if (authPacket == null) throw new ArgumentNullException(nameof(authPacket)); | |||
ReasonCode = authPacket.ReasonCode; | |||
ReasonString = authPacket.Properties?.ReasonString; | |||
AuthenticationMethod = authPacket.Properties?.AuthenticationMethod; | |||
AuthenticationData = authPacket.Properties?.AuthenticationData; | |||
UserProperties = authPacket.Properties?.UserProperties; | |||
Client = client ?? throw new ArgumentNullException(nameof(client)); | |||
} | |||
public MqttAuthenticateReasonCode ReasonCode { get; } | |||
public string ReasonString { get; } | |||
public string AuthenticationMethod { get; } | |||
public byte[] AuthenticationData { get; } | |||
public List<MqttUserProperty> UserProperties { get; } | |||
public IMqttClient Client { get; } | |||
} | |||
} |
@@ -0,0 +1,17 @@ | |||
using System.Collections.Generic; | |||
using MQTTnet.Packets; | |||
using MQTTnet.Protocol; | |||
namespace MQTTnet.Client.ExtendedAuthenticationExchange | |||
{ | |||
public class MqttExtendedAuthenticationExchangeData | |||
{ | |||
public MqttAuthenticateReasonCode ReasonCode { get; set; } | |||
public string ReasonString { get; set; } | |||
public byte[] AuthenticationData { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } | |||
} | |||
} |
@@ -3,6 +3,7 @@ using System.Threading; | |||
using System.Threading.Tasks; | |||
using MQTTnet.Client.Connecting; | |||
using MQTTnet.Client.Disconnecting; | |||
using MQTTnet.Client.ExtendedAuthenticationExchange; | |||
using MQTTnet.Client.Options; | |||
using MQTTnet.Client.Subscribing; | |||
using MQTTnet.Client.Unsubscribing; | |||
@@ -21,6 +22,7 @@ namespace MQTTnet.Client | |||
Task<MqttClientAuthenticateResult> ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken); | |||
Task DisconnectAsync(MqttClientDisconnectOptions options, CancellationToken cancellationToken); | |||
Task SendExtendedAuthenticationExchangeDataAsync(MqttExtendedAuthenticationExchangeData data, CancellationToken cancellationToken); | |||
Task<MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken); | |||
Task<MqttClientUnsubscribeResult> UnsubscribeAsync(MqttClientUnsubscribeOptions options, CancellationToken cancellationToken); | |||
} |
@@ -5,6 +5,7 @@ using System.Threading.Tasks; | |||
using MQTTnet.Adapter; | |||
using MQTTnet.Client.Connecting; | |||
using MQTTnet.Client.Disconnecting; | |||
using MQTTnet.Client.ExtendedAuthenticationExchange; | |||
using MQTTnet.Client.Options; | |||
using MQTTnet.Client.Publishing; | |||
using MQTTnet.Client.Receiving; | |||
@@ -138,6 +139,23 @@ namespace MQTTnet.Client | |||
} | |||
} | |||
public Task SendExtendedAuthenticationExchangeDataAsync(MqttExtendedAuthenticationExchangeData data, CancellationToken cancellationToken) | |||
{ | |||
if (data == null) throw new ArgumentNullException(nameof(data)); | |||
return SendAsync(new MqttAuthPacket | |||
{ | |||
Properties = new MqttAuthPacketProperties | |||
{ | |||
// This must always be equal to the value from the CONNECT packet. So we use it here to ensure that. | |||
AuthenticationMethod = Options.AuthenticationMethod, | |||
AuthenticationData = data.AuthenticationData, | |||
ReasonString = data.ReasonString, | |||
UserProperties = data.UserProperties | |||
} | |||
}, cancellationToken); | |||
} | |||
public async Task<MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken) | |||
{ | |||
if (options == null) throw new ArgumentNullException(nameof(options)); | |||
@@ -474,6 +492,14 @@ namespace MQTTnet.Client | |||
await DisconnectAsync(null, cancellationToken).ConfigureAwait(false); | |||
} | |||
else if (packet is MqttAuthPacket authPacket) | |||
{ | |||
var extendedAuthenticationExchangeHandler = Options.ExtendedAuthenticationExchangeHandler; | |||
if (extendedAuthenticationExchangeHandler != null) | |||
{ | |||
await extendedAuthenticationExchangeHandler.HandleRequestAsync(new MqttExtendedAuthenticationExchangeContext(authPacket, this)).ConfigureAwait(false); | |||
} | |||
} | |||
else | |||
{ | |||
_packetDispatcher.Dispatch(packet); | |||
@@ -4,6 +4,7 @@ using System.Threading; | |||
using System.Threading.Tasks; | |||
using MQTTnet.Client.Connecting; | |||
using MQTTnet.Client.Disconnecting; | |||
using MQTTnet.Client.ExtendedAuthenticationExchange; | |||
using MQTTnet.Client.Options; | |||
using MQTTnet.Client.Publishing; | |||
using MQTTnet.Client.Receiving; | |||
@@ -170,21 +171,36 @@ namespace MQTTnet.Client | |||
public static Task<MqttClientAuthenticateResult> ConnectAsync(this IMqttClient client, IMqttClientOptions options) | |||
{ | |||
if (client == null) throw new ArgumentNullException(nameof(client)); | |||
return client.ConnectAsync(options, CancellationToken.None); | |||
} | |||
public static Task DisconnectAsync(this IMqttClient client, MqttClientDisconnectOptions options) | |||
{ | |||
if (client == null) throw new ArgumentNullException(nameof(client)); | |||
return client.DisconnectAsync(options, CancellationToken.None); | |||
} | |||
public static Task SendExtendedAuthenticationExchangeDataAsync(this IMqttClient client, MqttExtendedAuthenticationExchangeData data) | |||
{ | |||
if (client == null) throw new ArgumentNullException(nameof(client)); | |||
return client.SendExtendedAuthenticationExchangeDataAsync(data, CancellationToken.None); | |||
} | |||
public static Task<MqttClientSubscribeResult> SubscribeAsync(this IMqttClient client, MqttClientSubscribeOptions options) | |||
{ | |||
if (client == null) throw new ArgumentNullException(nameof(client)); | |||
return client.SubscribeAsync(options, CancellationToken.None); | |||
} | |||
public static Task<MqttClientUnsubscribeResult> UnsubscribeAsync(this IMqttClient client, MqttClientUnsubscribeOptions options) | |||
{ | |||
if (client == null) throw new ArgumentNullException(nameof(client)); | |||
return client.UnsubscribeAsync(options, CancellationToken.None); | |||
} | |||
@@ -1,4 +1,5 @@ | |||
using System; | |||
using MQTTnet.Client.ExtendedAuthenticationExchange; | |||
using MQTTnet.Formatter; | |||
namespace MQTTnet.Client.Options | |||
@@ -8,6 +9,7 @@ namespace MQTTnet.Client.Options | |||
string ClientId { get; } | |||
bool CleanSession { get; } | |||
IMqttClientCredentials Credentials { get; } | |||
IMqttExtendedAuthenticationExchangeHandler ExtendedAuthenticationExchangeHandler { get; } | |||
MqttProtocolVersion ProtocolVersion { get; } | |||
IMqttClientChannelOptions ChannelOptions { get; } | |||
@@ -1,4 +1,5 @@ | |||
using System; | |||
using MQTTnet.Client.ExtendedAuthenticationExchange; | |||
using MQTTnet.Formatter; | |||
namespace MQTTnet.Client.Options | |||
@@ -8,6 +9,7 @@ namespace MQTTnet.Client.Options | |||
public string ClientId { get; set; } = Guid.NewGuid().ToString("N"); | |||
public bool CleanSession { get; set; } = true; | |||
public IMqttClientCredentials Credentials { get; set; } | |||
public IMqttExtendedAuthenticationExchangeHandler ExtendedAuthenticationExchangeHandler { get; set; } | |||
public MqttProtocolVersion ProtocolVersion { get; set; } = MqttProtocolVersion.V311; | |||
public IMqttClientChannelOptions ChannelOptions { get; set; } | |||
@@ -1,5 +1,6 @@ | |||
using System; | |||
using System.Linq; | |||
using MQTTnet.Client.ExtendedAuthenticationExchange; | |||
using MQTTnet.Formatter; | |||
namespace MQTTnet.Client.Options | |||
@@ -125,6 +126,12 @@ namespace MQTTnet.Client.Options | |||
return this; | |||
} | |||
public MqttClientOptionsBuilder WithExtendedAuthenticationExchangeHandler(IMqttExtendedAuthenticationExchangeHandler handler) | |||
{ | |||
_options.ExtendedAuthenticationExchangeHandler = handler; | |||
return this; | |||
} | |||
public MqttClientOptionsBuilder WithTcpServer(string server, int? port = null) | |||
{ | |||
_tcpOptions = new MqttClientTcpOptions | |||
@@ -10,7 +10,11 @@ namespace MQTTnet.Formatter.V3 | |||
public class MqttV310PacketFormatter : IMqttPacketFormatter | |||
{ | |||
private const int FixedHeaderSize = 1; | |||
private static readonly MqttPingReqPacket PingReqPacket = new MqttPingReqPacket(); | |||
private static readonly MqttPingRespPacket PingRespPacket = new MqttPingRespPacket(); | |||
private static readonly MqttDisconnectPacket DisconnectPacket = new MqttDisconnectPacket(); | |||
private readonly IMqttPacketWriter _packetWriter; | |||
public MqttV310PacketFormatter() | |||
@@ -67,14 +71,14 @@ namespace MQTTnet.Formatter.V3 | |||
{ | |||
case MqttControlPacketType.Connect: return DecodeConnectPacket(receivedMqttPacket.Body); | |||
case MqttControlPacketType.ConnAck: return DecodeConnAckPacket(receivedMqttPacket.Body); | |||
case MqttControlPacketType.Disconnect: return new MqttDisconnectPacket(); | |||
case MqttControlPacketType.Disconnect: return DisconnectPacket; | |||
case MqttControlPacketType.Publish: return DecodePublishPacket(receivedMqttPacket); | |||
case MqttControlPacketType.PubAck: return DecodePubAckPacket(receivedMqttPacket.Body); | |||
case MqttControlPacketType.PubRec: return DecodePubRecPacket(receivedMqttPacket.Body); | |||
case MqttControlPacketType.PubRel: return DecodePubRelPacket(receivedMqttPacket.Body); | |||
case MqttControlPacketType.PubComp: return DecodePubCompPacket(receivedMqttPacket.Body); | |||
case MqttControlPacketType.PingReq: return new MqttPingReqPacket(); | |||
case MqttControlPacketType.PingResp: return new MqttPingRespPacket(); | |||
case MqttControlPacketType.PingReq: return PingReqPacket; | |||
case MqttControlPacketType.PingResp: return PingRespPacket; | |||
case MqttControlPacketType.Subscribe: return DecodeSubscribePacket(receivedMqttPacket.Body); | |||
case MqttControlPacketType.SubAck: return DecodeSubAckPacket(receivedMqttPacket.Body); | |||
case MqttControlPacketType.Unsubscibe: return DecodeUnsubscribePacket(receivedMqttPacket.Body); | |||
@@ -229,7 +233,7 @@ namespace MQTTnet.Formatter.V3 | |||
if (!receivedMqttPacket.Body.EndOfStream) | |||
{ | |||
packet.Payload = receivedMqttPacket.Body.ReadRemainingData().ToArray(); | |||
packet.Payload = receivedMqttPacket.Body.ReadRemainingData(); | |||
} | |||
return packet; | |||
@@ -160,7 +160,7 @@ namespace MQTTnet.Formatter.V5 | |||
packet.TopicFilters.AddRange(options.TopicFilters); | |||
packet.Properties.SubscriptionIdentifier = options.SubscriptionIdentifier; | |||
packet.Properties.UserProperties.AddRange(options.UserProperties); | |||
packet.Properties.UserProperties = options.UserProperties; | |||
return packet; | |||
} | |||
@@ -175,7 +175,7 @@ namespace MQTTnet.Formatter.V5 | |||
}; | |||
packet.TopicFilters.AddRange(options.TopicFilters); | |||
packet.Properties.UserProperties.AddRange(options.UserProperties); | |||
packet.Properties.UserProperties = options.UserProperties; | |||
return packet; | |||
} | |||
@@ -1,6 +1,5 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using MQTTnet.Adapter; | |||
using MQTTnet.Exceptions; | |||
using MQTTnet.Packets; | |||
@@ -10,6 +9,10 @@ namespace MQTTnet.Formatter.V5 | |||
{ | |||
public class MqttV500PacketDecoder | |||
{ | |||
private static readonly MqttPingReqPacket PingReqPacket = new MqttPingReqPacket(); | |||
private static readonly MqttPingRespPacket PingRespPacket = new MqttPingRespPacket(); | |||
public MqttBasePacket Decode(ReceivedMqttPacket receivedMqttPacket) | |||
{ | |||
if (receivedMqttPacket == null) throw new ArgumentNullException(nameof(receivedMqttPacket)); | |||
@@ -46,10 +49,7 @@ namespace MQTTnet.Formatter.V5 | |||
{ | |||
ThrowIfBodyIsEmpty(body); | |||
var packet = new MqttConnectPacket | |||
{ | |||
Properties = new MqttConnectPacketProperties() | |||
}; | |||
var packet = new MqttConnectPacket(); | |||
var protocolName = body.ReadStringWithLengthPrefix(); | |||
var protocolVersion = body.ReadByte(); | |||
@@ -84,6 +84,11 @@ namespace MQTTnet.Formatter.V5 | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttConnectPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.SessionExpiryInterval) | |||
{ | |||
packet.Properties.SessionExpiryInterval = propertiesReader.ReadSessionExpiryInterval(); | |||
@@ -118,6 +123,11 @@ namespace MQTTnet.Formatter.V5 | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -213,13 +223,17 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttConnAckPacket | |||
{ | |||
IsSessionPresent = (acknowledgeFlags & 0x1) > 0, | |||
ReasonCode = (MqttConnectReasonCode)body.ReadByte(), | |||
Properties = new MqttConnAckPacketProperties() | |||
ReasonCode = (MqttConnectReasonCode)body.ReadByte() | |||
}; | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttConnAckPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.SessionExpiryInterval) | |||
{ | |||
packet.Properties.SessionExpiryInterval = propertiesReader.ReadSessionExpiryInterval(); | |||
@@ -282,6 +296,11 @@ namespace MQTTnet.Formatter.V5 | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -299,13 +318,17 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttDisconnectPacket | |||
{ | |||
ReasonCode = (MqttDisconnectReasonCode)body.ReadByte(), | |||
Properties = new MqttDisconnectPacketProperties() | |||
ReasonCode = (MqttDisconnectReasonCode)body.ReadByte() | |||
}; | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttDisconnectPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.SessionExpiryInterval) | |||
{ | |||
packet.Properties.SessionExpiryInterval = propertiesReader.ReadSessionExpiryInterval(); | |||
@@ -320,6 +343,11 @@ namespace MQTTnet.Formatter.V5 | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -337,19 +365,28 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttSubscribePacket | |||
{ | |||
PacketIdentifier = body.ReadTwoByteInteger(), | |||
Properties = new MqttSubscribePacketProperties() | |||
PacketIdentifier = body.ReadTwoByteInteger() | |||
}; | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttSubscribePacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.SubscriptionIdentifier) | |||
{ | |||
packet.Properties.SubscriptionIdentifier = propertiesReader.ReadSubscriptionIdentifier(); | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -387,19 +424,28 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttSubAckPacket | |||
{ | |||
PacketIdentifier = body.ReadTwoByteInteger(), | |||
Properties = new MqttSubAckPacketProperties() | |||
PacketIdentifier = body.ReadTwoByteInteger() | |||
}; | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttSubAckPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString) | |||
{ | |||
packet.Properties.ReasonString = propertiesReader.ReadReasonString(); | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -423,15 +469,24 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttUnsubscribePacket | |||
{ | |||
PacketIdentifier = body.ReadTwoByteInteger(), | |||
Properties = new MqttUnsubscribePacketProperties() | |||
PacketIdentifier = body.ReadTwoByteInteger() | |||
}; | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttUnsubscribePacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -454,19 +509,28 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttUnsubAckPacket | |||
{ | |||
PacketIdentifier = body.ReadTwoByteInteger(), | |||
Properties = new MqttUnsubAckPacketProperties() | |||
PacketIdentifier = body.ReadTwoByteInteger() | |||
}; | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttUnsubAckPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString) | |||
{ | |||
packet.Properties.ReasonString = propertiesReader.ReadReasonString(); | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -486,12 +550,12 @@ namespace MQTTnet.Formatter.V5 | |||
private static MqttBasePacket DecodePingReqPacket() | |||
{ | |||
return new MqttPingReqPacket(); | |||
return PingReqPacket; | |||
} | |||
private static MqttBasePacket DecodePingRespPacket() | |||
{ | |||
return new MqttPingRespPacket(); | |||
return PingRespPacket; | |||
} | |||
private static MqttBasePacket DecodePublishPacket(byte header, IMqttPacketBodyReader body) | |||
@@ -507,8 +571,7 @@ namespace MQTTnet.Formatter.V5 | |||
Topic = body.ReadStringWithLengthPrefix(), | |||
Retain = retain, | |||
QualityOfServiceLevel = qos, | |||
Dup = dup, | |||
Properties = new MqttPublishPacketProperties() | |||
Dup = dup | |||
}; | |||
if (qos > 0) | |||
@@ -519,6 +582,11 @@ namespace MQTTnet.Formatter.V5 | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttPublishPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.PayloadFormatIndicator) | |||
{ | |||
packet.Properties.PayloadFormatIndicator = propertiesReader.ReadPayloadFormatIndicator(); | |||
@@ -569,7 +637,7 @@ namespace MQTTnet.Formatter.V5 | |||
if (!body.EndOfStream) | |||
{ | |||
packet.Payload = body.ReadRemainingData().ToArray(); | |||
packet.Payload = body.ReadRemainingData(); | |||
} | |||
return packet; | |||
@@ -581,8 +649,7 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttPubAckPacket | |||
{ | |||
PacketIdentifier = body.ReadTwoByteInteger(), | |||
Properties = new MqttPubAckPacketProperties() | |||
PacketIdentifier = body.ReadTwoByteInteger() | |||
}; | |||
if (body.EndOfStream) | |||
@@ -596,12 +663,22 @@ namespace MQTTnet.Formatter.V5 | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttPubAckPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString) | |||
{ | |||
packet.Properties.ReasonString = propertiesReader.ReadReasonString(); | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -619,8 +696,7 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttPubRecPacket | |||
{ | |||
PacketIdentifier = body.ReadTwoByteInteger(), | |||
Properties = new MqttPubRecPacketProperties() | |||
PacketIdentifier = body.ReadTwoByteInteger() | |||
}; | |||
if (body.EndOfStream) | |||
@@ -634,12 +710,22 @@ namespace MQTTnet.Formatter.V5 | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttPubRecPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString) | |||
{ | |||
packet.Properties.ReasonString = propertiesReader.ReadReasonString(); | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -657,8 +743,7 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttPubRelPacket | |||
{ | |||
PacketIdentifier = body.ReadTwoByteInteger(), | |||
Properties = new MqttPubRelPacketProperties() | |||
PacketIdentifier = body.ReadTwoByteInteger() | |||
}; | |||
if (body.EndOfStream) | |||
@@ -672,12 +757,22 @@ namespace MQTTnet.Formatter.V5 | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttPubRelPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString) | |||
{ | |||
packet.Properties.ReasonString = propertiesReader.ReadReasonString(); | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -695,8 +790,7 @@ namespace MQTTnet.Formatter.V5 | |||
var packet = new MqttPubCompPacket | |||
{ | |||
PacketIdentifier = body.ReadTwoByteInteger(), | |||
Properties = new MqttPubCompPacketProperties() | |||
PacketIdentifier = body.ReadTwoByteInteger() | |||
}; | |||
if (body.EndOfStream) | |||
@@ -710,12 +804,22 @@ namespace MQTTnet.Formatter.V5 | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttPubCompPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString) | |||
{ | |||
packet.Properties.ReasonString = propertiesReader.ReadReasonString(); | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -731,10 +835,7 @@ namespace MQTTnet.Formatter.V5 | |||
{ | |||
ThrowIfBodyIsEmpty(body); | |||
var packet = new MqttAuthPacket | |||
{ | |||
Properties = new MqttAuthPacketProperties() | |||
}; | |||
var packet = new MqttAuthPacket(); | |||
if (body.EndOfStream) | |||
{ | |||
@@ -747,6 +848,11 @@ namespace MQTTnet.Formatter.V5 | |||
var propertiesReader = new MqttV500PropertiesReader(body); | |||
while (propertiesReader.MoveNext()) | |||
{ | |||
if (packet.Properties == null) | |||
{ | |||
packet.Properties = new MqttAuthPacketProperties(); | |||
} | |||
if (propertiesReader.CurrentPropertyId == MqttPropertyId.AuthenticationMethod) | |||
{ | |||
packet.Properties.AuthenticationMethod = propertiesReader.ReadAuthenticationMethod(); | |||
@@ -761,6 +867,11 @@ namespace MQTTnet.Formatter.V5 | |||
} | |||
else if (propertiesReader.CurrentPropertyId == MqttPropertyId.UserProperty) | |||
{ | |||
if (packet.Properties.UserProperties == null) | |||
{ | |||
packet.Properties.UserProperties = new List<MqttUserProperty>(); | |||
} | |||
propertiesReader.AddUserPropertyTo(packet.Properties.UserProperties); | |||
} | |||
else | |||
@@ -10,6 +10,6 @@ namespace MQTTnet.Packets | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -18,7 +18,7 @@ namespace MQTTnet.Packets | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
public bool? WildcardSubscriptionAvailable { get; set; } | |||
@@ -22,6 +22,6 @@ namespace MQTTnet.Packets | |||
public uint? MaximumPacketSize { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -8,7 +8,7 @@ namespace MQTTnet.Packets | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
public string ServerReference { get; set; } | |||
} |
@@ -6,6 +6,6 @@ namespace MQTTnet.Packets | |||
{ | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -6,6 +6,6 @@ namespace MQTTnet.Packets | |||
{ | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -6,6 +6,6 @@ namespace MQTTnet.Packets | |||
{ | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -6,6 +6,6 @@ namespace MQTTnet.Packets | |||
{ | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -8,7 +8,7 @@ namespace MQTTnet.Packets | |||
{ | |||
public ushort? PacketIdentifier { get; set; } | |||
public List<MqttSubscribeReturnCode> ReturnCodes { get; } = new List<MqttSubscribeReturnCode>(); | |||
public List<MqttSubscribeReturnCode> ReturnCodes { get; set; } = new List<MqttSubscribeReturnCode>(); | |||
#region Added in MQTTv5.0.0 | |||
@@ -6,6 +6,6 @@ namespace MQTTnet.Packets | |||
{ | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -7,7 +7,7 @@ namespace MQTTnet.Packets | |||
{ | |||
public ushort? PacketIdentifier { get; set; } | |||
public List<TopicFilter> TopicFilters { get; } = new List<TopicFilter>(); | |||
public List<TopicFilter> TopicFilters { get; set; } = new List<TopicFilter>(); | |||
#region Added in MQTTv5 | |||
@@ -6,6 +6,6 @@ namespace MQTTnet.Packets | |||
{ | |||
public uint? SubscriptionIdentifier { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -12,7 +12,7 @@ namespace MQTTnet.Packets | |||
public MqttUnsubAckPacketProperties Properties { get; set; } | |||
public List<MqttUnsubscribeReasonCode> ReasonCodes { get; } = new List<MqttUnsubscribeReasonCode>(); | |||
public List<MqttUnsubscribeReasonCode> ReasonCodes { get; set; } = new List<MqttUnsubscribeReasonCode>(); | |||
#endregion | |||
@@ -6,6 +6,6 @@ namespace MQTTnet.Packets | |||
{ | |||
public string ReasonString { get; set; } | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |
@@ -6,7 +6,7 @@ namespace MQTTnet.Packets | |||
{ | |||
public ushort? PacketIdentifier { get; set; } | |||
public List<string> TopicFilters { get; } = new List<string>(); | |||
public List<string> TopicFilters { get; set; } = new List<string>(); | |||
#region Added in MQTTv5 | |||
@@ -4,6 +4,6 @@ namespace MQTTnet.Packets | |||
{ | |||
public class MqttUnsubscribePacketProperties | |||
{ | |||
public List<MqttUserProperty> UserProperties { get; } = new List<MqttUserProperty>(); | |||
public List<MqttUserProperty> UserProperties { get; set; } | |||
} | |||
} |