Skip to content

Commit

Permalink
Merge implementation of IsAllowPacketFragmentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Dec 8, 2024
1 parent 9a7a8bd commit e01e5f3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
22 changes: 21 additions & 1 deletion Source/MQTTnet.AspnetCore/Features/PacketFragmentationFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@
// See the LICENSE file in the project root for more information.

using MQTTnet.Adapter;
using MQTTnet.Server;
using System;

namespace MQTTnet.AspNetCore
{
sealed class PacketFragmentationFeature(Func<IMqttChannelAdapter, bool> allowPacketFragmentationSelector)
{
public Func<IMqttChannelAdapter, bool> AllowPacketFragmentationSelector { get; } = allowPacketFragmentationSelector;
public Func<IMqttChannelAdapter, bool> AllowPacketFragmentationSelector { get; } = allowPacketFragmentationSelector;

public static bool IsAllowPacketFragmentation(IMqttChannelAdapter channelAdapter, MqttServerTcpEndpointBaseOptions? endpointOptions)
{
//if (endpointOptions != null && endpointOptions.AllowPacketFragmentationSelector != null)
//{
// return endpointOptions.AllowPacketFragmentationSelector(channelAdapter);
//}

// In the AspNetCore environment, we need to exclude WebSocket before AllowPacketFragmentation.
if (channelAdapter is MqttServerChannelAdapter serverChannelAdapter)
{
if (serverChannelAdapter.IsWebSocketConnection)
{
return false;
}
}

return endpointOptions == null || endpointOptions.AllowPacketFragmentation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ private void SetAllowPacketFragmentation(ConnectionContext connection, HttpConte
// When connection is from MapMqtt(),
// the PacketFragmentationFeature instance is copied from kestrel's ConnectionContext.Features to HttpContext.Features,
// but no longer from HttpContext.Features to connection.Features.
var feature = httpContext == null
var packetFragmentationFeature = httpContext == null
? connection.Features.Get<PacketFragmentationFeature>()
: httpContext.Features.Get<PacketFragmentationFeature>();

if (feature == null)
if (packetFragmentationFeature == null)
{
var value = !IsWebSocketConnection;
var value = PacketFragmentationFeature.IsAllowPacketFragmentation(this, null);
SetAllowPacketFragmentation(value);
}
else
{
var value = feature.AllowPacketFragmentationSelector(this);
var value = packetFragmentationFeature.AllowPacketFragmentationSelector(this);
SetAllowPacketFragmentation(value);
}
}
Expand Down
15 changes: 1 addition & 14 deletions Source/MQTTnet.AspnetCore/KestrelServerOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.Extensions.DependencyInjection;
using MQTTnet.Adapter;
using MQTTnet.Exceptions;
using MQTTnet.Server;
using System;
Expand Down Expand Up @@ -91,19 +90,7 @@ void UseMiddleware(ListenOptions listenOptions)
tlsConfigure?.Invoke(httpsOptions);
});
}
listenOptions.UseMqtt(protocols, AllowPacketFragmentationSelector);
}

bool AllowPacketFragmentationSelector(IMqttChannelAdapter channelAdapter)
{
if (channelAdapter is MqttServerChannelAdapter serverChannelAdapter)
{
if (serverChannelAdapter.IsWebSocketConnection)
{
return false;
}
}
return endpoint.AllowPacketFragmentation;
listenOptions.UseMqtt(protocols, channelAdapter => PacketFragmentationFeature.IsAllowPacketFragmentation(channelAdapter, endpoint));
}
}
}
Expand Down

0 comments on commit e01e5f3

Please sign in to comment.