Add support for MQTT 3.1.1 Bridge flag.

This commit is contained in:
Christian
2022-06-10 23:07:00 +02:00
parent f68b109471
commit ec2e08668f
6 changed files with 57 additions and 13 deletions
@@ -98,6 +98,16 @@ namespace MQTTnet.Client
/// </summary>
public ushort TopicAliasMaximum { get; set; }
/// <summary>
/// If set to true, the bridge will attempt to indicate to the remote broker that it is a bridge not an ordinary
/// client.
/// If successful, this means that loop detection will be more effective and that retained messages will be propagated
/// correctly.
/// Not all brokers support this feature so it may be necessary to set it to false if your bridge does not connect
/// properly.
/// </summary>
public bool TryPrivate { get; set; } = true;
/// <summary>
/// Gets or sets the user properties.
/// In MQTT 5, user properties are basic UTF-8 string key-value pairs that you can append to almost every type of MQTT
@@ -96,16 +96,6 @@ namespace MQTTnet.Client
return this;
}
/// <summary>
/// Sets the timeout which will be applied at socket level and internal operations.
/// The default value is the same as for sockets in .NET in general.
/// </summary>
public MqttClientOptionsBuilder WithTimeout(TimeSpan value)
{
_options.Timeout = value;
return this;
}
public MqttClientOptionsBuilder WithConnectionUri(Uri uri)
{
if (uri == null)
@@ -289,6 +279,16 @@ namespace MQTTnet.Client
return this;
}
/// <summary>
/// Sets the timeout which will be applied at socket level and internal operations.
/// The default value is the same as for sockets in .NET in general.
/// </summary>
public MqttClientOptionsBuilder WithTimeout(TimeSpan value)
{
_options.Timeout = value;
return this;
}
public MqttClientOptionsBuilder WithTls(MqttClientOptionsBuilderTlsParameters parameters)
{
_tlsParameters = parameters;
@@ -322,6 +322,20 @@ namespace MQTTnet.Client
return this;
}
/// <summary>
/// If set to true, the bridge will attempt to indicate to the remote broker that it is a bridge not an ordinary
/// client.
/// If successful, this means that loop detection will be more effective and that retained messages will be propagated
/// correctly.
/// Not all brokers support this feature so it may be necessary to set it to false if your bridge does not connect
/// properly.
/// </summary>
public MqttClientOptionsBuilder WithTryPrivate(bool tryPrivate = true)
{
_options.TryPrivate = true;
return this;
}
public MqttClientOptionsBuilder WithUserProperty(string name, string value)
{
if (name is null)
@@ -33,7 +33,8 @@ namespace MQTTnet.Formatter
RequestResponseInformation = clientOptions.RequestResponseInformation,
SessionExpiryInterval = clientOptions.SessionExpiryInterval,
TopicAliasMaximum = clientOptions.TopicAliasMaximum,
UserProperties = clientOptions.UserProperties
UserProperties = clientOptions.UserProperties,
TryPrivate = clientOptions.TryPrivate
};
if (!string.IsNullOrEmpty(clientOptions.WillTopic))
@@ -95,6 +95,10 @@ namespace MQTTnet.Formatter
var protocolName = _bufferReader.ReadString();
var protocolLevel = _bufferReader.ReadByte();
// Remove the mosquitto try_private flag (MQTT 3.1.1 Bridge).
// This flag is accepted but not yet used.
protocolLevel &= 0x7F;
if (protocolName == "MQTT")
{
if (protocolLevel == 5)
@@ -175,12 +175,18 @@ namespace MQTTnet.Formatter.V3
throw new MqttProtocolViolationException("MQTT protocol name do not match MQTT v3.");
}
var tryPrivate = (protocolVersion & 0x80) > 0;
protocolVersion &= 0x7F;
if (protocolVersion != 3 && protocolVersion != 4)
{
throw new MqttProtocolViolationException("MQTT protocol version do not match MQTT v3.");
}
var packet = new MqttConnectPacket();
var packet = new MqttConnectPacket
{
TryPrivate = tryPrivate
};
var connectFlags = _bufferReader.ReadByte();
if ((connectFlags & 0x1) > 0)
@@ -409,7 +415,14 @@ namespace MQTTnet.Formatter.V3
ValidateConnectPacket(packet);
bufferWriter.WriteString("MQIsdp");
bufferWriter.WriteByte(3); // Protocol Level 3
var protocolVersion = 3;
if (packet.TryPrivate)
{
protocolVersion |= 0x80;
}
bufferWriter.WriteByte((byte)protocolVersion);
byte connectFlags = 0x0;
if (packet.CleanSession)
@@ -64,6 +64,8 @@ namespace MQTTnet.Packets
public List<MqttUserProperty> WillUserProperties { get; set; }
public bool TryPrivate { get; set; }
public override string ToString()
{
var passwordText = string.Empty;