Skip to content

Commit

Permalink
fixed the ASCII indicator for MQTT 3.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Apr 13, 2024
1 parent 7c13b57 commit f4e8c02
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
13 changes: 7 additions & 6 deletions src/TurboMqtt.Core/Protocol/MqttEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/// <summary>
/// Estimates the size of the packet WITHOUT the length header.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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);
}
}
}

0 comments on commit f4e8c02

Please sign in to comment.