Skip to content

Commit

Permalink
Add some extension methods to IMqttChannelAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Dec 8, 2024
1 parent bffe065 commit ca5d13d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ public static bool CanAllowPacketFragmentation(IMqttChannelAdapter channelAdapte
//}

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

return endpointOptions == null || endpointOptions.AllowPacketFragmentation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using MQTTnet.Adapter;

namespace MQTTnet.AspNetCore
{
interface IAspNetCoreMqttChannelAdapter : IMqttChannelAdapter
{
HttpContext? HttpContext { get; }
IFeatureCollection? Features { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.

using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using MQTTnet.Adapter;
using MQTTnet.Formatter;
using MQTTnet.Packets;
Expand All @@ -15,7 +17,7 @@

namespace MQTTnet.AspNetCore;

sealed class MqttClientChannelAdapter : IMqttChannelAdapter, IAsyncDisposable
sealed class MqttClientChannelAdapter : IAspNetCoreMqttChannelAdapter, IAsyncDisposable
{
private bool _disposed = false;
private ConnectionContext? _connection;
Expand All @@ -25,6 +27,9 @@ sealed class MqttClientChannelAdapter : IMqttChannelAdapter, IAsyncDisposable
private readonly bool _allowPacketFragmentation;
private readonly MqttPacketInspector? _packetInspector;

public HttpContext? HttpContext => null;
public IFeatureCollection? Features => _connection?.Features;

public MqttClientChannelAdapter(
MqttPacketFormatterAdapter packetFormatterAdapter,
IMqttClientChannelOptions channelOptions,
Expand Down
10 changes: 8 additions & 2 deletions Source/MQTTnet.AspnetCore/Internal/MqttServerChannelAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@

using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;
using MQTTnet.Adapter;
using Microsoft.AspNetCore.Http.Features;
using MQTTnet.Formatter;
using System.Threading;
using System.Threading.Tasks;

namespace MQTTnet.AspNetCore;

sealed class MqttServerChannelAdapter : MqttChannel, IMqttChannelAdapter
sealed class MqttServerChannelAdapter : MqttChannel, IAspNetCoreMqttChannelAdapter
{
public HttpContext? HttpContext { get; }
public IFeatureCollection? Features { get; }

public MqttServerChannelAdapter(MqttPacketFormatterAdapter packetFormatterAdapter, ConnectionContext connection, HttpContext? httpContext)
: base(packetFormatterAdapter, connection, httpContext, packetInspector: null)
{
HttpContext = httpContext;
Features = connection.Features;

SetAllowPacketFragmentation(connection, httpContext);
}

Expand Down
48 changes: 48 additions & 0 deletions Source/MQTTnet.AspnetCore/MqttChannelAdapterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.AspNetCore.Http;
using MQTTnet.Adapter;
using System;

namespace MQTTnet.AspNetCore
{
public static class MqttChannelAdapterExtensions
{
public static bool? IsWebSocketConnection(this IMqttChannelAdapter channelAdapter)
{
ArgumentNullException.ThrowIfNull(channelAdapter);
return channelAdapter is IAspNetCoreMqttChannelAdapter adapter
? adapter.Features != null && adapter.Features.Get<WebSocketConnectionFeature>() != null
: null;
}

/// <summary>
/// Retrieves the requested feature from the feature collection of channelAdapter.
/// </summary>
/// <typeparam name="TFeature"></typeparam>
/// <param name="channelAdapter"></param>
/// <returns></returns>
public static TFeature? GetFeature<TFeature>(this IMqttChannelAdapter channelAdapter)
{
ArgumentNullException.ThrowIfNull(channelAdapter);
return channelAdapter is IAspNetCoreMqttChannelAdapter adapter && adapter.Features != null
? adapter.Features.Get<TFeature>()
: default;
}

/// <summary>
/// When the channelAdapter is a WebSocket connection, it can get an associated <see cref="HttpContext"/>.
/// </summary>
/// <param name="channelAdapter"></param>
/// <returns></returns>
public static HttpContext? GetHttpContext(this IMqttChannelAdapter channelAdapter)
{
ArgumentNullException.ThrowIfNull(channelAdapter);
return channelAdapter is IAspNetCoreMqttChannelAdapter adapter
? adapter.HttpContext
: null;
}
}
}

0 comments on commit ca5d13d

Please sign in to comment.