Refactor package object model.

This commit is contained in:
Christian Kratky
2020-10-28 23:11:12 +01:00
parent adce5662eb
commit b7fb0c1002
71 changed files with 761 additions and 582 deletions
@@ -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)