mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-17 17:53:03 +08:00
Refactor namings of serializers etc.
This commit is contained in:
@@ -4,7 +4,7 @@ using MQTTnet.Adapter;
|
||||
using MQTTnet.AspNetCore.Client.Tcp;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.AspNetCore.Client
|
||||
{
|
||||
|
||||
@@ -3,11 +3,11 @@ using MQTTnet.Adapter;
|
||||
using MQTTnet.AspNetCore.Client.Tcp;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Serializer;
|
||||
using System;
|
||||
using System.IO.Pipelines;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.AspNetCore
|
||||
{
|
||||
@@ -72,7 +72,7 @@ namespace MQTTnet.AspNetCore
|
||||
{
|
||||
if (!buffer.IsEmpty)
|
||||
{
|
||||
if (PacketSerializerAdapter.Serializer.TryDeserialize(buffer, out var packet, out consumed, out observed))
|
||||
if (PacketSerializerAdapter.Formatter.TryDeserialize(buffer, out var packet, out consumed, out observed))
|
||||
{
|
||||
return packet;
|
||||
}
|
||||
@@ -107,7 +107,7 @@ namespace MQTTnet.AspNetCore
|
||||
|
||||
public async Task SendPacketAsync(MqttBasePacket packet, CancellationToken cancellationToken)
|
||||
{
|
||||
var buffer = PacketSerializerAdapter.Serialize(packet).AsMemory();
|
||||
var buffer = PacketSerializerAdapter.Encode(packet).AsMemory();
|
||||
var output = Connection.Transport.Output;
|
||||
|
||||
await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
using Microsoft.AspNetCore.Connections;
|
||||
using Microsoft.AspNetCore.Connections.Features;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Server;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.AspNetCore
|
||||
{
|
||||
|
||||
@@ -3,8 +3,8 @@ using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Implementations;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet.AspNetCore
|
||||
|
||||
@@ -2,15 +2,17 @@
|
||||
using System.Buffers;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Serializer;
|
||||
|
||||
namespace MQTTnet.AspNetCore
|
||||
{
|
||||
public static class ReaderExtensions
|
||||
{
|
||||
public static bool TryDeserialize(this IMqttPacketSerializer serializer, in ReadOnlySequence<byte> input, out MqttBasePacket packet, out SequencePosition consumed, out SequencePosition observed)
|
||||
public static bool TryDeserialize(this IMqttPacketFormatter formatter, in ReadOnlySequence<byte> input, out MqttBasePacket packet, out SequencePosition consumed, out SequencePosition observed)
|
||||
{
|
||||
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
|
||||
|
||||
packet = null;
|
||||
consumed = input.Start;
|
||||
observed = input.End;
|
||||
@@ -33,7 +35,7 @@ namespace MQTTnet.AspNetCore
|
||||
|
||||
var bodySlice = copy.Slice(0, bodyLength);
|
||||
var buffer = bodySlice.GetArray();
|
||||
packet = serializer.Deserialize(new ReceivedMqttPacket(fixedheader, new MqttPacketBodyReader(buffer, 0, buffer.Length)));
|
||||
packet = formatter.Decode(new ReceivedMqttPacket(fixedheader, new MqttPacketBodyReader(buffer, 0, buffer.Length)));
|
||||
consumed = bodySlice.End;
|
||||
observed = bodySlice.End;
|
||||
return true;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Serializer;
|
||||
|
||||
namespace MQTTnet.Adapter
|
||||
{
|
||||
|
||||
@@ -7,9 +7,9 @@ using System.Threading.Tasks;
|
||||
using MQTTnet.Channel;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Internal;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Serializer;
|
||||
|
||||
namespace MQTTnet.Adapter
|
||||
{
|
||||
@@ -96,7 +96,7 @@ namespace MQTTnet.Adapter
|
||||
await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var packetData = PacketSerializerAdapter.Serialize(packet);
|
||||
var packetData = PacketSerializerAdapter.Encode(packet);
|
||||
await _channel.WriteAsync(packetData.Array, packetData.Offset, packetData.Count, cancellationToken).ConfigureAwait(false);
|
||||
PacketSerializerAdapter.FreeBuffer();
|
||||
|
||||
@@ -144,7 +144,7 @@ namespace MQTTnet.Adapter
|
||||
PacketSerializerAdapter.DetectProtocolVersion(receivedMqttPacket);
|
||||
}
|
||||
|
||||
var packet = PacketSerializerAdapter.Deserialize(receivedMqttPacket);
|
||||
var packet = PacketSerializerAdapter.Decode(receivedMqttPacket);
|
||||
if (packet == null)
|
||||
{
|
||||
throw new MqttProtocolViolationException("Received malformed packet.");
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Adapter
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Client
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Client
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Client
|
||||
{
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Packets;
|
||||
|
||||
namespace MQTTnet.Formatter
|
||||
{
|
||||
public interface IMqttPacketFormatter
|
||||
{
|
||||
ArraySegment<byte> Encode(MqttBasePacket mqttPacket);
|
||||
|
||||
MqttBasePacket Decode(ReceivedMqttPacket receivedMqttPacket);
|
||||
|
||||
void FreeBuffer();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Formatter
|
||||
{
|
||||
public struct MqttFixedHeader
|
||||
{
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Formatter
|
||||
{
|
||||
public class MqttPacketBodyReader
|
||||
{
|
||||
+1
-1
@@ -4,7 +4,7 @@ using MQTTnet.Channel;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Internal;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Formatter
|
||||
{
|
||||
public static class MqttPacketReader
|
||||
{
|
||||
+15
-12
@@ -1,9 +1,12 @@
|
||||
using System;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter.V310;
|
||||
using MQTTnet.Formatter.V311;
|
||||
using MQTTnet.Formatter.V500;
|
||||
using MQTTnet.Packets;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Formatter
|
||||
{
|
||||
public class MqttPacketSerializerAdapter
|
||||
{
|
||||
@@ -18,31 +21,31 @@ namespace MQTTnet.Serializer
|
||||
|
||||
public MqttProtocolVersion? ProtocolVersion { get; private set; }
|
||||
|
||||
public IMqttPacketSerializer Serializer { get; private set; }
|
||||
public IMqttPacketFormatter Formatter { get; private set; }
|
||||
|
||||
public ArraySegment<byte> Serialize(MqttBasePacket packet)
|
||||
public ArraySegment<byte> Encode(MqttBasePacket packet)
|
||||
{
|
||||
if (Serializer == null)
|
||||
if (Formatter == null)
|
||||
{
|
||||
throw new InvalidOperationException("Protocol version not set or detected.");
|
||||
}
|
||||
|
||||
return Serializer.Serialize(packet);
|
||||
return Formatter.Encode(packet);
|
||||
}
|
||||
|
||||
public MqttBasePacket Deserialize(ReceivedMqttPacket receivedMqttPacket)
|
||||
public MqttBasePacket Decode(ReceivedMqttPacket receivedMqttPacket)
|
||||
{
|
||||
if (Serializer == null)
|
||||
if (Formatter == null)
|
||||
{
|
||||
throw new InvalidOperationException("Protocol version not set or detected.");
|
||||
}
|
||||
|
||||
return Serializer.Deserialize(receivedMqttPacket);
|
||||
return Formatter.Decode(receivedMqttPacket);
|
||||
}
|
||||
|
||||
public void FreeBuffer()
|
||||
{
|
||||
Serializer?.FreeBuffer();
|
||||
Formatter?.FreeBuffer();
|
||||
}
|
||||
|
||||
public void DetectProtocolVersion(ReceivedMqttPacket receivedMqttPacket)
|
||||
@@ -65,20 +68,20 @@ namespace MQTTnet.Serializer
|
||||
{
|
||||
case MqttProtocolVersion.V500:
|
||||
{
|
||||
Serializer = new MqttV500PacketSerializer();
|
||||
Formatter = new MqttV500PacketFormatter();
|
||||
break;
|
||||
}
|
||||
|
||||
case MqttProtocolVersion.V311:
|
||||
{
|
||||
Serializer = new MqttV311PacketSerializer();
|
||||
Formatter = new MqttV311PacketFormatter();
|
||||
break;
|
||||
}
|
||||
|
||||
case MqttProtocolVersion.V310:
|
||||
{
|
||||
|
||||
Serializer = new MqttV310PacketSerializer();
|
||||
Formatter = new MqttV310PacketFormatter();
|
||||
break;
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Formatter
|
||||
{
|
||||
/// <summary>
|
||||
/// This is a custom implementation of a memory stream which provides only MQTTnet relevant features.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Formatter
|
||||
{
|
||||
public enum MqttProtocolVersion
|
||||
{
|
||||
+9
-13
@@ -1,19 +1,20 @@
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
using System;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Internal;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Formatter.V310
|
||||
{
|
||||
public class MqttV310PacketSerializer : IMqttPacketSerializer
|
||||
public class MqttV310PacketFormatter : IMqttPacketFormatter
|
||||
{
|
||||
private const int FixedHeaderSize = 1;
|
||||
|
||||
private readonly MqttPacketWriter _packetWriter = new MqttPacketWriter();
|
||||
|
||||
public ArraySegment<byte> Serialize(MqttBasePacket packet)
|
||||
public ArraySegment<byte> Encode(MqttBasePacket packet)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
|
||||
@@ -38,7 +39,7 @@ namespace MQTTnet.Serializer
|
||||
return new ArraySegment<byte>(buffer, headerOffset, _packetWriter.Length - headerOffset);
|
||||
}
|
||||
|
||||
public MqttBasePacket Deserialize(ReceivedMqttPacket receivedMqttPacket)
|
||||
public MqttBasePacket Decode(ReceivedMqttPacket receivedMqttPacket)
|
||||
{
|
||||
if (receivedMqttPacket == null) throw new ArgumentNullException(nameof(receivedMqttPacket));
|
||||
|
||||
@@ -300,11 +301,6 @@ namespace MQTTnet.Serializer
|
||||
{
|
||||
throw new MqttProtocolViolationException("CleanSession must be set if ClientId is empty [MQTT-3.1.3-7].");
|
||||
}
|
||||
|
||||
if (packet.Properties != null)
|
||||
{
|
||||
throw new MqttProtocolViolationException("Properties are only supported in MQTT Version 5.");
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
|
||||
+3
-2
@@ -1,10 +1,11 @@
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter.V310;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Formatter.V311
|
||||
{
|
||||
public class MqttV311PacketSerializer : MqttV310PacketSerializer
|
||||
public class MqttV311PacketFormatter : MqttV310PacketFormatter
|
||||
{
|
||||
protected override byte SerializeConnectPacket(MqttConnectPacket packet, MqttPacketWriter packetWriter)
|
||||
{
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MQTTnet.Formatter.V500
|
||||
{
|
||||
public class MqttV500PacketDecoder
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MQTTnet.Formatter.V500
|
||||
{
|
||||
public class MqttV500PacketEncoder
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter.V311;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Packets.Properties;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Formatter.V500
|
||||
{
|
||||
public class MqttV500PacketFormatter : MqttV311PacketFormatter
|
||||
{
|
||||
private readonly MqttV500PacketEncoder _encoder = new MqttV500PacketEncoder();
|
||||
private readonly MqttV500PacketDecoder _decoder = new MqttV500PacketDecoder();
|
||||
|
||||
protected override byte SerializeConnectPacket(MqttConnectPacket packet, MqttPacketWriter packetWriter)
|
||||
{
|
||||
ValidateConnectPacket(packet);
|
||||
|
||||
packetWriter.WriteWithLengthPrefix("MQTT");
|
||||
packetWriter.Write(5); // [3.1.2.2 Protocol Version]
|
||||
|
||||
byte connectFlags = 0x0;
|
||||
if (packet.CleanSession)
|
||||
{
|
||||
connectFlags |= 0x2;
|
||||
}
|
||||
|
||||
if (packet.WillMessage != null)
|
||||
{
|
||||
connectFlags |= 0x4;
|
||||
connectFlags |= (byte)((byte)packet.WillMessage.QualityOfServiceLevel << 3);
|
||||
|
||||
if (packet.WillMessage.Retain)
|
||||
{
|
||||
connectFlags |= 0x20;
|
||||
}
|
||||
}
|
||||
|
||||
if (packet.Password != null && packet.Username == null)
|
||||
{
|
||||
throw new MqttProtocolViolationException("If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22].");
|
||||
}
|
||||
|
||||
if (packet.Password != null)
|
||||
{
|
||||
connectFlags |= 0x40;
|
||||
}
|
||||
|
||||
if (packet.Username != null)
|
||||
{
|
||||
connectFlags |= 0x80;
|
||||
}
|
||||
|
||||
packetWriter.Write(connectFlags);
|
||||
packetWriter.Write(packet.KeepAlivePeriod);
|
||||
packetWriter.WriteWithLengthPrefix(packet.ClientId);
|
||||
|
||||
if (packet.WillMessage != null)
|
||||
{
|
||||
packetWriter.WriteWithLengthPrefix(packet.WillMessage.Topic);
|
||||
packetWriter.WriteWithLengthPrefix(packet.WillMessage.Payload);
|
||||
}
|
||||
|
||||
if (packet.Username != null)
|
||||
{
|
||||
packetWriter.WriteWithLengthPrefix(packet.Username);
|
||||
}
|
||||
|
||||
if (packet.Password != null)
|
||||
{
|
||||
packetWriter.WriteWithLengthPrefix(packet.Password);
|
||||
}
|
||||
|
||||
WriteProperty(MqttMessagePropertyID.WillDelayInterval, packet.WillDelayIntervalProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.SessionExpiryInterval, packet.SessionExpiryIntervalProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.AuthenticationMethod, packet.AuthenticationMethodProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.AuthenticationData, packet.AuthenticationDataProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.RequestProblemInformation, packet.RequestProblemInformationProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.RequestResponseInformation, packet.RequestResponseInformationProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.ReceiveMaximum, packet.ReceiveMaximumProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.TopicAlias, packet.TopicAliasMaximumProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.MaximumPacketSize, packet.MaximumPacketSizeProperty, packetWriter);
|
||||
WriteUserProperties(packet.UserPropertiesProperty, packetWriter);
|
||||
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Connect);
|
||||
}
|
||||
|
||||
protected override byte SerializeConnAckPacket(MqttConnAckPacket packet, MqttPacketWriter packetWriter)
|
||||
{
|
||||
byte connectAcknowledgeFlags = 0x0;
|
||||
if (packet.IsSessionPresent)
|
||||
{
|
||||
connectAcknowledgeFlags |= 0x1;
|
||||
}
|
||||
|
||||
packetWriter.Write(connectAcknowledgeFlags);
|
||||
packetWriter.Write((byte)packet.ConnectReturnCode);
|
||||
|
||||
WriteProperty(MqttMessagePropertyID.SessionExpiryInterval, packet.SessionExpiryIntervalProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.AuthenticationMethod, packet.AuthenticationMethodProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.AuthenticationData, packet.AuthenticationDataProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.ReceiveMaximum, packet.ReceiveMaximumProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.TopicAlias, packet.TopicAliasMaximumProperty, packetWriter);
|
||||
WriteProperty(MqttMessagePropertyID.MaximumPacketSize, packet.MaximumPacketSizeProperty, packetWriter);
|
||||
WriteUserProperties(packet.UserPropertiesProperty, packetWriter);
|
||||
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.ConnAck);
|
||||
}
|
||||
|
||||
private static void WriteProperty(MqttMessagePropertyID id, bool? value, MqttPacketWriter target)
|
||||
{
|
||||
if (!value.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
target.Write((byte)id);
|
||||
target.Write(value.Value ? (byte)0x1 : (byte)0x0);
|
||||
}
|
||||
|
||||
private static void WriteProperty(MqttMessagePropertyID id, ushort? value, MqttPacketWriter target)
|
||||
{
|
||||
if (!value.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
target.Write((byte)id);
|
||||
target.Write(value.Value);
|
||||
}
|
||||
|
||||
private static void WriteProperty(MqttMessagePropertyID id, uint? value, MqttPacketWriter target)
|
||||
{
|
||||
if (!value.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Check if order must be reversed like for ushort.
|
||||
target.Write((byte)id);
|
||||
var bytes = BitConverter.GetBytes(value.Value);
|
||||
target.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
private static void WriteProperty(MqttMessagePropertyID id, string value, MqttPacketWriter target)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
target.Write((byte)id);
|
||||
target.WriteWithLengthPrefix(value);
|
||||
}
|
||||
|
||||
private static void WriteProperty(MqttMessagePropertyID id, byte[] value, MqttPacketWriter target)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
target.Write((byte)id);
|
||||
target.WriteWithLengthPrefix(value);
|
||||
}
|
||||
|
||||
private static void WriteUserProperties(List<MqttUserProperty> userProperties, MqttPacketWriter target)
|
||||
{
|
||||
if (userProperties == null || userProperties.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var propertyWriter = new MqttPacketWriter();
|
||||
foreach (var property in userProperties)
|
||||
{
|
||||
propertyWriter.WriteWithLengthPrefix(property.Name);
|
||||
propertyWriter.WriteWithLengthPrefix(property.Value);
|
||||
}
|
||||
|
||||
target.Write((byte)MqttMessagePropertyID.UserProperty);
|
||||
target.WriteVariableLengthInteger((uint)propertyWriter.Length);
|
||||
target.Write(propertyWriter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Implementations
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Threading.Tasks;
|
||||
using Windows.Networking.Sockets;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet.Implementations
|
||||
|
||||
@@ -9,7 +9,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet.Implementations
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
namespace MQTTnet.Internal
|
||||
{
|
||||
public static class Extensions
|
||||
public static class ArraySegmentExtensions
|
||||
{
|
||||
public static byte[] ToArray(this ArraySegment<byte> source)
|
||||
{
|
||||
@@ -69,4 +69,10 @@
|
||||
<None Remove="NewFolder\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Net.Requests">
|
||||
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.1.0\ref\netcoreapp2.1\System.Net.Requests.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Packets.Properties;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Packets
|
||||
{
|
||||
/// <summary>
|
||||
/// Added in MQTTv5.0.0.
|
||||
/// </summary>
|
||||
public class MqttAuthPacket : MqttBasePacket
|
||||
{
|
||||
public MqttAuthenticateReasonCode ReasonCode { get; set; }
|
||||
|
||||
public List<IProperty> Properties { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,15 @@
|
||||
namespace MQTTnet.Packets
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Packets.Properties;
|
||||
|
||||
namespace MQTTnet.Packets
|
||||
{
|
||||
public class MqttBasePublishPacket : MqttBasePacket, IMqttPacketWithIdentifier
|
||||
{
|
||||
public ushort? PacketIdentifier { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Added in MQTTv5.0.0.
|
||||
/// </summary>
|
||||
public List<IProperty> Properties { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,53 @@
|
||||
using MQTTnet.Protocol;
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Packets
|
||||
{
|
||||
public class MqttConnAckPacket : MqttBasePacket
|
||||
{
|
||||
public MqttConnectReturnCode ConnectReturnCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Added in MQTTv3.1.1.
|
||||
/// </summary>
|
||||
public bool IsSessionPresent { get; set; }
|
||||
|
||||
public MqttConnectReturnCode ConnectReturnCode { get; set; }
|
||||
#region Added in MQTTv5
|
||||
|
||||
public uint? SessionExpiryIntervalProperty { get; set; }
|
||||
|
||||
// TODO: Add enum
|
||||
public byte? ReceiveMaximumProperty { get; set; }
|
||||
|
||||
public bool? RetainAvailableProperty { get; set; }
|
||||
|
||||
public uint? MaximumPacketSizeProperty { get; set; }
|
||||
|
||||
public string AssignedClientIdentifierProperty { get; set; }
|
||||
|
||||
public ushort? TopicAliasMaximumProperty { get; set; }
|
||||
|
||||
public string ReasonStringProperty { get; set; }
|
||||
|
||||
public List<MqttUserProperty> UserPropertiesProperty { get; set; }
|
||||
|
||||
public bool? WildcardSubscriptionAvailableProperty { get; set; }
|
||||
|
||||
public bool? SubscriptionIdentifiersAvailableProperty { get; set; }
|
||||
|
||||
public bool? SharedSubscriptionAvailableProperty { get; set; }
|
||||
|
||||
public ushort? ServerKeepAliveProperty { get; set; }
|
||||
|
||||
public string ResponseInformationProperty { get; set; }
|
||||
|
||||
public string ServerReferenceProperty { get; set; }
|
||||
|
||||
public string AuthenticationMethodProperty { get; set; }
|
||||
|
||||
public byte[] AuthenticationDataProperty { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Packets.Properties;
|
||||
|
||||
namespace MQTTnet.Packets
|
||||
{
|
||||
@@ -24,7 +23,29 @@ namespace MQTTnet.Packets
|
||||
|
||||
public MqttApplicationMessage WillMessage { get; set; }
|
||||
|
||||
public List<IProperty> Properties { get; set; }
|
||||
#region Added in MQTTv5
|
||||
|
||||
public uint? WillDelayIntervalProperty { get; set; }
|
||||
|
||||
public uint? SessionExpiryIntervalProperty { get; set; }
|
||||
|
||||
public string AuthenticationMethodProperty { get; set; }
|
||||
|
||||
public byte[] AuthenticationDataProperty { get; set; }
|
||||
|
||||
public bool? RequestProblemInformationProperty { get; set; }
|
||||
|
||||
public bool? RequestResponseInformationProperty { get; set; }
|
||||
|
||||
public ushort? ReceiveMaximumProperty { get; set; }
|
||||
|
||||
public ushort? TopicAliasMaximumProperty { get; set; }
|
||||
|
||||
public uint? MaximumPacketSizeProperty { get; set; }
|
||||
|
||||
public List<MqttUserProperty> UserPropertiesProperty { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
namespace MQTTnet.Packets
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Packets.Properties;
|
||||
|
||||
namespace MQTTnet.Packets
|
||||
{
|
||||
public class MqttDisconnectPacket : MqttBasePacket
|
||||
{
|
||||
/// <summary>
|
||||
/// Added in MQTTv5.0.0.
|
||||
/// </summary>
|
||||
public List<IProperty> Properties { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "Disconnect";
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using MQTTnet.Protocol;
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Packets.Properties;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Packets
|
||||
{
|
||||
@@ -14,6 +16,11 @@ namespace MQTTnet.Packets
|
||||
|
||||
public byte[] Payload { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Added in MQTTv5.0.0.
|
||||
/// </summary>
|
||||
public List<IProperty> Properties { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "Publish: [Topic=" + Topic + "]" +
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MQTTnet.Packets.Properties;
|
||||
|
||||
namespace MQTTnet.Packets
|
||||
{
|
||||
@@ -9,6 +10,11 @@ namespace MQTTnet.Packets
|
||||
|
||||
public IList<TopicFilter> TopicFilters { get; set; } = new List<TopicFilter>();
|
||||
|
||||
/// <summary>
|
||||
/// Added in MQTTv5.0.0.
|
||||
/// </summary>
|
||||
public List<IProperty> Properties { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var topicFiltersText = string.Join(",", TopicFilters.Select(f => f.Topic + "@" + f.QualityOfServiceLevel));
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Packets
|
||||
{
|
||||
public class MqttUserProperty
|
||||
{
|
||||
public MqttUserProperty(string name, string value)
|
||||
{
|
||||
Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
Value = value ?? throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string Value { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace MQTTnet.Packets.Properties
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class AssignedClientIdentifierProperty : StringProperty
|
||||
{
|
||||
public AssignedClientIdentifierProperty(string value)
|
||||
: base((byte)PropertyID.AssignedClientIdentifer, value)
|
||||
: base((byte)MqttMessagePropertyID.AssignedClientIdentifer, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
using System;
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class AuthenticationDataProperty : BinaryDataProperty
|
||||
{
|
||||
public AuthenticationDataProperty(ArraySegment<byte> data)
|
||||
: base((byte)PropertyID.AuthenticationData, data)
|
||||
: base((byte)MqttMessagePropertyID.AuthenticationData, data)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace MQTTnet.Packets.Properties
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class AuthenticationMethodProperty : StringProperty
|
||||
{
|
||||
public AuthenticationMethodProperty(string value)
|
||||
: base((byte)PropertyID.AuthenticationMethod, value)
|
||||
: base((byte)MqttMessagePropertyID.AuthenticationMethod, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
{
|
||||
public class BinaryDataProperty : IProperty
|
||||
{
|
||||
@@ -24,6 +24,7 @@ namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
if (writer == null) throw new ArgumentNullException(nameof(writer));
|
||||
|
||||
writer.Write(Id);
|
||||
writer.Write((ushort)Data.Count);
|
||||
writer.Write(Data);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
{
|
||||
public class ByteProperty : IProperty
|
||||
{
|
||||
@@ -18,7 +18,8 @@ namespace MQTTnet.Packets.Properties
|
||||
public void WriteTo(MqttPacketWriter writer)
|
||||
{
|
||||
if (writer == null) throw new ArgumentNullException(nameof(writer));
|
||||
|
||||
|
||||
writer.Write(Id);
|
||||
writer.Write(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
{
|
||||
@@ -20,6 +20,7 @@ namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
if (writer == null) throw new ArgumentNullException(nameof(writer));
|
||||
|
||||
// TODO: Check if order must be reversed like for ushort.
|
||||
writer.Write(Id);
|
||||
var bytes = BitConverter.GetBytes(Value);
|
||||
writer.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
{
|
||||
public class StringProperty : IProperty
|
||||
{
|
||||
@@ -19,6 +19,7 @@ namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
if (writer == null) throw new ArgumentNullException(nameof(writer));
|
||||
|
||||
writer.Write(Id);
|
||||
writer.WriteWithLengthPrefix(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
{
|
||||
@@ -19,6 +19,7 @@ namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
{
|
||||
if (writer == null) throw new ArgumentNullException(nameof(writer));
|
||||
|
||||
writer.Write(Id);
|
||||
writer.Write(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
{
|
||||
@@ -20,6 +20,8 @@ namespace MQTTnet.Packets.Properties.BaseTypes
|
||||
if (writer == null) throw new ArgumentNullException(nameof(writer));
|
||||
|
||||
var buffer = MqttPacketWriter.EncodeVariableByteInteger(Value);
|
||||
|
||||
writer.Write(Id);
|
||||
writer.Write(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace MQTTnet.Packets.Properties
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class ContentTypeProperty : StringProperty
|
||||
{
|
||||
public ContentTypeProperty(string value)
|
||||
: base((byte)PropertyID.ContentType, value)
|
||||
: base((byte)MqttMessagePropertyID.ContentType, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
using System;
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class CorrelationDataProperty : BinaryDataProperty
|
||||
{
|
||||
public CorrelationDataProperty(ArraySegment<byte> data)
|
||||
: base((byte)PropertyID.CorrelationData, data)
|
||||
: base((byte)MqttMessagePropertyID.CorrelationData, data)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class MaximumPacketSizeProperty : FourByteIntegerValue
|
||||
{
|
||||
public MaximumPacketSizeProperty(uint value)
|
||||
: base((byte)MqttMessagePropertyID.MaximumPacketSize, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class MaximumQoSProperty : ByteProperty
|
||||
{
|
||||
public MaximumQoSProperty(byte value)
|
||||
: base((byte)MqttMessagePropertyID.MaximumQoS, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace MQTTnet.Packets.Properties
|
||||
public class MessageExpiryIntervalProperty : FourByteIntegerValue
|
||||
{
|
||||
public MessageExpiryIntervalProperty(uint value)
|
||||
: base((byte)PropertyID.MessageExpiryInterval, value)
|
||||
: base((byte)MqttMessagePropertyID.MessageExpiryInterval, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public enum PropertyID
|
||||
public enum MqttMessagePropertyID
|
||||
{
|
||||
PayloadFormatIndicator = 1,
|
||||
MessageExpiryInterval = 2,
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace MQTTnet.Packets.Properties
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class PayloadFormatIndicatorProperty : ByteProperty
|
||||
{
|
||||
public PayloadFormatIndicatorProperty(byte value)
|
||||
: base((byte)PropertyID.PayloadFormatIndicator, value)
|
||||
: base((byte)MqttMessagePropertyID.PayloadFormatIndicator, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class ReasonStringProperty : StringProperty
|
||||
{
|
||||
public ReasonStringProperty(string value)
|
||||
: base((byte)MqttMessagePropertyID.ReasonString, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class ReceiveMaximumProperty : TwoByteIntegerProperty
|
||||
{
|
||||
public ReceiveMaximumProperty(ushort value)
|
||||
: base((byte)MqttMessagePropertyID.ReceiveMaximum, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace MQTTnet.Packets.Properties
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class RequestProblemInformationProperty : ByteProperty
|
||||
{
|
||||
public RequestProblemInformationProperty(byte value)
|
||||
: base((byte)PropertyID.RequestProblemInformation, value)
|
||||
: base((byte)MqttMessagePropertyID.RequestProblemInformation, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace MQTTnet.Packets.Properties
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class RequestResponseInformationProperty : ByteProperty
|
||||
{
|
||||
public RequestResponseInformationProperty(byte value)
|
||||
: base((byte)PropertyID.RequestResponseInformation, value)
|
||||
: base((byte)MqttMessagePropertyID.RequestResponseInformation, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class ResponseInformationProperty : StringProperty
|
||||
{
|
||||
public ResponseInformationProperty(string value)
|
||||
: base((byte)MqttMessagePropertyID.ResponseInformation, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace MQTTnet.Packets.Properties
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class ResponseTopicProperty : StringProperty
|
||||
{
|
||||
public ResponseTopicProperty(string value)
|
||||
: base((byte)PropertyID.ResponseTopic, value)
|
||||
: base((byte)MqttMessagePropertyID.ResponseTopic, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class RetainAvailableProperty : ByteProperty
|
||||
{
|
||||
public RetainAvailableProperty(byte value)
|
||||
: base((byte)MqttMessagePropertyID.RetainAvailable, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace MQTTnet.Packets.Properties
|
||||
public class ServerKeepAliveProperty : TwoByteIntegerProperty
|
||||
{
|
||||
public ServerKeepAliveProperty(ushort value)
|
||||
: base((byte)PropertyID.ServerKeepAlive, value)
|
||||
: base((byte)MqttMessagePropertyID.ServerKeepAlive, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class ServerReferenceProperty : StringProperty
|
||||
{
|
||||
public ServerReferenceProperty(string value)
|
||||
: base((byte)MqttMessagePropertyID.ServerReference, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace MQTTnet.Packets.Properties
|
||||
public class SessionExpiryIntervalProperty : FourByteIntegerValue
|
||||
{
|
||||
public SessionExpiryIntervalProperty(uint value)
|
||||
: base((byte)PropertyID.SessionExpiryInterval, value)
|
||||
: base((byte)MqttMessagePropertyID.SessionExpiryInterval, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class SharedSubscriptionAvailableProperty : ByteProperty
|
||||
{
|
||||
public SharedSubscriptionAvailableProperty(byte value)
|
||||
: base((byte)MqttMessagePropertyID.SharedSubscriptionAvailable, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class SubscriptionIdentifierAvailable : ByteProperty
|
||||
{
|
||||
public SubscriptionIdentifierAvailable(byte value)
|
||||
: base((byte)MqttMessagePropertyID.SubscriptionIdentifierAvailable, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace MQTTnet.Packets.Properties
|
||||
public class SubscriptionIdentifierProperty : VariableByteIntegerProperty
|
||||
{
|
||||
public SubscriptionIdentifierProperty(uint value)
|
||||
: base((byte)PropertyID.SubscriptionIdentifier, value)
|
||||
: base((byte)MqttMessagePropertyID.SubscriptionIdentifier, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class TopicAliasMaximumProperty : TwoByteIntegerProperty
|
||||
{
|
||||
public TopicAliasMaximumProperty(ushort value)
|
||||
: base((byte)MqttMessagePropertyID.TopicAliasMaximum, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class TopicAliasProperty : TwoByteIntegerProperty
|
||||
{
|
||||
public TopicAliasProperty(ushort value)
|
||||
: base((byte)MqttMessagePropertyID.TopicAlias, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class UserPropertyProperty : IProperty
|
||||
{
|
||||
public UserPropertyProperty(string name, string value)
|
||||
{
|
||||
Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
Value = value ?? throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
public byte Id { get; } = (byte)MqttMessagePropertyID.UserProperty;
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string Value { get; }
|
||||
|
||||
public void WriteTo(MqttPacketWriter writer)
|
||||
{
|
||||
if (writer == null) throw new ArgumentNullException(nameof(writer));
|
||||
|
||||
writer.Write(Id);
|
||||
writer.WriteWithLengthPrefix(Name);
|
||||
writer.WriteWithLengthPrefix(Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using MQTTnet.Packets.Properties.BaseTypes;
|
||||
|
||||
namespace MQTTnet.Packets.Properties
|
||||
{
|
||||
public class WildcardSubscriptionAvailableProperty : ByteProperty
|
||||
{
|
||||
public WildcardSubscriptionAvailableProperty(byte value)
|
||||
: base((byte)MqttMessagePropertyID.WildcardSubscriptionAvailable, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace MQTTnet.Packets.Properties
|
||||
public class WillDelayIntervalProperty : FourByteIntegerValue
|
||||
{
|
||||
public WillDelayIntervalProperty(uint value)
|
||||
: base((byte)PropertyID.WillDelayInterval, value)
|
||||
: base((byte)MqttMessagePropertyID.WillDelayInterval, value)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace MQTTnet.Protocol
|
||||
{
|
||||
public enum MqttAuthenticateReasonCode
|
||||
{
|
||||
Success = 0,
|
||||
ContinueAuthentication = 24,
|
||||
ReAuthenticate = 25
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Packets;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
{
|
||||
public interface IMqttPacketSerializer
|
||||
{
|
||||
ArraySegment<byte> Serialize(MqttBasePacket mqttPacket);
|
||||
|
||||
MqttBasePacket Deserialize(ReceivedMqttPacket receivedMqttPacket);
|
||||
|
||||
void FreeBuffer();
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
using System;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Serializer
|
||||
{
|
||||
public class MqttV500PacketSerializer : MqttV311PacketSerializer
|
||||
{
|
||||
//public ArraySegment<byte> Serialize(MqttBasePacket mqttPacket)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//public MqttBasePacket Deserialize(ReceivedMqttPacket receivedMqttPacket)
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
protected override byte SerializeConnectPacket(MqttConnectPacket packet, MqttPacketWriter packetWriter)
|
||||
{
|
||||
ValidateConnectPacket(packet);
|
||||
|
||||
packetWriter.WriteWithLengthPrefix("MQTT");
|
||||
packetWriter.Write(5); // [3.1.2.2 Protocol Version]
|
||||
|
||||
byte connectFlags = 0x0;
|
||||
if (packet.CleanSession)
|
||||
{
|
||||
connectFlags |= 0x2;
|
||||
}
|
||||
|
||||
if (packet.WillMessage != null)
|
||||
{
|
||||
connectFlags |= 0x4;
|
||||
connectFlags |= (byte)((byte)packet.WillMessage.QualityOfServiceLevel << 3);
|
||||
|
||||
if (packet.WillMessage.Retain)
|
||||
{
|
||||
connectFlags |= 0x20;
|
||||
}
|
||||
}
|
||||
|
||||
if (packet.Password != null && packet.Username == null)
|
||||
{
|
||||
throw new MqttProtocolViolationException("If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22].");
|
||||
}
|
||||
|
||||
if (packet.Password != null)
|
||||
{
|
||||
connectFlags |= 0x40;
|
||||
}
|
||||
|
||||
if (packet.Username != null)
|
||||
{
|
||||
connectFlags |= 0x80;
|
||||
}
|
||||
|
||||
packetWriter.Write(connectFlags);
|
||||
packetWriter.Write(packet.KeepAlivePeriod);
|
||||
packetWriter.WriteWithLengthPrefix(packet.ClientId);
|
||||
|
||||
if (packet.WillMessage != null)
|
||||
{
|
||||
packetWriter.WriteWithLengthPrefix(packet.WillMessage.Topic);
|
||||
packetWriter.WriteWithLengthPrefix(packet.WillMessage.Payload);
|
||||
}
|
||||
|
||||
if (packet.Username != null)
|
||||
{
|
||||
packetWriter.WriteWithLengthPrefix(packet.Username);
|
||||
}
|
||||
|
||||
if (packet.Password != null)
|
||||
{
|
||||
packetWriter.WriteWithLengthPrefix(packet.Password);
|
||||
}
|
||||
|
||||
if (packet.Properties != null)
|
||||
{
|
||||
var propertyWriter = new MqttPacketWriter();
|
||||
foreach (var property in packet.Properties)
|
||||
{
|
||||
propertyWriter.Write(property.Id);
|
||||
property.WriteTo(propertyWriter);
|
||||
}
|
||||
|
||||
packetWriter.WriteVariableLengthInteger((uint)propertyWriter.Length);
|
||||
packetWriter.Write(propertyWriter);
|
||||
}
|
||||
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Connect);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Serializer;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user