-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring MQTTnet.AspNetCore #2103
Open
xljiulang
wants to merge
91
commits into
dotnet:master
Choose a base branch
from
xljiulang:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dotnet-policy-service agree |
…IMqttClientAdapterFactory
…in certain areas.
… with TestEnvironment.
The development of this PR has been completed and has passed the same one-sided tests as MQTTnet(Client) and MQTTnet.Server. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Extensions
IMqttServerBuilder
Extensions
IMqttClientBuilder
Extensions
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 by
the IConnectionFactory service of Asp.NetCore to create ConnectionContext instead of SocketConnectionClientConnectionContext, which is a wrapper class for Stream and now supports TCP, TLS, WS and WSS. See also #2104 #2105IConnectionFactory 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 new
AspNetCoreTestEnvironment
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.