Skip to content

Commit

Permalink
Expose configuration of packet fragmentation in server options. (#1769)
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 authored Jun 15, 2023
1 parent 560e804 commit 18614d4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 2 additions & 0 deletions Source/MQTTnet/Implementations/MqttTcpServerListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ await sslStream.AuthenticateAsServerAsync(

using (var clientAdapter = new MqttChannelAdapter(tcpChannel, packetFormatterAdapter, _rootLogger))
{
clientAdapter.AllowPacketFragmentation = _options.AllowPacketFragmentation;
await clientHandler(clientAdapter).ConfigureAwait(false);
}
}
Expand All @@ -255,6 +256,7 @@ await sslStream.AuthenticateAsServerAsync(
{
try
{
// ReSharper disable once MethodHasAsyncOverload
stream?.Dispose();
clientSocket?.Dispose();
}
Expand Down
12 changes: 12 additions & 0 deletions Source/MQTTnet/Server/Options/MqttServerOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ public MqttServerOptionsBuilder WithoutEncryptedEndpoint()
return this;
}

/// <summary>
/// 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.
/// </summary>
public MqttServerOptionsBuilder WithoutPacketFragmentation()
{
_options.DefaultEndpointOptions.AllowPacketFragmentation = false;
_options.TlsEndpointOptions.AllowPacketFragmentation = false;
return this;
}

public MqttServerOptionsBuilder WithPersistentSessions(bool value = true)
{
_options.EnablePersistentSessions = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public abstract class MqttServerTcpEndpointBaseOptions
/// </summary>
public bool? KeepAlive { get; set; }

/// <summary>
/// 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_.
/// </summary>
public bool AllowPacketFragmentation { get; set; } = true;

/// <summary>
/// Gets or sets the TCP keep alive interval.
/// The value _null_ indicates that the OS and framework defaults should be used.
Expand Down

0 comments on commit 18614d4

Please sign in to comment.