Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
Refactor code
  • Loading branch information
chkr1011 committed May 15, 2024
1 parent 2bf56a7 commit 4adfca1
Show file tree
Hide file tree
Showing 176 changed files with 6,165 additions and 6,294 deletions.
8 changes: 4 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ dotnet_naming_rule.static_readonly_rule.symbols = static_readonly_symbols
dotnet_naming_style.lower_camel_case_style.capitalization = camel_case
dotnet_naming_style.lower_camel_case_style.required_prefix = _
dotnet_naming_style.upper_camel_case_style.capitalization = pascal_case
dotnet_naming_symbols.constants_symbols.applicable_accessibilities = public,internal,protected,protected_internal,private_protected
dotnet_naming_symbols.constants_symbols.applicable_accessibilities = public, internal, protected, protected_internal, private_protected
dotnet_naming_symbols.constants_symbols.applicable_kinds = field
dotnet_naming_symbols.constants_symbols.required_modifiers = const
dotnet_naming_symbols.public_fields_symbols.applicable_accessibilities = public,internal,protected,protected_internal,private_protected
dotnet_naming_symbols.public_fields_symbols.applicable_accessibilities = public, internal, protected, protected_internal, private_protected
dotnet_naming_symbols.public_fields_symbols.applicable_kinds = field
dotnet_naming_symbols.static_readonly_symbols.applicable_accessibilities = public,internal,protected,protected_internal,private_protected
dotnet_naming_symbols.static_readonly_symbols.applicable_accessibilities = public, internal, protected, protected_internal, private_protected
dotnet_naming_symbols.static_readonly_symbols.applicable_kinds = field
dotnet_naming_symbols.static_readonly_symbols.required_modifiers = static,readonly
dotnet_naming_symbols.static_readonly_symbols.required_modifiers = static, readonly
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ jobs:
- name: Build solution
run: dotnet build MQTTnet.sln --configuration Release /p:FileVersion=${{ env.VERSION }} /p:AssemblyVersion=${{ env.VERSION }} /p:PackageVersion=${{ env.VERSION }} /p:SignAssembly=true /p:AssemblyOriginatorKeyFile=${{ github.workspace }}/certificate.snk

- name: Collect nuget Packages
- name: Collect nuget packages
uses: actions/upload-artifact@v2
with:
name: nuget Packages
path: |
**\*.nupkg
**\*.snupkg
**/*.nupkg
**/*.snupkg
- name: Execute tests
run: dotnet test --no-restore --framework net8.0 Source/MQTTnet.Tests/MQTTnet.Tests.csproj
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

# MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports the MQTT protocol up to version 5. It is compatible with mostly any supported .NET Framework version and CPU architecture.
MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (
broker) and supports the MQTT protocol up to version 5. It is compatible with mostly any supported .NET Framework
version and CPU architecture.

## Features

Expand All @@ -28,13 +30,15 @@ MQTTnet is a high performance .NET library for MQTT based communication. It prov
* Unit tested (~636 tests)
* No external dependencies

\* Tested on local machine (Intel i7 8700K) with MQTTnet client and server running in the same process using the TCP channel. The app for verification is part of this repository and stored in _/Tests/MQTTnet.TestApp.NetCore_.
\* Tested on local machine (Intel i7 8700K) with MQTTnet client and server running in the same process using the TCP
channel. The app for verification is part of this repository and stored in _/Tests/MQTTnet.TestApp.NetCore_.

### Client

* Communication via TCP (+TLS) or WS (WebSocket) supported
* Included core _LowLevelMqttClient_ with low level functionality
* Also included _ManagedMqttClient_ which maintains the connection and subscriptions automatically. Also application messages are queued and re-scheduled for higher QoS levels automatically.
* Also included _ManagedMqttClient_ which maintains the connection and subscriptions automatically. Also application
messages are queued and re-scheduled for higher QoS levels automatically.
* Rx support (via another project)
* Compatible with Microsoft Azure IoT Hub

Expand All @@ -52,9 +56,11 @@ MQTTnet is a high performance .NET library for MQTT based communication. It prov

## Getting Started

MQTTnet is delivered via <b>NuGet</b> package manager. You can find the packages here: https://www.nuget.org/packages/MQTTnet/
MQTTnet is delivered via <b>NuGet</b> package manager. You can find the packages
here: https://www.nuget.org/packages/MQTTnet/

Use these command in the Package Manager console to install MQTTnet manually:

```
Install-Package MQTTnet
```
Expand All @@ -68,7 +74,8 @@ Samples for using MQTTnet are part of this repository. For starters these sample

## Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community.
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our
community.
For more information see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).

## .NET Foundation
Expand Down
10 changes: 5 additions & 5 deletions Samples/MQTTnet.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

<ItemGroup>

<ProjectReference Include="..\Source\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj" />
<ProjectReference Include="..\Source\MQTTnet.Extensions.ManagedClient\MQTTnet.Extensions.ManagedClient.csproj" />
<ProjectReference Include="..\Source\MQTTnet.Extensions.Rpc\MQTTnet.Extensions.Rpc.csproj" />
<ProjectReference Include="..\Source\MQTTnet.Server\MQTTnet.Server.csproj" />
<ProjectReference Include="..\Source\MQTTnet\MQTTnet.csproj" />
<ProjectReference Include="..\Source\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj"/>
<ProjectReference Include="..\Source\MQTTnet.Extensions.ManagedClient\MQTTnet.Extensions.ManagedClient.csproj"/>
<ProjectReference Include="..\Source\MQTTnet.Extensions.Rpc\MQTTnet.Extensions.Rpc.csproj"/>
<ProjectReference Include="..\Source\MQTTnet.Server\MQTTnet.Server.csproj"/>
<ProjectReference Include="..\Source\MQTTnet\MQTTnet.csproj"/>
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion Samples/Server/Server_Diagnostics_Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace MQTTnet.Samples.Server;
// ReSharper disable InconsistentNaming
// ReSharper disable EmptyConstructor
// ReSharper disable MemberCanBeMadeStatic.Local

public static class Server_Diagnostics_Samples
{
public static async Task Get_Notified_When_Client_Received_Message()
Expand Down
2 changes: 1 addition & 1 deletion Samples/Server/Server_Simple_Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,4 @@ public void Publish(MqttNetLogLevel logLevel, string source, string message, obj
}
}
}
}
}
8 changes: 4 additions & 4 deletions Source/MQTTnet.AspTestApp/MQTTnet.AspTestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj" />
<ProjectReference Include="..\..\Source\MQTTnet.Server\MQTTnet.Server.csproj" />
<ProjectReference Include="..\MQTTnet\MQTTnet.csproj" />
<ProjectReference Include="..\..\Source\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj"/>
<ProjectReference Include="..\..\Source\MQTTnet.Server\MQTTnet.Server.csproj"/>
<ProjectReference Include="..\MQTTnet\MQTTnet.csproj"/>
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
<Folder Include="wwwroot\"/>
</ItemGroup>

</Project>
33 changes: 17 additions & 16 deletions Source/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
using Microsoft.Extensions.DependencyInjection;
using MQTTnet.Server;

namespace MQTTnet.AspNetCore
namespace MQTTnet.AspNetCore;

public static class ApplicationBuilderExtensions
{
public static class ApplicationBuilderExtensions
[Obsolete(
"This class is obsolete and will be removed in a future version. The recommended alternative is to use MapMqtt inside Microsoft.AspNetCore.Builder.UseEndpoints(...).")]
public static IApplicationBuilder UseMqttEndpoint(this IApplicationBuilder app, string path = "/mqtt")
{
[Obsolete("This class is obsolete and will be removed in a future version. The recommended alternative is to use MapMqtt inside Microsoft.AspNetCore.Builder.UseEndpoints(...).")]
public static IApplicationBuilder UseMqttEndpoint(this IApplicationBuilder app, string path = "/mqtt")
{
app.UseWebSockets();
app.Use(async (context, next) =>
app.UseWebSockets();
app.Use(
async (context, next) =>
{
if (!context.WebSockets.IsWebSocketRequest || context.Request.Path != path)
{
Expand All @@ -37,16 +39,15 @@ public static IApplicationBuilder UseMqttEndpoint(this IApplicationBuilder app,
}
});

return app;
}
return app;
}

public static IApplicationBuilder UseMqttServer(this IApplicationBuilder app, Action<MqttServer> configure)
{
var server = app.ApplicationServices.GetRequiredService<MqttServer>();
public static IApplicationBuilder UseMqttServer(this IApplicationBuilder app, Action<MqttServer> configure)
{
var server = app.ApplicationServices.GetRequiredService<MqttServer>();

configure(server);
configure(server);

return app;
}
return app;
}
}
}
19 changes: 9 additions & 10 deletions Source/MQTTnet.AspnetCore/AspNetMqttServerOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using MQTTnet.Server;
using System;
using MQTTnet.Server;

namespace MQTTnet.AspNetCore;

namespace MQTTnet.AspNetCore
public sealed class AspNetMqttServerOptionsBuilder : MqttServerOptionsBuilder
{
public sealed class AspNetMqttServerOptionsBuilder : MqttServerOptionsBuilder
public AspNetMqttServerOptionsBuilder(IServiceProvider serviceProvider)
{
public AspNetMqttServerOptionsBuilder(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
}

public IServiceProvider ServiceProvider { get; }
ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
}
}

public IServiceProvider ServiceProvider { get; }
}
8 changes: 4 additions & 4 deletions Source/MQTTnet.AspnetCore/MQTTnet.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release'">
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MQTTnet.Server\MQTTnet.Server.csproj" />
<ProjectReference Include="..\MQTTnet\MQTTnet.csproj" />
<ProjectReference Include="..\MQTTnet.Server\MQTTnet.Server.csproj"/>
<ProjectReference Include="..\MQTTnet\MQTTnet.csproj"/>
</ItemGroup>

</Project>
72 changes: 35 additions & 37 deletions Source/MQTTnet.AspnetCore/MqttConnectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,56 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using MQTTnet.Adapter;
using MQTTnet.Server;
using System;
using System.Threading.Tasks;
using MQTTnet.Diagnostics;
using MQTTnet.Formatter;
using MQTTnet.Server.Adapter;
using MQTTnet.Server;

namespace MQTTnet.AspNetCore;

namespace MQTTnet.AspNetCore
public sealed class MqttConnectionHandler : ConnectionHandler, IMqttServerAdapter
{
public sealed class MqttConnectionHandler : ConnectionHandler, IMqttServerAdapter
{
MqttServerOptions _serverOptions;
MqttServerOptions _serverOptions;

public Func<IMqttChannelAdapter, Task> ClientHandler { get; set; }

public Func<IMqttChannelAdapter, Task> ClientHandler { get; set; }
public void Dispose()
{
}

public override async Task OnConnectedAsync(ConnectionContext connection)
public override async Task OnConnectedAsync(ConnectionContext connection)
{
// required for websocket transport to work
var transferFormatFeature = connection.Features.Get<ITransferFormatFeature>();
if (transferFormatFeature != null)
{
// required for websocket transport to work
var transferFormatFeature = connection.Features.Get<ITransferFormatFeature>();
if (transferFormatFeature != null)
{
transferFormatFeature.ActiveFormat = TransferFormat.Binary;
}
transferFormatFeature.ActiveFormat = TransferFormat.Binary;
}

var formatter = new MqttPacketFormatterAdapter(new MqttBufferWriter(_serverOptions.WriterBufferSize, _serverOptions.WriterBufferSizeMax));
using (var adapter = new MqttConnectionContext(formatter, connection))
var formatter = new MqttPacketFormatterAdapter(new MqttBufferWriter(_serverOptions.WriterBufferSize, _serverOptions.WriterBufferSizeMax));
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);
}
}
}

public Task StartAsync(MqttServerOptions options, IMqttNetLogger logger)
{
_serverOptions = options;

return Task.CompletedTask;
}
public Task StartAsync(MqttServerOptions options, IMqttNetLogger logger)
{
_serverOptions = options;

public Task StopAsync()
{
return Task.CompletedTask;
}
return Task.CompletedTask;
}

public void Dispose()
{
}
public Task StopAsync()
{
return Task.CompletedTask;
}
}
}
43 changes: 20 additions & 23 deletions Source/MQTTnet.AspnetCore/MqttHostedServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,33 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using MQTTnet.Adapter;
using MQTTnet.Diagnostics;
using MQTTnet.Server;
using MQTTnet.Server.Adapter;

namespace MQTTnet.AspNetCore
namespace MQTTnet.AspNetCore;

public sealed class MqttHostedServer : MqttServer, IHostedService
{
public sealed class MqttHostedServer : MqttServer, IHostedService
{
readonly MqttServerFactory _mqttServerFactory;
readonly MqttServerFactory _mqttServerFactory;

public MqttHostedServer(MqttServerFactory mqttServerFactory, MqttServerOptions options, IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger) : base(
options,
adapters,
logger)
{
_mqttServerFactory = mqttServerFactory ?? throw new ArgumentNullException(nameof(mqttServerFactory));
}
public MqttHostedServer(MqttServerFactory mqttServerFactory, MqttServerOptions options, IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger) : base(
options,
adapters,
logger)
{
_mqttServerFactory = mqttServerFactory ?? throw new ArgumentNullException(nameof(mqttServerFactory));
}

public async Task StartAsync(CancellationToken cancellationToken)
{
// The yield makes sure that the hosted service is considered up and running.
await Task.Yield();
public async Task StartAsync(CancellationToken cancellationToken)
{
// The yield makes sure that the hosted service is considered up and running.
await Task.Yield();

_ = StartAsync();
}
_ = StartAsync();
}

public Task StopAsync(CancellationToken cancellationToken)
{
return StopAsync(_mqttServerFactory.CreateMqttServerStopOptionsBuilder().Build());
}
public Task StopAsync(CancellationToken cancellationToken)
{
return StopAsync(_mqttServerFactory.CreateMqttServerStopOptionsBuilder().Build());
}
}
Loading

0 comments on commit 4adfca1

Please sign in to comment.