|
@@ -1,4 +1,6 @@ |
|
|
using System; |
|
|
using System; |
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
using System.IO; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using System.Text; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
@@ -16,79 +18,95 @@ namespace MQTTnet.Core.Serializer |
|
|
|
|
|
|
|
|
public MqttProtocolVersion ProtocolVersion { get; set; } = MqttProtocolVersion.V311; |
|
|
public MqttProtocolVersion ProtocolVersion { get; set; } = MqttProtocolVersion.V311; |
|
|
|
|
|
|
|
|
public Task SerializeAsync(MqttBasePacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
public async Task SerializeAsync(MqttBasePacket packet, IMqttCommunicationChannel destination) |
|
|
{ |
|
|
{ |
|
|
if (packet == null) throw new ArgumentNullException(nameof(packet)); |
|
|
if (packet == null) throw new ArgumentNullException(nameof(packet)); |
|
|
if (destination == null) throw new ArgumentNullException(nameof(destination)); |
|
|
if (destination == null) throw new ArgumentNullException(nameof(destination)); |
|
|
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream()) |
|
|
|
|
|
using (var writer = new MqttPacketWriter(stream)) |
|
|
|
|
|
{ |
|
|
|
|
|
var header = new List<byte>(); |
|
|
|
|
|
header.Add(SerializePacket(packet, writer)); |
|
|
|
|
|
|
|
|
|
|
|
var body = stream.ToArray(); |
|
|
|
|
|
MqttPacketWriter.BuildLengthHeader(body.Length, header); |
|
|
|
|
|
|
|
|
|
|
|
await destination.WriteAsync(header.ToArray()).ConfigureAwait(false); |
|
|
|
|
|
await destination.WriteAsync(body).ConfigureAwait(false); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private byte SerializePacket(MqttBasePacket packet, MqttPacketWriter writer) |
|
|
|
|
|
{ |
|
|
if (packet is MqttConnectPacket connectPacket) |
|
|
if (packet is MqttConnectPacket connectPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(connectPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(connectPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttConnAckPacket connAckPacket) |
|
|
if (packet is MqttConnAckPacket connAckPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(connAckPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(connAckPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttDisconnectPacket disconnectPacket) |
|
|
if (packet is MqttDisconnectPacket disconnectPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(disconnectPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(disconnectPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttPingReqPacket pingReqPacket) |
|
|
if (packet is MqttPingReqPacket pingReqPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(pingReqPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(pingReqPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttPingRespPacket pingRespPacket) |
|
|
if (packet is MqttPingRespPacket pingRespPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(pingRespPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(pingRespPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttPublishPacket publishPacket) |
|
|
if (packet is MqttPublishPacket publishPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(publishPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(publishPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttPubAckPacket pubAckPacket) |
|
|
if (packet is MqttPubAckPacket pubAckPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(pubAckPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(pubAckPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttPubRecPacket pubRecPacket) |
|
|
if (packet is MqttPubRecPacket pubRecPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(pubRecPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(pubRecPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttPubRelPacket pubRelPacket) |
|
|
if (packet is MqttPubRelPacket pubRelPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(pubRelPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(pubRelPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttPubCompPacket pubCompPacket) |
|
|
if (packet is MqttPubCompPacket pubCompPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(pubCompPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(pubCompPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttSubscribePacket subscribePacket) |
|
|
if (packet is MqttSubscribePacket subscribePacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(subscribePacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(subscribePacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttSubAckPacket subAckPacket) |
|
|
if (packet is MqttSubAckPacket subAckPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(subAckPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(subAckPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttUnsubscribePacket unsubscribePacket) |
|
|
if (packet is MqttUnsubscribePacket unsubscribePacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(unsubscribePacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(unsubscribePacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (packet is MqttUnsubAckPacket unsubAckPacket) |
|
|
if (packet is MqttUnsubAckPacket unsubAckPacket) |
|
|
{ |
|
|
{ |
|
|
return SerializeAsync(unsubAckPacket, destination); |
|
|
|
|
|
|
|
|
return Serialize(unsubAckPacket, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
throw new MqttProtocolViolationException("Packet type invalid."); |
|
|
throw new MqttProtocolViolationException("Packet type invalid."); |
|
@@ -97,12 +115,18 @@ namespace MQTTnet.Core.Serializer |
|
|
public async Task<MqttBasePacket> DeserializeAsync(IMqttCommunicationChannel source) |
|
|
public async Task<MqttBasePacket> DeserializeAsync(IMqttCommunicationChannel source) |
|
|
{ |
|
|
{ |
|
|
if (source == null) throw new ArgumentNullException(nameof(source)); |
|
|
if (source == null) throw new ArgumentNullException(nameof(source)); |
|
|
|
|
|
|
|
|
|
|
|
var header = await MqttPacketReader.ReadHeaderFromSourceAsync(source).ConfigureAwait(false); |
|
|
|
|
|
|
|
|
using (var mqttPacketReader = new MqttPacketReader(source)) |
|
|
|
|
|
|
|
|
var body = new byte[header.BodyLength]; |
|
|
|
|
|
if (header.BodyLength > 0) |
|
|
{ |
|
|
{ |
|
|
await mqttPacketReader.ReadToEndAsync().ConfigureAwait(false); |
|
|
|
|
|
|
|
|
await MqttPacketReader.ReadFromSourceAsync(source, body).ConfigureAwait(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
switch (mqttPacketReader.ControlPacketType) |
|
|
|
|
|
|
|
|
using (var mqttPacketReader = new MqttPacketReader(new MemoryStream(body), header)) |
|
|
|
|
|
{ |
|
|
|
|
|
switch (header.ControlPacketType) |
|
|
{ |
|
|
{ |
|
|
case MqttControlPacketType.Connect: |
|
|
case MqttControlPacketType.Connect: |
|
|
{ |
|
|
{ |
|
@@ -121,14 +145,14 @@ namespace MQTTnet.Core.Serializer |
|
|
|
|
|
|
|
|
case MqttControlPacketType.Publish: |
|
|
case MqttControlPacketType.Publish: |
|
|
{ |
|
|
{ |
|
|
return DeserializePublish(mqttPacketReader); |
|
|
|
|
|
|
|
|
return DeserializePublish(mqttPacketReader, header); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
case MqttControlPacketType.PubAck: |
|
|
case MqttControlPacketType.PubAck: |
|
|
{ |
|
|
{ |
|
|
return new MqttPubAckPacket |
|
|
return new MqttPubAckPacket |
|
|
{ |
|
|
{ |
|
|
PacketIdentifier = mqttPacketReader.ReadRemainingDataUShort() |
|
|
|
|
|
|
|
|
PacketIdentifier = mqttPacketReader.ReadUInt16() |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -136,7 +160,7 @@ namespace MQTTnet.Core.Serializer |
|
|
{ |
|
|
{ |
|
|
return new MqttPubRecPacket |
|
|
return new MqttPubRecPacket |
|
|
{ |
|
|
{ |
|
|
PacketIdentifier = mqttPacketReader.ReadRemainingDataUShort() |
|
|
|
|
|
|
|
|
PacketIdentifier = mqttPacketReader.ReadUInt16() |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -144,7 +168,7 @@ namespace MQTTnet.Core.Serializer |
|
|
{ |
|
|
{ |
|
|
return new MqttPubRelPacket |
|
|
return new MqttPubRelPacket |
|
|
{ |
|
|
{ |
|
|
PacketIdentifier = mqttPacketReader.ReadRemainingDataUShort() |
|
|
|
|
|
|
|
|
PacketIdentifier = mqttPacketReader.ReadUInt16() |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -152,7 +176,7 @@ namespace MQTTnet.Core.Serializer |
|
|
{ |
|
|
{ |
|
|
return new MqttPubCompPacket |
|
|
return new MqttPubCompPacket |
|
|
{ |
|
|
{ |
|
|
PacketIdentifier = mqttPacketReader.ReadRemainingDataUShort() |
|
|
|
|
|
|
|
|
PacketIdentifier = mqttPacketReader.ReadUInt16() |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -185,13 +209,13 @@ namespace MQTTnet.Core.Serializer |
|
|
{ |
|
|
{ |
|
|
return new MqttUnsubAckPacket |
|
|
return new MqttUnsubAckPacket |
|
|
{ |
|
|
{ |
|
|
PacketIdentifier = mqttPacketReader.ReadRemainingDataUShort() |
|
|
|
|
|
|
|
|
PacketIdentifier = mqttPacketReader.ReadUInt16() |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
default: |
|
|
default: |
|
|
{ |
|
|
{ |
|
|
throw new MqttProtocolViolationException($"Packet type ({(int)mqttPacketReader.ControlPacketType}) not supported."); |
|
|
|
|
|
|
|
|
throw new MqttProtocolViolationException($"Packet type ({(int)header.ControlPacketType}) not supported."); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@@ -201,12 +225,12 @@ namespace MQTTnet.Core.Serializer |
|
|
{ |
|
|
{ |
|
|
var packet = new MqttUnsubscribePacket |
|
|
var packet = new MqttUnsubscribePacket |
|
|
{ |
|
|
{ |
|
|
PacketIdentifier = reader.ReadRemainingDataUShort(), |
|
|
|
|
|
|
|
|
PacketIdentifier = reader.ReadUInt16(), |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
while (!reader.EndOfRemainingData) |
|
|
while (!reader.EndOfRemainingData) |
|
|
{ |
|
|
{ |
|
|
packet.TopicFilters.Add(reader.ReadRemainingDataStringWithLengthPrefix()); |
|
|
|
|
|
|
|
|
packet.TopicFilters.Add(reader.ReadStringWithLengthPrefix()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return packet; |
|
|
return packet; |
|
@@ -216,32 +240,32 @@ namespace MQTTnet.Core.Serializer |
|
|
{ |
|
|
{ |
|
|
var packet = new MqttSubscribePacket |
|
|
var packet = new MqttSubscribePacket |
|
|
{ |
|
|
{ |
|
|
PacketIdentifier = reader.ReadRemainingDataUShort() |
|
|
|
|
|
|
|
|
PacketIdentifier = reader.ReadUInt16() |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
while (!reader.EndOfRemainingData) |
|
|
while (!reader.EndOfRemainingData) |
|
|
{ |
|
|
{ |
|
|
packet.TopicFilters.Add(new TopicFilter( |
|
|
packet.TopicFilters.Add(new TopicFilter( |
|
|
reader.ReadRemainingDataStringWithLengthPrefix(), |
|
|
|
|
|
(MqttQualityOfServiceLevel)reader.ReadRemainingDataByte())); |
|
|
|
|
|
|
|
|
reader.ReadStringWithLengthPrefix(), |
|
|
|
|
|
(MqttQualityOfServiceLevel)reader.ReadByte())); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return packet; |
|
|
return packet; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static MqttBasePacket DeserializePublish(MqttPacketReader reader) |
|
|
|
|
|
|
|
|
private static MqttBasePacket DeserializePublish(MqttPacketReader reader, MqttPacketHeader mqttPacketHeader) |
|
|
{ |
|
|
{ |
|
|
var fixedHeader = new ByteReader(reader.FixedHeader); |
|
|
|
|
|
|
|
|
var fixedHeader = new ByteReader(mqttPacketHeader.FixedHeader); |
|
|
var retain = fixedHeader.Read(); |
|
|
var retain = fixedHeader.Read(); |
|
|
var qualityOfServiceLevel = (MqttQualityOfServiceLevel)fixedHeader.Read(2); |
|
|
var qualityOfServiceLevel = (MqttQualityOfServiceLevel)fixedHeader.Read(2); |
|
|
var dup = fixedHeader.Read(); |
|
|
var dup = fixedHeader.Read(); |
|
|
|
|
|
|
|
|
var topic = reader.ReadRemainingDataStringWithLengthPrefix(); |
|
|
|
|
|
|
|
|
var topic = reader.ReadStringWithLengthPrefix(); |
|
|
|
|
|
|
|
|
ushort packetIdentifier = 0; |
|
|
ushort packetIdentifier = 0; |
|
|
if (qualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) |
|
|
if (qualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) |
|
|
{ |
|
|
{ |
|
|
packetIdentifier = reader.ReadRemainingDataUShort(); |
|
|
|
|
|
|
|
|
packetIdentifier = reader.ReadUInt16(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var packet = new MqttPublishPacket |
|
|
var packet = new MqttPublishPacket |
|
@@ -259,13 +283,13 @@ namespace MQTTnet.Core.Serializer |
|
|
|
|
|
|
|
|
private static MqttBasePacket DeserializeConnect(MqttPacketReader reader) |
|
|
private static MqttBasePacket DeserializeConnect(MqttPacketReader reader) |
|
|
{ |
|
|
{ |
|
|
reader.ReadRemainingData(2); // Skip 2 bytes |
|
|
|
|
|
|
|
|
reader.ReadBytes(2); // Skip 2 bytes |
|
|
|
|
|
|
|
|
MqttProtocolVersion protocolVersion; |
|
|
MqttProtocolVersion protocolVersion; |
|
|
var protocolName = reader.ReadRemainingData(4); |
|
|
|
|
|
|
|
|
var protocolName = reader.ReadBytes(4); |
|
|
if (protocolName.SequenceEqual(ProtocolVersionV310Name)) |
|
|
if (protocolName.SequenceEqual(ProtocolVersionV310Name)) |
|
|
{ |
|
|
{ |
|
|
reader.ReadRemainingData(2); |
|
|
|
|
|
|
|
|
reader.ReadBytes(2); |
|
|
protocolVersion = MqttProtocolVersion.V310; |
|
|
protocolVersion = MqttProtocolVersion.V310; |
|
|
} |
|
|
} |
|
|
else if (protocolName.SequenceEqual(ProtocolVersionV311Name)) |
|
|
else if (protocolName.SequenceEqual(ProtocolVersionV311Name)) |
|
@@ -277,8 +301,8 @@ namespace MQTTnet.Core.Serializer |
|
|
throw new MqttProtocolViolationException("Protocol name is not supported."); |
|
|
throw new MqttProtocolViolationException("Protocol name is not supported."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var protocolLevel = reader.ReadRemainingDataByte(); |
|
|
|
|
|
var connectFlags = reader.ReadRemainingDataByte(); |
|
|
|
|
|
|
|
|
var protocolLevel = reader.ReadByte(); |
|
|
|
|
|
var connectFlags = reader.ReadByte(); |
|
|
|
|
|
|
|
|
var connectFlagsReader = new ByteReader(connectFlags); |
|
|
var connectFlagsReader = new ByteReader(connectFlags); |
|
|
connectFlagsReader.Read(); // Reserved. |
|
|
connectFlagsReader.Read(); // Reserved. |
|
@@ -295,26 +319,26 @@ namespace MQTTnet.Core.Serializer |
|
|
var passwordFlag = connectFlagsReader.Read(); |
|
|
var passwordFlag = connectFlagsReader.Read(); |
|
|
var usernameFlag = connectFlagsReader.Read(); |
|
|
var usernameFlag = connectFlagsReader.Read(); |
|
|
|
|
|
|
|
|
packet.KeepAlivePeriod = reader.ReadRemainingDataUShort(); |
|
|
|
|
|
packet.ClientId = reader.ReadRemainingDataStringWithLengthPrefix(); |
|
|
|
|
|
|
|
|
packet.KeepAlivePeriod = reader.ReadUInt16(); |
|
|
|
|
|
packet.ClientId = reader.ReadStringWithLengthPrefix(); |
|
|
|
|
|
|
|
|
if (willFlag) |
|
|
if (willFlag) |
|
|
{ |
|
|
{ |
|
|
packet.WillMessage = new MqttApplicationMessage( |
|
|
packet.WillMessage = new MqttApplicationMessage( |
|
|
reader.ReadRemainingDataStringWithLengthPrefix(), |
|
|
|
|
|
reader.ReadRemainingDataWithLengthPrefix(), |
|
|
|
|
|
|
|
|
reader.ReadStringWithLengthPrefix(), |
|
|
|
|
|
reader.ReadWithLengthPrefix(), |
|
|
(MqttQualityOfServiceLevel)willQoS, |
|
|
(MqttQualityOfServiceLevel)willQoS, |
|
|
willRetain); |
|
|
willRetain); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (usernameFlag) |
|
|
if (usernameFlag) |
|
|
{ |
|
|
{ |
|
|
packet.Username = reader.ReadRemainingDataStringWithLengthPrefix(); |
|
|
|
|
|
|
|
|
packet.Username = reader.ReadStringWithLengthPrefix(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (passwordFlag) |
|
|
if (passwordFlag) |
|
|
{ |
|
|
{ |
|
|
packet.Password = reader.ReadRemainingDataStringWithLengthPrefix(); |
|
|
|
|
|
|
|
|
packet.Password = reader.ReadStringWithLengthPrefix(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ValidateConnectPacket(packet); |
|
|
ValidateConnectPacket(packet); |
|
@@ -325,12 +349,12 @@ namespace MQTTnet.Core.Serializer |
|
|
{ |
|
|
{ |
|
|
var packet = new MqttSubAckPacket |
|
|
var packet = new MqttSubAckPacket |
|
|
{ |
|
|
{ |
|
|
PacketIdentifier = reader.ReadRemainingDataUShort() |
|
|
|
|
|
|
|
|
PacketIdentifier = reader.ReadUInt16() |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
while (!reader.EndOfRemainingData) |
|
|
while (!reader.EndOfRemainingData) |
|
|
{ |
|
|
{ |
|
|
packet.SubscribeReturnCodes.Add((MqttSubscribeReturnCode)reader.ReadRemainingDataByte()); |
|
|
|
|
|
|
|
|
packet.SubscribeReturnCodes.Add((MqttSubscribeReturnCode)reader.ReadByte()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return packet; |
|
|
return packet; |
|
@@ -338,8 +362,8 @@ namespace MQTTnet.Core.Serializer |
|
|
|
|
|
|
|
|
private static MqttBasePacket DeserializeConnAck(MqttPacketReader reader) |
|
|
private static MqttBasePacket DeserializeConnAck(MqttPacketReader reader) |
|
|
{ |
|
|
{ |
|
|
var variableHeader1 = reader.ReadRemainingDataByte(); |
|
|
|
|
|
var variableHeader2 = reader.ReadRemainingDataByte(); |
|
|
|
|
|
|
|
|
var variableHeader1 = reader.ReadByte(); |
|
|
|
|
|
var variableHeader2 = reader.ReadByte(); |
|
|
|
|
|
|
|
|
var packet = new MqttConnAckPacket |
|
|
var packet = new MqttConnAckPacket |
|
|
{ |
|
|
{ |
|
@@ -366,260 +390,212 @@ namespace MQTTnet.Core.Serializer |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private Task SerializeAsync(MqttConnectPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private byte Serialize(MqttConnectPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
ValidateConnectPacket(packet); |
|
|
ValidateConnectPacket(packet); |
|
|
|
|
|
|
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
|
|
|
// Write variable header |
|
|
|
|
|
writer.Write(0x00, 0x04); // 3.1.2.1 Protocol Name |
|
|
|
|
|
if (ProtocolVersion == MqttProtocolVersion.V311) |
|
|
{ |
|
|
{ |
|
|
// Write variable header |
|
|
|
|
|
output.Write(0x00, 0x04); // 3.1.2.1 Protocol Name |
|
|
|
|
|
if (ProtocolVersion == MqttProtocolVersion.V311) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(ProtocolVersionV311Name); |
|
|
|
|
|
output.Write(0x04); // 3.1.2.2 Protocol Level (4) |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(ProtocolVersionV310Name); |
|
|
|
|
|
output.Write(0x64); |
|
|
|
|
|
output.Write(0x70); |
|
|
|
|
|
output.Write(0x03); // Protocol Level (3) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var connectFlags = new ByteWriter(); // 3.1.2.3 Connect Flags |
|
|
|
|
|
connectFlags.Write(false); // Reserved |
|
|
|
|
|
connectFlags.Write(packet.CleanSession); |
|
|
|
|
|
connectFlags.Write(packet.WillMessage != null); |
|
|
|
|
|
|
|
|
writer.Write(ProtocolVersionV311Name); |
|
|
|
|
|
writer.Write(0x04); // 3.1.2.2 Protocol Level (4) |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
writer.Write(ProtocolVersionV310Name); |
|
|
|
|
|
writer.Write(0x64); |
|
|
|
|
|
writer.Write(0x70); |
|
|
|
|
|
writer.Write(0x03); // Protocol Level (3) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (packet.WillMessage != null) |
|
|
|
|
|
{ |
|
|
|
|
|
connectFlags.Write((int)packet.WillMessage.QualityOfServiceLevel, 2); |
|
|
|
|
|
connectFlags.Write(packet.WillMessage.Retain); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
connectFlags.Write(0, 2); |
|
|
|
|
|
connectFlags.Write(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
connectFlags.Write(packet.Password != null); |
|
|
|
|
|
connectFlags.Write(packet.Username != null); |
|
|
|
|
|
|
|
|
var connectFlags = new ByteWriter(); // 3.1.2.3 Connect Flags |
|
|
|
|
|
connectFlags.Write(false); // Reserved |
|
|
|
|
|
connectFlags.Write(packet.CleanSession); |
|
|
|
|
|
connectFlags.Write(packet.WillMessage != null); |
|
|
|
|
|
|
|
|
output.Write(connectFlags); |
|
|
|
|
|
output.Write(packet.KeepAlivePeriod); |
|
|
|
|
|
output.WriteWithLengthPrefix(packet.ClientId); |
|
|
|
|
|
|
|
|
if (packet.WillMessage != null) |
|
|
|
|
|
{ |
|
|
|
|
|
connectFlags.Write((int)packet.WillMessage.QualityOfServiceLevel, 2); |
|
|
|
|
|
connectFlags.Write(packet.WillMessage.Retain); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
connectFlags.Write(0, 2); |
|
|
|
|
|
connectFlags.Write(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (packet.WillMessage != null) |
|
|
|
|
|
{ |
|
|
|
|
|
output.WriteWithLengthPrefix(packet.WillMessage.Topic); |
|
|
|
|
|
output.WriteWithLengthPrefix(packet.WillMessage.Payload); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
connectFlags.Write(packet.Password != null); |
|
|
|
|
|
connectFlags.Write(packet.Username != null); |
|
|
|
|
|
|
|
|
if (packet.Username != null) |
|
|
|
|
|
{ |
|
|
|
|
|
output.WriteWithLengthPrefix(packet.Username); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
writer.Write(connectFlags); |
|
|
|
|
|
writer.Write(packet.KeepAlivePeriod); |
|
|
|
|
|
writer.WriteWithLengthPrefix(packet.ClientId); |
|
|
|
|
|
|
|
|
if (packet.Password != null) |
|
|
|
|
|
{ |
|
|
|
|
|
output.WriteWithLengthPrefix(packet.Password); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (packet.WillMessage != null) |
|
|
|
|
|
{ |
|
|
|
|
|
writer.WriteWithLengthPrefix(packet.WillMessage.Topic); |
|
|
|
|
|
writer.WriteWithLengthPrefix(packet.WillMessage.Payload); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (packet.Username != null) |
|
|
|
|
|
{ |
|
|
|
|
|
writer.WriteWithLengthPrefix(packet.Username); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.Connect); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
|
|
|
if (packet.Password != null) |
|
|
|
|
|
{ |
|
|
|
|
|
writer.WriteWithLengthPrefix(packet.Password); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Connect); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private Task SerializeAsync(MqttConnAckPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private byte Serialize(MqttConnAckPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
|
|
|
var connectAcknowledgeFlags = new ByteWriter(); |
|
|
|
|
|
|
|
|
|
|
|
if (ProtocolVersion == MqttProtocolVersion.V311) |
|
|
{ |
|
|
{ |
|
|
var connectAcknowledgeFlags = new ByteWriter(); |
|
|
|
|
|
|
|
|
connectAcknowledgeFlags.Write(packet.IsSessionPresent); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (ProtocolVersion == MqttProtocolVersion.V311) |
|
|
|
|
|
{ |
|
|
|
|
|
connectAcknowledgeFlags.Write(packet.IsSessionPresent); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
output.Write(connectAcknowledgeFlags); |
|
|
|
|
|
output.Write((byte)packet.ConnectReturnCode); |
|
|
|
|
|
|
|
|
writer.Write(connectAcknowledgeFlags); |
|
|
|
|
|
writer.Write((byte)packet.ConnectReturnCode); |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.ConnAck); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.ConnAck); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static async Task SerializeAsync(MqttPubRelPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttPubRelPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.PubRel, 0x02); |
|
|
|
|
|
await output.WriteToAsync(destination).ConfigureAwait(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRel, 0x02); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttDisconnectPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttDisconnectPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
return SerializeEmptyPacketAsync(MqttControlPacketType.Disconnect, destination); |
|
|
|
|
|
|
|
|
return SerializeEmptyPacketAsync(MqttControlPacketType.Disconnect, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttPingReqPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttPingReqPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
return SerializeEmptyPacketAsync(MqttControlPacketType.PingReq, destination); |
|
|
|
|
|
|
|
|
return SerializeEmptyPacketAsync(MqttControlPacketType.PingReq, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttPingRespPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttPingRespPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
return SerializeEmptyPacketAsync(MqttControlPacketType.PingResp, destination); |
|
|
|
|
|
|
|
|
return SerializeEmptyPacketAsync(MqttControlPacketType.PingResp, writer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttPublishPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttPublishPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
ValidatePublishPacket(packet); |
|
|
ValidatePublishPacket(packet); |
|
|
|
|
|
|
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.WriteWithLengthPrefix(packet.Topic); |
|
|
|
|
|
|
|
|
writer.WriteWithLengthPrefix(packet.Topic); |
|
|
|
|
|
|
|
|
if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
|
|
|
if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) |
|
|
|
|
|
{ |
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
if (packet.PacketIdentifier > 0) |
|
|
{ |
|
|
{ |
|
|
if (packet.PacketIdentifier > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new MqttProtocolViolationException("Packet identifier must be empty if QoS == 0 [MQTT-2.3.1-5]."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
throw new MqttProtocolViolationException("Packet identifier must be empty if QoS == 0 [MQTT-2.3.1-5]."); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (packet.Payload?.Length > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.Payload); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (packet.Payload?.Length > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
writer.Write(packet.Payload); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var fixedHeader = new ByteWriter(); |
|
|
|
|
|
fixedHeader.Write(packet.Retain); |
|
|
|
|
|
fixedHeader.Write((byte)packet.QualityOfServiceLevel, 2); |
|
|
|
|
|
fixedHeader.Write(packet.Dup); |
|
|
|
|
|
|
|
|
var fixedHeader = new ByteWriter(); |
|
|
|
|
|
fixedHeader.Write(packet.Retain); |
|
|
|
|
|
fixedHeader.Write((byte)packet.QualityOfServiceLevel, 2); |
|
|
|
|
|
fixedHeader.Write(packet.Dup); |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.Publish, fixedHeader.Value); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Publish, fixedHeader.Value); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttPubAckPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttPubAckPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.PubAck); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubAck); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttPubRecPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttPubRecPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.PubRec); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRec); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttPubCompPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttPubCompPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.PubComp); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubComp); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttSubscribePacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttSubscribePacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
if (packet.TopicFilters?.Count > 0) |
|
|
|
|
|
|
|
|
if (packet.TopicFilters?.Count > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
foreach (var topicFilter in packet.TopicFilters) |
|
|
{ |
|
|
{ |
|
|
foreach (var topicFilter in packet.TopicFilters) |
|
|
|
|
|
{ |
|
|
|
|
|
output.WriteWithLengthPrefix(topicFilter.Topic); |
|
|
|
|
|
output.Write((byte)topicFilter.QualityOfServiceLevel); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
writer.WriteWithLengthPrefix(topicFilter.Topic); |
|
|
|
|
|
writer.Write((byte)topicFilter.QualityOfServiceLevel); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.Subscribe, 0x02); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Subscribe, 0x02); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttSubAckPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttSubAckPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
if (packet.SubscribeReturnCodes?.Any() == true) |
|
|
|
|
|
|
|
|
if (packet.SubscribeReturnCodes?.Any() == true) |
|
|
|
|
|
{ |
|
|
|
|
|
foreach (var packetSubscribeReturnCode in packet.SubscribeReturnCodes) |
|
|
{ |
|
|
{ |
|
|
foreach (var packetSubscribeReturnCode in packet.SubscribeReturnCodes) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write((byte)packetSubscribeReturnCode); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
writer.Write((byte)packetSubscribeReturnCode); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.SubAck); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.SubAck); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttUnsubscribePacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttUnsubscribePacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
if (packet.TopicFilters?.Any() == true) |
|
|
|
|
|
|
|
|
if (packet.TopicFilters?.Any() == true) |
|
|
|
|
|
{ |
|
|
|
|
|
foreach (var topicFilter in packet.TopicFilters) |
|
|
{ |
|
|
{ |
|
|
foreach (var topicFilter in packet.TopicFilters) |
|
|
|
|
|
{ |
|
|
|
|
|
output.WriteWithLengthPrefix(topicFilter); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
writer.WriteWithLengthPrefix(topicFilter); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.Unsubscibe, 0x02); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Unsubscibe, 0x02); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeAsync(MqttUnsubAckPacket packet, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte Serialize(MqttUnsubAckPacket packet, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
writer.Write(packet.PacketIdentifier); |
|
|
|
|
|
|
|
|
output.InjectFixedHeader(MqttControlPacketType.UnsubAck); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.UnsubAck); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static Task SerializeEmptyPacketAsync(MqttControlPacketType type, IMqttCommunicationChannel destination) |
|
|
|
|
|
|
|
|
private static byte SerializeEmptyPacketAsync(MqttControlPacketType type, MqttPacketWriter writer) |
|
|
{ |
|
|
{ |
|
|
using (var output = new MqttPacketWriter()) |
|
|
|
|
|
{ |
|
|
|
|
|
output.InjectFixedHeader(type); |
|
|
|
|
|
return output.WriteToAsync(destination); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return MqttPacketWriter.BuildFixedHeader(type); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |