From 51fb18564776c56da122860cf4ab5318b831bcfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Mon, 9 Dec 2024 14:56:27 +0800 Subject: [PATCH] Adapt MqttServerTcpEndpointBaseOptions to the Socket accepted by kestrel. --- .../KestrelServerOptionsExtensions.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Source/MQTTnet.AspnetCore/KestrelServerOptionsExtensions.cs b/Source/MQTTnet.AspnetCore/KestrelServerOptionsExtensions.cs index e91f7f754..6f33a3053 100644 --- a/Source/MQTTnet.AspnetCore/KestrelServerOptionsExtensions.cs +++ b/Source/MQTTnet.AspnetCore/KestrelServerOptionsExtensions.cs @@ -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; @@ -10,6 +11,7 @@ using MQTTnet.Server; using System; using System.Net; +using System.Net.Sockets; using System.Security.Cryptography.X509Certificates; namespace MQTTnet.AspNetCore @@ -82,6 +84,16 @@ void Listen(MqttServerTcpEndpointBaseOptions endpoint) void UseMiddleware(ListenOptions listenOptions) { + listenOptions.Use(next => context => + { + var socketFeature = context.Features.Get(); + if (socketFeature != null) + { + endpoint.AdaptTo(socketFeature.Socket); + } + return next(context); + }); + if (endpoint is MqttServerTlsTcpEndpointOptions tlsEndPoint) { listenOptions.UseHttps(httpsOptions => @@ -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;