From f4e8c022d1f81e7f2f462e4a1d9d2801f7f17fcd Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Sat, 13 Apr 2024 12:59:39 -0500 Subject: [PATCH] fixed the ASCII indicator for MQTT 3.1.1 --- src/TurboMqtt.Core/Protocol/MqttEncoder.cs | 13 +++++++------ .../Packets/Connect/ConnectPacketMqtt311Specs.cs | 4 ++-- .../Packets/Connect/ConnectPacketMqtt5Specs.cs | 6 +++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/TurboMqtt.Core/Protocol/MqttEncoder.cs b/src/TurboMqtt.Core/Protocol/MqttEncoder.cs index a562b9bb..2bdee046 100644 --- a/src/TurboMqtt.Core/Protocol/MqttEncoder.cs +++ b/src/TurboMqtt.Core/Protocol/MqttEncoder.cs @@ -9,12 +9,13 @@ namespace TurboMqtt.Core.Protocol; -public static class MqttEncoder +internal static class MqttPacketSizeEstimator { - private const int PacketIdLength = 2; - private const int StringSizeLength = 2; - private const int MaxVariableLength = 4; - private const string Mqtt5ProtocolName = "MQTT"; + internal const int PacketIdLength = 2; + internal const int StringSizeLength = 2; + internal const int MaxVariableLength = 4; + internal const string Mqtt5ProtocolName = "MQTT"; + internal const string Mqtt311ProtocolName = "MQIsdp"; /// /// Estimates the size of the packet WITHOUT the length header. @@ -212,7 +213,7 @@ private static int EstimateConnectPacketSizeMqtt311(ConnectPacket packet) // Protocol Name (2 bytes length + actual length of string) - size += 2 + Encoding.ASCII.GetByteCount(Mqtt5ProtocolName); // MQTT uses ASCII, not UTF8, for the protocol name: https://www.emqx.com/en/blog/mqtt-5-0-control-packets-01-connect-connack#connect-packet-structure + size += 2 + Encoding.ASCII.GetByteCount(Mqtt311ProtocolName); // MQTT uses ASCII, not UTF8, for the protocol name: https://www.emqx.com/en/blog/mqtt-5-0-control-packets-01-connect-connack#connect-packet-structure // Protocol Version (1 byte) size += 1; diff --git a/tests/TurboMqtt.Core.Tests/Packets/Connect/ConnectPacketMqtt311Specs.cs b/tests/TurboMqtt.Core.Tests/Packets/Connect/ConnectPacketMqtt311Specs.cs index b43ffe10..847a749a 100644 --- a/tests/TurboMqtt.Core.Tests/Packets/Connect/ConnectPacketMqtt311Specs.cs +++ b/tests/TurboMqtt.Core.Tests/Packets/Connect/ConnectPacketMqtt311Specs.cs @@ -120,7 +120,7 @@ [Fact] public void should_estimate_correct_size() TopicAliasMaximum = 5, // should be ignored - only supported in MQTT 5.0 }; - MqttEncoder.EstimatePacketSize(connectPacket, MqttProtocolVersion.V3_1_1).Should().Be(49); + MqttPacketSizeEstimator.EstimatePacketSize(connectPacket, MqttProtocolVersion.V3_1_1).Should().Be(51); } // estimate the packet size without username and password @@ -134,7 +134,7 @@ [Fact] public void should_estimate_correct_size_without_username_password() TopicAliasMaximum = 5, // should be ignored - only supported in MQTT 5.0 }; - MqttEncoder.EstimatePacketSize(connectPacket, MqttProtocolVersion.V3_1_1).Should().Be(34); + MqttPacketSizeEstimator.EstimatePacketSize(connectPacket, MqttProtocolVersion.V3_1_1).Should().Be(36); } } } \ No newline at end of file diff --git a/tests/TurboMqtt.Core.Tests/Packets/Connect/ConnectPacketMqtt5Specs.cs b/tests/TurboMqtt.Core.Tests/Packets/Connect/ConnectPacketMqtt5Specs.cs index 404271b1..7a17e52a 100644 --- a/tests/TurboMqtt.Core.Tests/Packets/Connect/ConnectPacketMqtt5Specs.cs +++ b/tests/TurboMqtt.Core.Tests/Packets/Connect/ConnectPacketMqtt5Specs.cs @@ -85,7 +85,7 @@ public void should_estimate_correct_packet_size() { var packet = new ConnectPacket("clientId", MqttProtocolVersion.V5_0); - MqttEncoder.EstimatePacketSize(packet, MqttProtocolVersion.V5_0).Should().Be(42); + MqttPacketSizeEstimator.EstimatePacketSize(packet, MqttProtocolVersion.V5_0).Should().Be(42); } [Fact] @@ -100,7 +100,7 @@ public void should_estimate_correct_packet_size_with_properties() } }; - MqttEncoder.EstimatePacketSize(packet, MqttProtocolVersion.V5_0).Should().Be(52); + MqttPacketSizeEstimator.EstimatePacketSize(packet, MqttProtocolVersion.V5_0).Should().Be(52); } [Fact] @@ -127,7 +127,7 @@ public void should_estimate_correct_packet_size_with_will() }; - MqttEncoder.EstimatePacketSize(packet, MqttProtocolVersion.V5_0).Should().Be(77); + MqttPacketSizeEstimator.EstimatePacketSize(packet, MqttProtocolVersion.V5_0).Should().Be(77); } } } \ No newline at end of file