Skip to content

Commit e01e5f3

Browse files
committed
Merge implementation of IsAllowPacketFragmentation.
1 parent 9a7a8bd commit e01e5f3

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

Source/MQTTnet.AspnetCore/Features/PacketFragmentationFeature.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@
33
// See the LICENSE file in the project root for more information.
44

55
using MQTTnet.Adapter;
6+
using MQTTnet.Server;
67
using System;
78

89
namespace MQTTnet.AspNetCore
910
{
1011
sealed class PacketFragmentationFeature(Func<IMqttChannelAdapter, bool> allowPacketFragmentationSelector)
1112
{
12-
public Func<IMqttChannelAdapter, bool> AllowPacketFragmentationSelector { get; } = allowPacketFragmentationSelector;
13+
public Func<IMqttChannelAdapter, bool> AllowPacketFragmentationSelector { get; } = allowPacketFragmentationSelector;
14+
15+
public static bool IsAllowPacketFragmentation(IMqttChannelAdapter channelAdapter, MqttServerTcpEndpointBaseOptions? endpointOptions)
16+
{
17+
//if (endpointOptions != null && endpointOptions.AllowPacketFragmentationSelector != null)
18+
//{
19+
// return endpointOptions.AllowPacketFragmentationSelector(channelAdapter);
20+
//}
21+
22+
// In the AspNetCore environment, we need to exclude WebSocket before AllowPacketFragmentation.
23+
if (channelAdapter is MqttServerChannelAdapter serverChannelAdapter)
24+
{
25+
if (serverChannelAdapter.IsWebSocketConnection)
26+
{
27+
return false;
28+
}
29+
}
30+
31+
return endpointOptions == null || endpointOptions.AllowPacketFragmentation;
32+
}
1333
}
1434
}

Source/MQTTnet.AspnetCore/Internal/MqttServerChannelAdapter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ private void SetAllowPacketFragmentation(ConnectionContext connection, HttpConte
2424
// When connection is from MapMqtt(),
2525
// the PacketFragmentationFeature instance is copied from kestrel's ConnectionContext.Features to HttpContext.Features,
2626
// but no longer from HttpContext.Features to connection.Features.
27-
var feature = httpContext == null
27+
var packetFragmentationFeature = httpContext == null
2828
? connection.Features.Get<PacketFragmentationFeature>()
2929
: httpContext.Features.Get<PacketFragmentationFeature>();
3030

31-
if (feature == null)
31+
if (packetFragmentationFeature == null)
3232
{
33-
var value = !IsWebSocketConnection;
33+
var value = PacketFragmentationFeature.IsAllowPacketFragmentation(this, null);
3434
SetAllowPacketFragmentation(value);
3535
}
3636
else
3737
{
38-
var value = feature.AllowPacketFragmentationSelector(this);
38+
var value = packetFragmentationFeature.AllowPacketFragmentationSelector(this);
3939
SetAllowPacketFragmentation(value);
4040
}
4141
}

Source/MQTTnet.AspnetCore/KestrelServerOptionsExtensions.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.AspNetCore.Server.Kestrel.Core;
77
using Microsoft.AspNetCore.Server.Kestrel.Https;
88
using Microsoft.Extensions.DependencyInjection;
9-
using MQTTnet.Adapter;
109
using MQTTnet.Exceptions;
1110
using MQTTnet.Server;
1211
using System;
@@ -91,19 +90,7 @@ void UseMiddleware(ListenOptions listenOptions)
9190
tlsConfigure?.Invoke(httpsOptions);
9291
});
9392
}
94-
listenOptions.UseMqtt(protocols, AllowPacketFragmentationSelector);
95-
}
96-
97-
bool AllowPacketFragmentationSelector(IMqttChannelAdapter channelAdapter)
98-
{
99-
if (channelAdapter is MqttServerChannelAdapter serverChannelAdapter)
100-
{
101-
if (serverChannelAdapter.IsWebSocketConnection)
102-
{
103-
return false;
104-
}
105-
}
106-
return endpoint.AllowPacketFragmentation;
93+
listenOptions.UseMqtt(protocols, channelAdapter => PacketFragmentationFeature.IsAllowPacketFragmentation(channelAdapter, endpoint));
10794
}
10895
}
10996
}

0 commit comments

Comments
 (0)