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

Conversation

@xljiulang
Copy link
Contributor

@xljiulangxljiulang commentedNov 14, 2024
edited
Loading

After several submissions, I have implemented the following refactorings. See also#2101

IMqttBuilder

IMqttBuilder is used to provide common functions for build MqttServer and MqttClient, such as logging.

publicinterfaceIMqttBuilder{IServiceCollectionServices{get;}}

Extensions

publicstaticIMqttBuilderUseMqttNetNullLogger(thisIMqttBuilderbuilder);publicstaticIMqttBuilderUseAspNetCoreMqttNetLogger(thisIMqttBuilderbuilder);publicstaticIMqttBuilderUseLogger<TLogger>(thisIMqttBuilderbuilder)whereTLogger:class,IMqttNetLogger;

IMqttServerBuilder

publicinterfaceIMqttServerBuilder:IMqttBuilder{}

Extensions

publicstaticIMqttServerBuilderAddMqttServer(thisIServiceCollectionservices);publicstaticIMqttServerBuilderConfigureMqttServer(thisIMqttServerBuilderbuilder,Action<MqttServerOptionsBuilder>configure);publicstaticIMqttServerBuilderConfigureMqttServerStop(thisIMqttServerBuilderbuilder,Action<MqttServerStopOptionsBuilder>configure);publicstaticIMqttServerBuilderAddMqttServerAdapter<TMqttServerAdapter>(thisIMqttServerBuilderbuilder);

IMqttClientBuilder

publicinterfaceIMqttClientBuilder:IMqttBuilder{}

Extensions

publicstaticIMqttClientBuilderAddMqttClient(thisIServiceCollectionservices);publicstaticIMqttClientBuilderUseMQTTnetMqttClientAdapterFactory(thisIMqttClientBuilderbuilder);publicstaticIMqttClientBuilderUseAspNetCoreMqttClientAdapterFactory(thisIMqttClientBuilderbuilder);publicstaticIMqttClientBuilderUseMqttClientAdapterFactory<TMqttClientAdapterFactory>(thisIMqttClientBuilderbuilder)whereTMqttClientAdapterFactory:class,IMqttClientAdapterFactory;

New implementation of IMqttClientAdapterFactory

The SocketConnection that the original MqttClientConnectionContextFactory depends on is a copy of the early code of the kestrel project. It is now deleted and replaced bythe IConnectionFactory service of Asp.NetCore to create ConnectionContext instead of SocketConnection ClientConnectionContext, which is a wrapper class for Stream and now supports TCP, TLS, WS and WSS. See also#2104#2105

IConnectionFactory only provides an asynchronous ConnectAsync() method. In order to call this asynchronous method, we need to modify the CreateClientAdapter method of IMqttClientAdapterFactory toValueTask<IMqttChannelAdapter> CreateClientAdapterAsync().

Now, the implementation type of IMqttClientAdapterFactory is AspNetCoreMqttClientAdapterFactory, which is an internal modified type and is used by users through IMqttClientBuilder.UseAspNetCoreMqttClientAdapterFactory().

The dependency chain is IMqttClientAdapterFactory-->AspNetCoreMqttClientAdapterFactory-->MqttClientChannelAdapter-->MqttChannel-->ClientConnectionContext

Split MqttHostedServer

MqttHostedServer acts as both MqttServer and HostedService, which makes it difficult to register the service and start the service background.

It has now been split into AspNetCoreMqttServer and AspNetCoreMqttHostedServer. AspNetCoreMqttHostedServer inherits BackgroundService. In its ExecuteAsync method, it waits for the application to start and then starts AspNetCoreMqttServer.#2102

Split MqttConnectionHandler

MqttConnectionHandler no longer implements the IMqttServerAdapter interface. The implementation type of IMqttServerAdapter is AspNetCoreMqttServerAdapter.

The dependency chain is IMqttServerAdapter-->AspNetCoreMqttServerAdapter-->MqttConnectionHandler-->MqttServerChannelAdapter-->MqttChannel-->Kestrel ConnectionContext

Add MqttBufferWriterPool in server mode

MqttBufferWriterPool pools the MqttBufferWriters of channels whose lifecycles are less than one minute to reduce the number of MqttBufferWriters created when clients frequently connect and disconnect.

Add MqttConnectionMiddleware

MqttConnectionMiddleware allows a single port to support both mqtt and mqtt-overt-websocket protocols. It is ultimately applied by IConnectionBuilder.UseMqtt(MqttProtocols).

Add KestrelServerOptions.ListenMqtt(MqttProtocols) extensions

This extension perfectly adapts the listening options of MqttServerOptions to kestrel, allowing users to continue to use the familiar MqttServerOptionsBuilder. In addition, we also provide enhanced httpsOptions configuration.

Make all implementation types internal

Now all implementation types have been modified to internal, and use InternalsVisibleToAttribute to expose them to the specified assembly.

Logger

The new AspNetCoreMqttNetLogger type is now the default IMqttNetLogger, which writes log content to Microsoft.Extensions.Logging.

Nullable

Nullable feature is enabled.

UnitTest

The newAspNetCoreTestEnvironment is added to the test environment and participates in the test at the same time as the defaultTestEnvironment.

Exception

Now the exception types and exception behaviors of MqttChannel are exactly the same as those of MqttChannelAdapter.

@xljiulang
Copy link
ContributorAuthor

@dotnet-policy-service agree

@xljiulang
Copy link
ContributorAuthor

The development of this PR has been completed and has passed the same one-sided tests as MQTTnet(Client) and MQTTnet.Server.
In the future, after#2109 is merged into the master branch, the new method of MqttPacketInspector can be continued to be adapted to reduce memory allocation.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@CZEMacLeodCZEMacLeodCZEMacLeod left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@xljiulang@CZEMacLeod

[8]ページ先頭

©2009-2025 Movatter.jp