mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-26 16:24:10 +08:00
Refactor package object model.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using MQTTnet.Client.Connecting;
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Client.Connecting;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Publishing;
|
||||
using MQTTnet.Client.Subscribing;
|
||||
using MQTTnet.Client.Unsubscribing;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
using MqttClientSubscribeResult = MQTTnet.Client.Subscribing.MqttClientSubscribeResult;
|
||||
|
||||
@@ -30,8 +32,12 @@ namespace MQTTnet.Formatter
|
||||
|
||||
MqttSubscribePacket CreateSubscribePacket(MqttClientSubscribeOptions options);
|
||||
|
||||
MqttSubAckPacket CreateSubAckPacket(MqttSubscribePacket subscribePacket, Server.MqttClientSubscribeResult subscribeResult);
|
||||
|
||||
MqttUnsubscribePacket CreateUnsubscribePacket(MqttClientUnsubscribeOptions options);
|
||||
|
||||
MqttUnsubAckPacket CreateUnsubAckPacket(MqttUnsubscribePacket unsubscribePacket, List<MqttUnsubscribeReasonCode> reasonCodes);
|
||||
|
||||
MqttDisconnectPacket CreateDisconnectPacket(MqttClientDisconnectOptions options);
|
||||
|
||||
MqttClientPublishResult CreatePublishResult(MqttPubAckPacket pubAckPacket);
|
||||
|
||||
@@ -8,7 +8,7 @@ using MQTTnet.Packets;
|
||||
|
||||
namespace MQTTnet.Formatter
|
||||
{
|
||||
public class MqttPacketFormatterAdapter
|
||||
public sealed class MqttPacketFormatterAdapter
|
||||
{
|
||||
IMqttPacketFormatter _formatter;
|
||||
|
||||
@@ -77,17 +77,6 @@ namespace MQTTnet.Formatter
|
||||
UseProtocolVersion(protocolVersion);
|
||||
}
|
||||
|
||||
private void UseProtocolVersion(MqttProtocolVersion protocolVersion)
|
||||
{
|
||||
if (protocolVersion == MqttProtocolVersion.Unknown)
|
||||
{
|
||||
throw new InvalidOperationException("MQTT protocol version is invalid.");
|
||||
}
|
||||
|
||||
ProtocolVersion = protocolVersion;
|
||||
_formatter = GetMqttPacketFormatter(protocolVersion, Writer);
|
||||
}
|
||||
|
||||
public static IMqttPacketFormatter GetMqttPacketFormatter(MqttProtocolVersion protocolVersion, IMqttPacketWriter writer)
|
||||
{
|
||||
if (protocolVersion == MqttProtocolVersion.Unknown)
|
||||
@@ -116,7 +105,18 @@ namespace MQTTnet.Formatter
|
||||
}
|
||||
}
|
||||
|
||||
MqttProtocolVersion ParseProtocolVersion(ReceivedMqttPacket receivedMqttPacket)
|
||||
void UseProtocolVersion(MqttProtocolVersion protocolVersion)
|
||||
{
|
||||
if (protocolVersion == MqttProtocolVersion.Unknown)
|
||||
{
|
||||
throw new InvalidOperationException("MQTT protocol version is invalid.");
|
||||
}
|
||||
|
||||
ProtocolVersion = protocolVersion;
|
||||
_formatter = GetMqttPacketFormatter(protocolVersion, Writer);
|
||||
}
|
||||
|
||||
static MqttProtocolVersion ParseProtocolVersion(ReceivedMqttPacket receivedMqttPacket)
|
||||
{
|
||||
if (receivedMqttPacket == null) throw new ArgumentNullException(nameof(receivedMqttPacket));
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MqttClientSubscribeResult = MQTTnet.Client.Subscribing.MqttClientSubscribeResult;
|
||||
|
||||
@@ -172,6 +173,18 @@ namespace MQTTnet.Formatter.V3
|
||||
return subscribePacket;
|
||||
}
|
||||
|
||||
public MqttSubAckPacket CreateSubAckPacket(MqttSubscribePacket subscribePacket, Server.MqttClientSubscribeResult subscribeResult)
|
||||
{
|
||||
var subackPacket = new MqttSubAckPacket
|
||||
{
|
||||
PacketIdentifier = subscribePacket.PacketIdentifier
|
||||
};
|
||||
|
||||
subackPacket.ReturnCodes.AddRange(subscribeResult.ReturnCodes);
|
||||
|
||||
return subackPacket;
|
||||
}
|
||||
|
||||
public MqttUnsubscribePacket CreateUnsubscribePacket(MqttClientUnsubscribeOptions options)
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
@@ -182,6 +195,15 @@ namespace MQTTnet.Formatter.V3
|
||||
return unsubscribePacket;
|
||||
}
|
||||
|
||||
public MqttUnsubAckPacket CreateUnsubAckPacket(MqttUnsubscribePacket unsubscribePacket, List<MqttUnsubscribeReasonCode> reasonCodes)
|
||||
{
|
||||
return new MqttUnsubAckPacket
|
||||
{
|
||||
PacketIdentifier = unsubscribePacket.PacketIdentifier,
|
||||
ReasonCodes = reasonCodes
|
||||
};
|
||||
}
|
||||
|
||||
public MqttDisconnectPacket CreateDisconnectPacket(MqttClientDisconnectOptions options)
|
||||
{
|
||||
if (options.ReasonCode != MqttClientDisconnectReason.NormalDisconnection || options.ReasonString != null)
|
||||
|
||||
@@ -210,7 +210,7 @@ namespace MQTTnet.Formatter.V3
|
||||
|
||||
var topic = receivedMqttPacket.Body.ReadStringWithLengthPrefix();
|
||||
|
||||
ushort? packetIdentifier = null;
|
||||
ushort packetIdentifier = 0;
|
||||
if (qualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
|
||||
{
|
||||
packetIdentifier = receivedMqttPacket.Body.ReadTwoByteInteger();
|
||||
@@ -414,12 +414,12 @@ namespace MQTTnet.Formatter.V3
|
||||
|
||||
static byte EncodePubRelPacket(MqttPubRelPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("PubRel packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRel, 0x02);
|
||||
}
|
||||
@@ -432,12 +432,12 @@ namespace MQTTnet.Formatter.V3
|
||||
|
||||
if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("Publish packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -471,36 +471,36 @@ namespace MQTTnet.Formatter.V3
|
||||
|
||||
static byte EncodePubAckPacket(MqttPubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("PubAck packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubAck);
|
||||
}
|
||||
|
||||
static byte EncodePubRecPacket(MqttPubRecPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("PubRec packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRec);
|
||||
}
|
||||
|
||||
static byte EncodePubCompPacket(MqttPubCompPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("PubComp packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubComp);
|
||||
}
|
||||
@@ -509,12 +509,12 @@ namespace MQTTnet.Formatter.V3
|
||||
{
|
||||
if (!packet.TopicFilters.Any()) throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.8.3-3].");
|
||||
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("Subscribe packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
if (packet.TopicFilters?.Count > 0)
|
||||
{
|
||||
@@ -530,12 +530,12 @@ namespace MQTTnet.Formatter.V3
|
||||
|
||||
static byte EncodeSubAckPacket(MqttSubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("SubAck packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
if (packet.ReturnCodes?.Any() == true)
|
||||
{
|
||||
@@ -552,12 +552,12 @@ namespace MQTTnet.Formatter.V3
|
||||
{
|
||||
if (!packet.TopicFilters.Any()) throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.10.3-2].");
|
||||
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("Unsubscribe packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
if (packet.TopicFilters?.Any() == true)
|
||||
{
|
||||
@@ -572,12 +572,12 @@ namespace MQTTnet.Formatter.V3
|
||||
|
||||
static byte EncodeUnsubAckPacket(MqttUnsubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("UnsubAck packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.UnsubAck);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Formatter.V3
|
||||
{
|
||||
public class MqttV311PacketFormatter : MqttV310PacketFormatter
|
||||
public sealed class MqttV311PacketFormatter : MqttV310PacketFormatter
|
||||
{
|
||||
public MqttV311PacketFormatter(IMqttPacketWriter packetWriter)
|
||||
: base(packetWriter)
|
||||
|
||||
@@ -200,6 +200,18 @@ namespace MQTTnet.Formatter.V5
|
||||
return packet;
|
||||
}
|
||||
|
||||
public MqttSubAckPacket CreateSubAckPacket(MqttSubscribePacket subscribePacket, Server.MqttClientSubscribeResult subscribeResult)
|
||||
{
|
||||
var subackPacket = new MqttSubAckPacket
|
||||
{
|
||||
PacketIdentifier = subscribePacket.PacketIdentifier
|
||||
};
|
||||
|
||||
subackPacket.ReasonCodes.AddRange(subscribeResult.ReasonCodes);
|
||||
|
||||
return subackPacket;
|
||||
}
|
||||
|
||||
public MqttUnsubscribePacket CreateUnsubscribePacket(MqttClientUnsubscribeOptions options)
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
@@ -215,6 +227,15 @@ namespace MQTTnet.Formatter.V5
|
||||
return packet;
|
||||
}
|
||||
|
||||
public MqttUnsubAckPacket CreateUnsubAckPacket(MqttUnsubscribePacket unsubscribePacket, List<MqttUnsubscribeReasonCode> reasonCodes)
|
||||
{
|
||||
return new MqttUnsubAckPacket
|
||||
{
|
||||
PacketIdentifier = unsubscribePacket.PacketIdentifier,
|
||||
ReasonCodes = reasonCodes
|
||||
};
|
||||
}
|
||||
|
||||
public MqttDisconnectPacket CreateDisconnectPacket(MqttClientDisconnectOptions options)
|
||||
{
|
||||
var packet = new MqttDisconnectPacket();
|
||||
|
||||
@@ -6,14 +6,13 @@ using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Formatter.V5
|
||||
{
|
||||
public class MqttV500PacketEncoder
|
||||
public sealed class MqttV500PacketEncoder
|
||||
{
|
||||
private readonly IMqttPacketWriter _packetWriter;
|
||||
readonly IMqttPacketWriter _packetWriter;
|
||||
|
||||
public MqttV500PacketEncoder()
|
||||
: this(new MqttPacketWriter())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MqttV500PacketEncoder(IMqttPacketWriter packetWriter)
|
||||
@@ -21,7 +20,6 @@ namespace MQTTnet.Formatter.V5
|
||||
_packetWriter = packetWriter;
|
||||
}
|
||||
|
||||
|
||||
public ArraySegment<byte> Encode(MqttBasePacket packet)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
@@ -52,7 +50,7 @@ namespace MQTTnet.Formatter.V5
|
||||
_packetWriter.FreeBuffer();
|
||||
}
|
||||
|
||||
private static byte EncodePacket(MqttBasePacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodePacket(MqttBasePacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
switch (packet)
|
||||
{
|
||||
@@ -76,7 +74,7 @@ namespace MQTTnet.Formatter.V5
|
||||
}
|
||||
}
|
||||
|
||||
private static byte EncodeConnectPacket(MqttConnectPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodeConnectPacket(MqttConnectPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
if (packetWriter == null) throw new ArgumentNullException(nameof(packetWriter));
|
||||
@@ -176,7 +174,7 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Connect);
|
||||
}
|
||||
|
||||
private static byte EncodeConnAckPacket(MqttConnAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodeConnAckPacket(MqttConnAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
if (packetWriter == null) throw new ArgumentNullException(nameof(packetWriter));
|
||||
@@ -221,7 +219,7 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.ConnAck);
|
||||
}
|
||||
|
||||
private static byte EncodePublishPacket(MqttPublishPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodePublishPacket(MqttPublishPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
if (packetWriter == null) throw new ArgumentNullException(nameof(packetWriter));
|
||||
@@ -235,12 +233,12 @@ namespace MQTTnet.Formatter.V5
|
||||
|
||||
if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("Publish packet has no packet identifier.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -287,12 +285,12 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Publish, fixedHeader);
|
||||
}
|
||||
|
||||
private static byte EncodePubAckPacket(MqttPubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodePubAckPacket(MqttPubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
if (packetWriter == null) throw new ArgumentNullException(nameof(packetWriter));
|
||||
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
if (packet.PacketIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException("PubAck packet has no packet identifier.");
|
||||
}
|
||||
@@ -302,7 +300,7 @@ namespace MQTTnet.Formatter.V5
|
||||
throw new MqttProtocolViolationException("PubAck packet must contain a reason code.");
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
var propertiesWriter = new MqttV500PropertiesWriter();
|
||||
if (packet.Properties != null)
|
||||
@@ -320,9 +318,9 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubAck);
|
||||
}
|
||||
|
||||
private static byte EncodePubRecPacket(MqttPubRecPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodePubRecPacket(MqttPubRecPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
ThrowIfPacketIdentifierIsInvalid(packet);
|
||||
ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet);
|
||||
|
||||
if (!packet.ReasonCode.HasValue)
|
||||
{
|
||||
@@ -336,7 +334,7 @@ namespace MQTTnet.Formatter.V5
|
||||
propertiesWriter.WriteUserProperties(packet.Properties.UserProperties);
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
if (packetWriter.Length > 0 || packet.ReasonCode.Value != MqttPubRecReasonCode.Success)
|
||||
{
|
||||
@@ -347,9 +345,9 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRec);
|
||||
}
|
||||
|
||||
private static byte EncodePubRelPacket(MqttPubRelPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodePubRelPacket(MqttPubRelPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
ThrowIfPacketIdentifierIsInvalid(packet);
|
||||
ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet);
|
||||
|
||||
if (!packet.ReasonCode.HasValue)
|
||||
{
|
||||
@@ -363,7 +361,7 @@ namespace MQTTnet.Formatter.V5
|
||||
propertiesWriter.WriteUserProperties(packet.Properties.UserProperties);
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
if (propertiesWriter.Length > 0 || packet.ReasonCode.Value != MqttPubRelReasonCode.Success)
|
||||
{
|
||||
@@ -374,16 +372,16 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRel, 0x02);
|
||||
}
|
||||
|
||||
private static byte EncodePubCompPacket(MqttPubCompPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodePubCompPacket(MqttPubCompPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
ThrowIfPacketIdentifierIsInvalid(packet);
|
||||
ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet);
|
||||
|
||||
if (!packet.ReasonCode.HasValue)
|
||||
{
|
||||
ThrowReasonCodeNotSetException();
|
||||
}
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
var propertiesWriter = new MqttV500PropertiesWriter();
|
||||
if (packet.Properties != null)
|
||||
@@ -401,13 +399,13 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubComp);
|
||||
}
|
||||
|
||||
private static byte EncodeSubscribePacket(MqttSubscribePacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodeSubscribePacket(MqttSubscribePacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (packet.TopicFilters?.Any() != true) throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.8.3-3].");
|
||||
|
||||
ThrowIfPacketIdentifierIsInvalid(packet);
|
||||
ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet);
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
var propertiesWriter = new MqttV500PropertiesWriter();
|
||||
if (packet.Properties != null)
|
||||
@@ -448,13 +446,13 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Subscribe, 0x02);
|
||||
}
|
||||
|
||||
private static byte EncodeSubAckPacket(MqttSubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodeSubAckPacket(MqttSubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (packet.ReasonCodes?.Any() != true) throw new MqttProtocolViolationException("At least one reason code must be set[MQTT - 3.8.3 - 3].");
|
||||
|
||||
ThrowIfPacketIdentifierIsInvalid(packet);
|
||||
ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet);
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
var propertiesWriter = new MqttV500PropertiesWriter();
|
||||
if (packet.Properties != null)
|
||||
@@ -473,13 +471,13 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.SubAck);
|
||||
}
|
||||
|
||||
private static byte EncodeUnsubscribePacket(MqttUnsubscribePacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodeUnsubscribePacket(MqttUnsubscribePacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (packet.TopicFilters?.Any() != true) throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.10.3-2].");
|
||||
|
||||
ThrowIfPacketIdentifierIsInvalid(packet);
|
||||
ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet);
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
var propertiesWriter = new MqttV500PropertiesWriter();
|
||||
if (packet.Properties != null)
|
||||
@@ -497,13 +495,13 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Unsubscibe, 0x02);
|
||||
}
|
||||
|
||||
private static byte EncodeUnsubAckPacket(MqttUnsubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodeUnsubAckPacket(MqttUnsubAckPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (packet.ReasonCodes?.Any() != true) throw new MqttProtocolViolationException("At least one reason code must be set[MQTT - 3.8.3 - 3].");
|
||||
|
||||
ThrowIfPacketIdentifierIsInvalid(packet);
|
||||
ThrowIfPacketIdentifierIsInvalid(packet.PacketIdentifier, packet);
|
||||
|
||||
packetWriter.Write(packet.PacketIdentifier.Value);
|
||||
packetWriter.Write(packet.PacketIdentifier);
|
||||
|
||||
var propertiesWriter = new MqttV500PropertiesWriter();
|
||||
if (packet.Properties != null)
|
||||
@@ -522,7 +520,7 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.UnsubAck);
|
||||
}
|
||||
|
||||
private static byte EncodeDisconnectPacket(MqttDisconnectPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodeDisconnectPacket(MqttDisconnectPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
if (!packet.ReasonCode.HasValue)
|
||||
{
|
||||
@@ -545,17 +543,17 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Disconnect);
|
||||
}
|
||||
|
||||
private static byte EncodePingReqPacket()
|
||||
static byte EncodePingReqPacket()
|
||||
{
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PingReq);
|
||||
}
|
||||
|
||||
private static byte EncodePingRespPacket()
|
||||
static byte EncodePingRespPacket()
|
||||
{
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PingResp);
|
||||
}
|
||||
|
||||
private static byte EncodeAuthPacket(MqttAuthPacket packet, IMqttPacketWriter packetWriter)
|
||||
static byte EncodeAuthPacket(MqttAuthPacket packet, IMqttPacketWriter packetWriter)
|
||||
{
|
||||
packetWriter.Write((byte)packet.ReasonCode);
|
||||
|
||||
@@ -573,14 +571,16 @@ namespace MQTTnet.Formatter.V5
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Auth);
|
||||
}
|
||||
|
||||
private static void ThrowReasonCodeNotSetException()
|
||||
static void ThrowReasonCodeNotSetException()
|
||||
{
|
||||
throw new MqttProtocolViolationException("The ReasonCode must be set for MQTT version 5.");
|
||||
}
|
||||
|
||||
private static void ThrowIfPacketIdentifierIsInvalid(IMqttPacketWithIdentifier packet)
|
||||
static void ThrowIfPacketIdentifierIsInvalid(ushort packetIdentifier, MqttBasePacket packet)
|
||||
{
|
||||
if (!packet.PacketIdentifier.HasValue)
|
||||
// SUBSCRIBE, UNSUBSCRIBE, and PUBLISH(in cases where QoS > 0) Control Packets MUST contain a non-zero 16 - bit Packet Identifier[MQTT - 2.3.1 - 1].
|
||||
|
||||
if (packetIdentifier == 0)
|
||||
{
|
||||
throw new MqttProtocolViolationException($"Packet identifier is not set for {packet.GetType().Name}.");
|
||||
}
|
||||
|
||||
@@ -5,10 +5,9 @@ using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Formatter.V5
|
||||
{
|
||||
public class MqttV500PropertiesWriter
|
||||
public sealed class MqttV500PropertiesWriter
|
||||
{
|
||||
// TODO: Consider lazy init on first write to avoid useless allocations.
|
||||
private readonly MqttPacketWriter _packetWriter = new MqttPacketWriter();
|
||||
readonly MqttPacketWriter _packetWriter = new MqttPacketWriter();
|
||||
|
||||
public int Length => _packetWriter.Length;
|
||||
|
||||
@@ -178,7 +177,7 @@ namespace MQTTnet.Formatter.V5
|
||||
Write(MqttPropertyId.ResponseInformation, value);
|
||||
}
|
||||
|
||||
private void Write(MqttPropertyId id, bool? value)
|
||||
void Write(MqttPropertyId id, bool? value)
|
||||
{
|
||||
if (!value.HasValue)
|
||||
{
|
||||
@@ -189,7 +188,7 @@ namespace MQTTnet.Formatter.V5
|
||||
_packetWriter.Write(value.Value ? (byte)0x1 : (byte)0x0);
|
||||
}
|
||||
|
||||
private void Write(MqttPropertyId id, byte? value)
|
||||
void Write(MqttPropertyId id, byte? value)
|
||||
{
|
||||
if (!value.HasValue)
|
||||
{
|
||||
@@ -200,7 +199,7 @@ namespace MQTTnet.Formatter.V5
|
||||
_packetWriter.Write(value.Value);
|
||||
}
|
||||
|
||||
private void Write(MqttPropertyId id, ushort? value)
|
||||
void Write(MqttPropertyId id, ushort? value)
|
||||
{
|
||||
if (!value.HasValue)
|
||||
{
|
||||
@@ -211,7 +210,7 @@ namespace MQTTnet.Formatter.V5
|
||||
_packetWriter.Write(value.Value);
|
||||
}
|
||||
|
||||
private void WriteAsVariableLengthInteger(MqttPropertyId id, uint? value)
|
||||
void WriteAsVariableLengthInteger(MqttPropertyId id, uint? value)
|
||||
{
|
||||
if (!value.HasValue)
|
||||
{
|
||||
@@ -222,7 +221,7 @@ namespace MQTTnet.Formatter.V5
|
||||
_packetWriter.WriteVariableLengthInteger(value.Value);
|
||||
}
|
||||
|
||||
private void WriteAsFourByteInteger(MqttPropertyId id, uint? value)
|
||||
void WriteAsFourByteInteger(MqttPropertyId id, uint? value)
|
||||
{
|
||||
if (!value.HasValue)
|
||||
{
|
||||
@@ -236,7 +235,7 @@ namespace MQTTnet.Formatter.V5
|
||||
_packetWriter.Write((byte)value.Value);
|
||||
}
|
||||
|
||||
private void Write(MqttPropertyId id, string value)
|
||||
void Write(MqttPropertyId id, string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
@@ -247,7 +246,7 @@ namespace MQTTnet.Formatter.V5
|
||||
_packetWriter.WriteWithLengthPrefix(value);
|
||||
}
|
||||
|
||||
private void Write(MqttPropertyId id, byte[] value)
|
||||
void Write(MqttPropertyId id, byte[] value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user