From 18614d4e81b64ca486a5f951a806eea2d495eb21 Mon Sep 17 00:00:00 2001
From: Christian <6939810+chkr1011@users.noreply.github.com>
Date: Thu, 15 Jun 2023 18:51:43 +0200
Subject: [PATCH] Expose configuration of packet fragmentation in server
options. (#1769)
---
.github/workflows/ReleaseNotes.md | 1 +
.../MQTTnet/Implementations/MqttTcpServerListener.cs | 2 ++
.../Server/Options/MqttServerOptionsBuilder.cs | 12 ++++++++++++
.../Options/MqttServerTcpEndpointBaseOptions.cs | 8 ++++++++
4 files changed, 23 insertions(+)
diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md
index 6c670b923..c45abf782 100644
--- a/.github/workflows/ReleaseNotes.md
+++ b/.github/workflows/ReleaseNotes.md
@@ -1,2 +1,3 @@
* [Client] Fixed _PlatformNotSupportedException_ when using Blazor (#1755, thanks to @Nickztar).
* [Server] Fixed _NullReferenceException_ in retained messages management (#1762, thanks to @logicaloud).
+* [Server] Exposed new option which allows disabling packet fragmentation (#1753).
diff --git a/Source/MQTTnet/Implementations/MqttTcpServerListener.cs b/Source/MQTTnet/Implementations/MqttTcpServerListener.cs
index d22f4157d..48cbb25c8 100644
--- a/Source/MQTTnet/Implementations/MqttTcpServerListener.cs
+++ b/Source/MQTTnet/Implementations/MqttTcpServerListener.cs
@@ -231,6 +231,7 @@ await sslStream.AuthenticateAsServerAsync(
using (var clientAdapter = new MqttChannelAdapter(tcpChannel, packetFormatterAdapter, _rootLogger))
{
+ clientAdapter.AllowPacketFragmentation = _options.AllowPacketFragmentation;
await clientHandler(clientAdapter).ConfigureAwait(false);
}
}
@@ -255,6 +256,7 @@ await sslStream.AuthenticateAsServerAsync(
{
try
{
+ // ReSharper disable once MethodHasAsyncOverload
stream?.Dispose();
clientSocket?.Dispose();
}
diff --git a/Source/MQTTnet/Server/Options/MqttServerOptionsBuilder.cs b/Source/MQTTnet/Server/Options/MqttServerOptionsBuilder.cs
index e966ae6c9..a8859a3bf 100644
--- a/Source/MQTTnet/Server/Options/MqttServerOptionsBuilder.cs
+++ b/Source/MQTTnet/Server/Options/MqttServerOptionsBuilder.cs
@@ -131,6 +131,18 @@ public MqttServerOptionsBuilder WithoutEncryptedEndpoint()
return this;
}
+ ///
+ /// Usually the MQTT packets can be send partially to improve memory allocations.
+ /// This is done by using multiple TCP packets or WebSocket frames etc.
+ /// Unfortunately not all clients do support this and will close the connection when receiving partial packets.
+ ///
+ public MqttServerOptionsBuilder WithoutPacketFragmentation()
+ {
+ _options.DefaultEndpointOptions.AllowPacketFragmentation = false;
+ _options.TlsEndpointOptions.AllowPacketFragmentation = false;
+ return this;
+ }
+
public MqttServerOptionsBuilder WithPersistentSessions(bool value = true)
{
_options.EnablePersistentSessions = value;
diff --git a/Source/MQTTnet/Server/Options/MqttServerTcpEndpointBaseOptions.cs b/Source/MQTTnet/Server/Options/MqttServerTcpEndpointBaseOptions.cs
index 1fbdb26b4..91a38c75f 100644
--- a/Source/MQTTnet/Server/Options/MqttServerTcpEndpointBaseOptions.cs
+++ b/Source/MQTTnet/Server/Options/MqttServerTcpEndpointBaseOptions.cs
@@ -23,6 +23,14 @@ public abstract class MqttServerTcpEndpointBaseOptions
///
public bool? KeepAlive { get; set; }
+ ///
+ /// Usually the MQTT packets can be send partially. This is done by using multiple TCP packets
+ /// or WebSocket frames etc. Unfortunately not all clients do support this feature and
+ /// will close the connection when receiving such packets. If such clients are connecting to this
+ /// server the flag must be set to _false_.
+ ///
+ public bool AllowPacketFragmentation { get; set; } = true;
+
///
/// Gets or sets the TCP keep alive interval.
/// The value _null_ indicates that the OS and framework defaults should be used.