Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Refactoring MQTTnet.AspNetCore#2103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
xljiulang wants to merge91 commits intodotnet:master
base:master
Choose a base branch
Loading
fromxljiulang:master
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
91 commits
Select commitHold shift + click to select a range
897c551
Use IConnectionFactory to create ConnectionContext to replace SocketC…
xljiulangNov 7, 2024
f38cca3
Refactoring AspNetMQTT
xljiulangNov 12, 2024
d8c94bd
Separate AspNetCoreMqttServerAdapter from MqttConnectionHandler
xljiulangNov 13, 2024
73502e4
TryAdd IConnectionFactory as Singleton
xljiulangNov 13, 2024
3a641ab
Add some remarks
xljiulangNov 13, 2024
91763ae
Add AspNetCoreMqttNetLogger
xljiulangNov 13, 2024
9c14222
Delayed start of AspNetCoreMqttServer
xljiulangNov 13, 2024
2372352
Using fields to cache IHttpContextFeature
xljiulangNov 13, 2024
8d96e19
Update Server_ASP_NET_Samples
xljiulangNov 13, 2024
ad5c798
Use ActivatorUtilities to create TMQttServerWrapper
xljiulangNov 13, 2024
7f95f02
Add IMqttServerBuilder
xljiulangNov 13, 2024
7b44ea2
Add IMqttClientBuilder
xljiulangNov 13, 2024
73c1365
Conditionally load SocketConnectionFactoryAssembly
xljiulangNov 13, 2024
2c10d2c
Add LICENSE
xljiulangNov 13, 2024
6cda003
DynamicallyAccessedMembers
xljiulangNov 13, 2024
faaadbd
Inject IOptions<MqttServerOptionsBuilder>
xljiulangNov 13, 2024
13c9198
Change the namespace to MQTTnet.AspNetCore
xljiulangNov 13, 2024
b8d8abb
await for_aspNetCoreMqttServer.StartAsync
xljiulangNov 14, 2024
64ed638
enable Nullable
xljiulangNov 14, 2024
3526212
Always dispose _connection of AspNetCoreMqttChannelAdapter
xljiulangNov 14, 2024
b4b0b04
UseTls
xljiulangNov 14, 2024
d9e02ce
Restore the IMqttClientAdapterFactory interface
xljiulangNov 15, 2024
b5019bd
Calculate the property values ​​when constructing MqttChannel
xljiulangNov 15, 2024
c206a4a
MqttServerChannelAdapter is modified to inherit MqttChannel
xljiulangNov 15, 2024
0a216a9
Add ClientConnectionContext.WebSocket
xljiulangNov 15, 2024
580257a
Add LICENSE
xljiulangNov 15, 2024
4f6b7ae
Add support for CreateLowLevelMqttClient
xljiulangNov 15, 2024
b25159b
AddMqttClient: Use AspNetCoreMqttClientAdapterFactory as the default …
xljiulangNov 16, 2024
ecb404f
Check that UseMqtt() and MapMqtt() are used.
xljiulangNov 16, 2024
858a914
AspNetCoreMqttServerAdapter: Logging when MqttServerOptions are ignored
xljiulangNov 16, 2024
fedb631
Add IMqttServerBuilder.AddMqttServerAdapter() extensions
xljiulangNov 16, 2024
67239cd
Register MqttServerOptions and MqttServerStopOptions as services
xljiulangNov 16, 2024
e03b9b1
IMqttBuilder: Add IMqttBuilder.UseAspNetCoreMqttNetLogger() extension
xljiulangNov 16, 2024
014a50b
Remove some dead code.
xljiulangNov 16, 2024
ad62877
Apply the properties of MqttClientTcpOptions to the Socket
xljiulangNov 16, 2024
c49beb0
TlsConnectionFeature supports passing in ClientCertificate
xljiulangNov 17, 2024
4c55368
Add support for MqttPacketInspector
xljiulangNov 17, 2024
34a4db2
Add route syntax for pattern parameter.
xljiulangNov 17, 2024
6dc18e1
Add KestrelServerOptions.ListenMqtt() extensions.
xljiulangNov 17, 2024
67dfda4
Optimize the implementation of ListenMqtt.
xljiulangNov 17, 2024
5813a13
Adapt MqttServerTlsTcpEndpointOptions to HttpsConnectionAdapterOptions
xljiulangNov 17, 2024
7628614
Compatible with the default server certificate.
xljiulangNov 17, 2024
7d8562a
Supports both MQTT and MQTT over WebSocket on a ConnectionContext.
xljiulangNov 18, 2024
1bd5949
MqttProtocols adds WebSocket item.
xljiulangNov 18, 2024
c4d9a22
Make sure services.AddMqttServer() has been called before operating M…
xljiulangNov 18, 2024
62313c8
Simplify MqttConnectionMiddleware
xljiulangNov 18, 2024
e087acb
Improve the compatibility of wss connections.
xljiulangNov 18, 2024
db4614c
Update UnixSocket sample.
xljiulangNov 18, 2024
acf8ed7
Update benchmark
xljiulangNov 18, 2024
457dcc1
Enhanced IOptions of MQTT ServiceOptions
xljiulangNov 19, 2024
358c2c0
add IMqttServerBuilder.ConfigureMqttSocketTransport extension.
xljiulangNov 19, 2024
02778aa
Simplifying MqttOptionsFactory
xljiulangNov 19, 2024
b46b357
Optimizing MqttChannel.SendPacketAsync
xljiulangNov 19, 2024
38eb1c3
Check buffer IsEmpty.
xljiulangNov 20, 2024
f953d6d
Merge branch 'dotnet:master' into master
xljiulangNov 30, 2024
ef838f8
Register MqttClientFactory as a service.
xljiulangNov 30, 2024
fcad255
Merge branch 'dotnet:master' into master
xljiulangDec 1, 2024
72b42d5
MqttOptionsFactory.Build() -> MqttOptionsFactory.CreateOptions()
xljiulangDec 2, 2024
b40c8a1
Add MqttBufferWriterPool
xljiulangDec 2, 2024
4305ad7
Merge branch 'dotnet:master' into master
xljiulangDec 2, 2024
cb68a13
Merged from the master branch.
xljiulangDec 2, 2024
a2c014f
Adapt the RemoteEndPoint property.
xljiulangDec 2, 2024
dad4faa
Add MqttBufferWriterPoolOptions
xljiulangDec 3, 2024
6f60eae
Add more conditions to the pool of MqttBufferWriterPoolOptions.
xljiulangDec 3, 2024
cc64f95
Merge branch 'main'
xljiulangDec 4, 2024
54f0b4d
MqttBufferWriterPool: Implementing the IReadOnlyCollection interface.
xljiulangDec 4, 2024
ee4de82
ConfigureAwait(false)
xljiulangDec 4, 2024
b708344
MqttChannel: adapt AllowPacketFragmentation option.
xljiulangDec 4, 2024
13e8190
Fixed the issue that GetRemoteEndPoint did not use the remoteEndPoint…
xljiulangDec 4, 2024
4bfd77d
Add some unit tests.
xljiulangDec 4, 2024
9d9dd44
Add more unit test.
xljiulangDec 4, 2024
8b7c411
MqttBufferWriterPoolOptions: Renaming properties.
xljiulangDec 4, 2024
8a3624d
Rename and update benchmark.
xljiulangDec 5, 2024
06f03cb
Add AspNetCoreTestEnvironment to test environments.
xljiulangDec 6, 2024
146b161
CreateTestEnvironment -> CreateMQTTnetTestEnvironment
xljiulangDec 6, 2024
83148ca
Remove Google's connection test to avoid Google being blocked by SNI …
xljiulangDec 6, 2024
41af5c5
AspNetCoreTestEnvironment: Adapt logger.
xljiulangDec 6, 2024
f94e17a
wait with timeout.
xljiulangDec 6, 2024
efcd6ab
AspNetCoreTestEnvironment: Adjust all configurations to be consistent…
xljiulangDec 6, 2024
b035659
MqttChannel: Exception handling remains consistent with MqttChannelAd…
xljiulangDec 6, 2024
6171c81
CrossPlatformSocket_Tests: create a localhost web server for remote h…
xljiulangDec 6, 2024
9a7a8bd
Server-side adaptation of AllowPacketFragmentation options.
xljiulangDec 7, 2024
e01e5f3
Merge implementation of IsAllowPacketFragmentation.
xljiulangDec 8, 2024
bffe065
Added UseLogger overloaded method and renamed an internal method.
xljiulangDec 8, 2024
ca5d13d
Add some extension methods to IMqttChannelAdapter.
xljiulangDec 8, 2024
8ceba5f
Add IAspNetCoreMqttChannel and remove IAspNetCoreMqttChannelAdapter;
xljiulangDec 9, 2024
b65c176
MapMqtt: Restricted to WebSocket transport protocol.
xljiulangDec 9, 2024
51fb185
Adapt MqttServerTcpEndpointBaseOptions to the Socket accepted by kest…
xljiulangDec 9, 2024
79f4f68
SocketOptionName.ReuseAddress can only be used for listening Socket s…
xljiulangDec 9, 2024
7139431
Accurately detect the DualMode value of listenSocket.
xljiulangDec 9, 2024
5c6e128
Merge branch 'dotnet:master' into master
xljiulangDec 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Use IConnectionFactory to create ConnectionContext to replace SocketC…
…onnection
  • Loading branch information
@xljiulang
xljiulang committedNov 7, 2024
commit897c551d8cb9ef0e6d47380f4a0460c108730b31
2 changes: 1 addition & 1 deletionSamples/MQTTnet.Samples.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,7 @@
<NuGetAuditMode>all</NuGetAuditMode>
<NuGetAudit>true</NuGetAudit>
<NuGetAuditLevel>low</NuGetAuditLevel>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<!--<AnalysisLevel>latest-Recommended</AnalysisLevel>-->
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletionSource/MQTTnet.AspTestApp/MQTTnet.AspTestApp.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,7 @@
<NuGetAuditMode>all</NuGetAuditMode>
<NuGetAudit>true</NuGetAudit>
<NuGetAuditLevel>low</NuGetAuditLevel>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<!--<AnalysisLevel>latest-Recommended</AnalysisLevel>-->
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletionSource/MQTTnet.AspnetCore/MQTTnet.AspNetCore.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,7 +37,7 @@
<NuGetAudit>true</NuGetAudit>
<NuGetAuditLevel>low</NuGetAuditLevel>
<NuGetAuditLevel>low</NuGetAuditLevel>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<!--<AnalysisLevel>latest-Recommended</AnalysisLevel>-->
</PropertyGroup>

<ItemGroup>
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,25 +2,37 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

usingSystem;
usingMicrosoft.AspNetCore.Connections;
using MQTTnet.Adapter;
using MQTTnet.Diagnostics.Logger;
using MQTTnet.Formatter;
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace MQTTnet.AspNetCore
{
public sealed class MqttClientConnectionContextFactory : IMqttClientAdapterFactory
{
public IMqttChannelAdapter CreateClientAdapter(MqttClientOptions options, MqttPacketInspector packetInspector, IMqttNetLogger logger)
private readonly IConnectionFactory connectionFactory;

public MqttClientConnectionContextFactory(IConnectionFactory connectionFactory)
{
this.connectionFactory = connectionFactory;
}

public async ValueTask<IMqttChannelAdapter> CreateClientAdapterAsync(MqttClientOptions options, MqttPacketInspector packetInspector, IMqttNetLogger logger)
{
if (options == null) throw new ArgumentNullException(nameof(options));

switch (options.ChannelOptions)
{
case MqttClientTcpOptions tcpOptions:
{
vartcpConnection =new SocketConnection(tcpOptions.RemoteEndpoint);

varendPoint =await CreateIPEndPointAsync(tcpOptions.RemoteEndpoint);
var tcpConnection = await connectionFactory.ConnectAsync(endPoint);
var formatter = new MqttPacketFormatterAdapter(options.ProtocolVersion, new MqttBufferWriter(4096, 65535));
return new MqttConnectionContext(formatter, tcpConnection);
}
Expand All@@ -30,5 +42,24 @@ public IMqttChannelAdapter CreateClientAdapter(MqttClientOptions options, MqttPa
}
}
}

private static async ValueTask<IPEndPoint> CreateIPEndPointAsync(EndPoint endpoint)
{
if (endpoint is IPEndPoint ipEndPoint)
{
return ipEndPoint;
}

if (endpoint is DnsEndPoint dnsEndPoint)
{
var hostEntry = await Dns.GetHostEntryAsync(dnsEndPoint.Host);
var address = hostEntry.AddressList.OrderBy(item => item.AddressFamily).FirstOrDefault();
return address == null
? throw new SocketException((int)SocketError.HostNotFound)
: new IPEndPoint(address, dnsEndPoint.Port);
}

throw new NotSupportedException("Only supports IPEndPoint or DnsEndPoint for now.");
}
}
}
38 changes: 14 additions & 24 deletionsSource/MQTTnet.AspnetCore/MqttConnectionContext.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,13 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers;
using System.IO.Pipelines;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Connections.Features;
using Microsoft.AspNetCore.Http.Features;
Expand All@@ -17,6 +10,13 @@
using MQTTnet.Formatter;
using MQTTnet.Internal;
using MQTTnet.Packets;
using System;
using System.Buffers;
using System.IO.Pipelines;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;

namespace MQTTnet.AspNetCore;

Expand All@@ -25,19 +25,15 @@ public sealed class MqttConnectionContext : IMqttChannelAdapter
readonly ConnectionContext _connection;
readonly AsyncLock _writerLock = new();

PipeReader _input;
PipeWriter _output;
readonlyPipeReader _input;
readonlyPipeWriter _output;

public MqttConnectionContext(MqttPacketFormatterAdapter packetFormatterAdapter, ConnectionContext connection)
{
PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter));
_connection = connection ?? throw new ArgumentNullException(nameof(connection));

if (!(_connection is SocketConnection tcp) || tcp.IsConnected)
{
_input = connection.Transport.Input;
_output = connection.Transport.Output;
}
_input = connection.Transport.Input;
_output = connection.Transport.Output;
}

public long BytesReceived { get; private set; }
Expand DownExpand Up@@ -106,15 +102,9 @@ public bool IsSecureConnection

public MqttPacketFormatterAdapter PacketFormatterAdapter { get; }

publicasyncTask ConnectAsync(CancellationToken cancellationToken)
public Task ConnectAsync(CancellationToken cancellationToken)
{
if (_connection is SocketConnection tcp && !tcp.IsConnected)
{
await tcp.StartAsync().ConfigureAwait(false);
}

_input = _connection.Transport.Input;
_output = _connection.Transport.Output;
return Task.CompletedTask;
}

public Task DisconnectAsync(CancellationToken cancellationToken)
Expand All@@ -126,7 +116,7 @@ public Task DisconnectAsync(CancellationToken cancellationToken)
}

public void Dispose()
{
{
_writerLock.Dispose();
}

Expand Down
10 changes: 4 additions & 6 deletionsSource/MQTTnet.AspnetCore/MqttConnectionHandler.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,13 +33,11 @@ public override async Task OnConnectedAsync(ConnectionContext connection)
}

var formatter = new MqttPacketFormatterAdapter(new MqttBufferWriter(_serverOptions.WriterBufferSize, _serverOptions.WriterBufferSizeMax));
using (var adapter = new MqttConnectionContext(formatter, connection))
using var adapter = new MqttConnectionContext(formatter, connection);
var clientHandler = ClientHandler;
if (clientHandler != null)
{
var clientHandler = ClientHandler;
if (clientHandler != null)
{
await clientHandler(adapter).ConfigureAwait(false);
}
await clientHandler(adapter).ConfigureAwait(false);
}
}

Expand Down
20 changes: 19 additions & 1 deletionSource/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,18 +2,25 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

usingSystem;
usingMicrosoft.AspNetCore.Connections;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using MQTTnet.Adapter;
using MQTTnet.Diagnostics.Logger;
using MQTTnet.Server;
using MQTTnet.Server.Internal.Adapter;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace MQTTnet.AspNetCore;

public static class ServiceCollectionExtensions
{
const string SocketConnectionFactoryTypeName = "Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionFactory";
const string SocketConnectionFactoryAssemblyName = "Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets";

public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, MqttServerOptions options)
{
ArgumentNullException.ThrowIfNull(services);
Expand DownExpand Up@@ -106,4 +113,15 @@ public static IServiceCollection AddMqttWebSocketServerAdapter(this IServiceColl

return services;
}


[DynamicDependency(DynamicallyAccessedMemberTypes.All, SocketConnectionFactoryTypeName, SocketConnectionFactoryAssemblyName)]
public static IServiceCollection AddMqttClientConnectionContextFactory(this IServiceCollection services)
{
var socketConnectionFactoryType = Assembly.Load(SocketConnectionFactoryAssemblyName).GetType(SocketConnectionFactoryTypeName);
services.AddSingleton(typeof(IConnectionFactory), socketConnectionFactoryType);
services.TryAddSingleton<MqttClientConnectionContextFactory>();
services.TryAddSingleton<IMqttClientAdapterFactory>(serviceProvider => serviceProvider.GetRequiredService<MqttClientConnectionContextFactory>());
return services;
}
}
77 changes: 0 additions & 77 deletionsSource/MQTTnet.AspnetCore/SocketAwaitable.cs
View file
Open in desktop

This file was deleted.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp