Skip to content

Commit

Permalink
Adapt MqttServerTcpEndpointBaseOptions to the Socket accepted by kest…
Browse files Browse the repository at this point in the history
…rel.
  • Loading branch information
xljiulang committed Dec 9, 2024
1 parent b65c176 commit 51fb185
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Source/MQTTnet.AspnetCore/KestrelServerOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Connections.Features;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Https;
Expand All @@ -10,6 +11,7 @@
using MQTTnet.Server;
using System;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;

namespace MQTTnet.AspNetCore
Expand Down Expand Up @@ -82,6 +84,16 @@ void Listen(MqttServerTcpEndpointBaseOptions endpoint)

void UseMiddleware(ListenOptions listenOptions)
{
listenOptions.Use(next => context =>
{
var socketFeature = context.Features.Get<IConnectionSocketFeature>();
if (socketFeature != null)
{
endpoint.AdaptTo(socketFeature.Socket);
}
return next(context);
});

if (endpoint is MqttServerTlsTcpEndpointOptions tlsEndPoint)
{
listenOptions.UseHttps(httpsOptions =>
Expand All @@ -90,11 +102,54 @@ void UseMiddleware(ListenOptions listenOptions)
tlsConfigure?.Invoke(httpsOptions);
});
}

listenOptions.UseMqtt(protocols, channelAdapter => PacketFragmentationFeature.CanAllowPacketFragmentation(channelAdapter, endpoint));
}
}
}

private static void AdaptTo(this MqttServerTcpEndpointBaseOptions endpoint, Socket socket)
{
if (endpoint.NoDelay)
{
socket.NoDelay = true;
}

if (endpoint.LingerState != null)
{
socket.LingerState = endpoint.LingerState;
}

if (endpoint.ReuseAddress)
{
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}

if (endpoint.KeepAlive.HasValue)
{
var value = endpoint.KeepAlive.Value;
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, value);
}

if (endpoint.TcpKeepAliveInterval.HasValue)
{
var value = endpoint.TcpKeepAliveInterval.Value;
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.TcpKeepAliveInterval, value);
}

if (endpoint.TcpKeepAliveRetryCount.HasValue)
{
var value = endpoint.TcpKeepAliveRetryCount.Value;
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.TcpKeepAliveRetryCount, value);
}

if (endpoint.TcpKeepAliveTime.HasValue)
{
var value = endpoint.TcpKeepAliveTime.Value;
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.TcpKeepAliveTime, value);
}
}

private static void AdaptTo(this MqttServerTlsTcpEndpointOptions tlsEndPoint, HttpsConnectionAdapterOptions httpsOptions)
{
httpsOptions.SslProtocols = tlsEndPoint.SslProtocol;
Expand Down

0 comments on commit 51fb185

Please sign in to comment.