Skip to content

Commit 18614d4

Browse files
authored
Expose configuration of packet fragmentation in server options. (#1769)
1 parent 560e804 commit 18614d4

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

.github/workflows/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
* [Client] Fixed _PlatformNotSupportedException_ when using Blazor (#1755, thanks to @Nickztar).
22
* [Server] Fixed _NullReferenceException_ in retained messages management (#1762, thanks to @logicaloud).
3+
* [Server] Exposed new option which allows disabling packet fragmentation (#1753).

Source/MQTTnet/Implementations/MqttTcpServerListener.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ await sslStream.AuthenticateAsServerAsync(
231231

232232
using (var clientAdapter = new MqttChannelAdapter(tcpChannel, packetFormatterAdapter, _rootLogger))
233233
{
234+
clientAdapter.AllowPacketFragmentation = _options.AllowPacketFragmentation;
234235
await clientHandler(clientAdapter).ConfigureAwait(false);
235236
}
236237
}
@@ -255,6 +256,7 @@ await sslStream.AuthenticateAsServerAsync(
255256
{
256257
try
257258
{
259+
// ReSharper disable once MethodHasAsyncOverload
258260
stream?.Dispose();
259261
clientSocket?.Dispose();
260262
}

Source/MQTTnet/Server/Options/MqttServerOptionsBuilder.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ public MqttServerOptionsBuilder WithoutEncryptedEndpoint()
131131
return this;
132132
}
133133

134+
/// <summary>
135+
/// Usually the MQTT packets can be send partially to improve memory allocations.
136+
/// This is done by using multiple TCP packets or WebSocket frames etc.
137+
/// Unfortunately not all clients do support this and will close the connection when receiving partial packets.
138+
/// </summary>
139+
public MqttServerOptionsBuilder WithoutPacketFragmentation()
140+
{
141+
_options.DefaultEndpointOptions.AllowPacketFragmentation = false;
142+
_options.TlsEndpointOptions.AllowPacketFragmentation = false;
143+
return this;
144+
}
145+
134146
public MqttServerOptionsBuilder WithPersistentSessions(bool value = true)
135147
{
136148
_options.EnablePersistentSessions = value;

Source/MQTTnet/Server/Options/MqttServerTcpEndpointBaseOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public abstract class MqttServerTcpEndpointBaseOptions
2323
/// </summary>
2424
public bool? KeepAlive { get; set; }
2525

26+
/// <summary>
27+
/// Usually the MQTT packets can be send partially. This is done by using multiple TCP packets
28+
/// or WebSocket frames etc. Unfortunately not all clients do support this feature and
29+
/// will close the connection when receiving such packets. If such clients are connecting to this
30+
/// server the flag must be set to _false_.
31+
/// </summary>
32+
public bool AllowPacketFragmentation { get; set; } = true;
33+
2634
/// <summary>
2735
/// Gets or sets the TCP keep alive interval.
2836
/// The value _null_ indicates that the OS and framework defaults should be used.

0 commit comments

Comments
 (0)