mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 19:58:57 +08:00
Improve performance and drop support for old frameworks (#2068)
* Remove WebSocket4Net extension library * Remove UWP support * Drop support for all old frameworks * Update CI * Update CI * Update CI * Update CI * Remove obsolete code * Set default protocol version to 5.0.0 * Remove obsolete tests * Remove old dependencies * Reset release notes * Move server to dedicated project * Fix unit tests * Update and cleanup nuget packages * Fix unit tests Refactor code * No longer throw exception when receiving non success CONNACK * Enable nuget signing * Enable nuget signing * Enable nuget signing * Enable nuget signing * Enable nuget signing * Enable nuget signing * Enable nuget signing * Enable nuget signing * Enable code signing * Enable code signing * Enable code signing * Fix client endpoint handling * Improve CI * Fix CI * Fix CI * Fix CI * Fix CI * Import changes from version 4 * Cleanup target frameworks and remove obsolete code * Drop support for dotnet7.0 * Remove managed client * Version5 auditmode (#2016) Added NuGet audit mode and some other settings to get informed about security issues. * Address warnings from analyzers * Support for ReadOnlySequence as payload (#2046) * use ReadOnlySequence as payload * fix build * fix build * skip sign in forks * remove unnecessary owner * tests and extensions * remove test * simplify WritePacketBuffer, remove unused helper * Adjust namespaces * Fix build errors * Import latest fixes from main branch * Adjust namespaces --------- Co-authored-by: SeppPenner <HansM2013@gmx.de> Co-authored-by: Martin Regen <mregen@microsoft.com>
This commit is contained in:
co-authored by
SeppPenner
Martin Regen
parent
e18a91a4b5
commit
980a5d0a6d
@@ -4,8 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
|
||||
namespace MQTTnet.TestApp
|
||||
{
|
||||
@@ -17,15 +16,15 @@ namespace MQTTnet.TestApp
|
||||
{
|
||||
var logger = new MqttNetEventLogger();
|
||||
MqttNetConsoleLogger.ForwardToConsole(logger);
|
||||
|
||||
var factory = new MqttFactory(logger);
|
||||
|
||||
|
||||
var factory = new MqttClientFactory(logger);
|
||||
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("localhost")
|
||||
.Build();
|
||||
|
||||
|
||||
Console.WriteLine("BEFORE CONNECT");
|
||||
await client.ConnectAsync(options);
|
||||
Console.WriteLine("AFTER CONNECT");
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
// 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.Client;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.Internal;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
@@ -22,7 +21,7 @@ namespace MQTTnet.TestApp
|
||||
var logger = new MqttNetEventLogger();
|
||||
MqttNetConsoleLogger.ForwardToConsole(logger);
|
||||
|
||||
var factory = new MqttFactory(logger);
|
||||
var factory = new MqttClientFactory(logger);
|
||||
var client = factory.CreateMqttClient();
|
||||
var clientOptions = new MqttClientOptions
|
||||
{
|
||||
@@ -35,21 +34,18 @@ namespace MQTTnet.TestApp
|
||||
client.ApplicationMessageReceivedAsync += e =>
|
||||
{
|
||||
var payloadText = string.Empty;
|
||||
if (e.ApplicationMessage.PayloadSegment.Count > 0)
|
||||
if (e.ApplicationMessage.Payload.Length > 0)
|
||||
{
|
||||
payloadText = Encoding.UTF8.GetString(
|
||||
e.ApplicationMessage.PayloadSegment.Array,
|
||||
e.ApplicationMessage.PayloadSegment.Offset,
|
||||
e.ApplicationMessage.PayloadSegment.Count);
|
||||
payloadText = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||
}
|
||||
|
||||
|
||||
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
|
||||
Console.WriteLine($"+ Payload = {payloadText}");
|
||||
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
||||
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
|
||||
Console.WriteLine();
|
||||
|
||||
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,25 +3,26 @@
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<DebugType>Full</DebugType>
|
||||
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
|
||||
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net452;net461</TargetFrameworks>
|
||||
<TargetFrameworks>net8.0</TargetFrameworks>
|
||||
<IsPackable>false</IsPackable>
|
||||
<EnableNETAnalyzers>false</EnableNETAnalyzers>
|
||||
<EnforceCodeStyleInBuild>false</EnforceCodeStyleInBuild>
|
||||
<DisableImplicitAspNetCoreAnalyzers>true</DisableImplicitAspNetCoreAnalyzers>
|
||||
<NoWarn>1591;NETSDK1138</NoWarn>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<NoWarn>1591;NETSDK1138;NU1803;NU1901;NU1902</NoWarn>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<NuGetAuditMode>all</NuGetAuditMode>
|
||||
<NuGetAudit>true</NuGetAudit>
|
||||
<AnalysisLevel>latest-Recommended</AnalysisLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Source\MQTTnet.Extensions.ManagedClient\MQTTnet.Extensions.ManagedClient.csproj" />
|
||||
<ProjectReference Include="..\..\Source\MQTTnet.Extensions.WebSocket4Net\MQTTnet.Extensions.WebSocket4Net.csproj" />
|
||||
<ProjectReference Include="..\..\Source\MQTTnet\MQTTnet.csproj" />
|
||||
<ProjectReference Include="..\MQTTnet.Extensions.TopicTemplate\MQTTnet.Extensions.TopicTemplate.csproj" />
|
||||
<ProjectReference Include="..\MQTTnet.Server\MQTTnet.Server.csproj"/>
|
||||
<ProjectReference Include="..\MQTTnet\MQTTnet.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// 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 System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Extensions.ManagedClient;
|
||||
using MQTTnet.Internal;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.TestApp
|
||||
{
|
||||
public static class ManagedClientTest
|
||||
{
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
var ms = new ClientRetainedMessageHandler();
|
||||
|
||||
var options = new ManagedMqttClientOptions
|
||||
{
|
||||
ClientOptions = new MqttClientOptions
|
||||
{
|
||||
ClientId = "MQTTnetManagedClientTest",
|
||||
Credentials = new RandomPassword(),
|
||||
ChannelOptions = new MqttClientTcpOptions
|
||||
{
|
||||
RemoteEndpoint = new DnsEndPoint("broker.hivemq.com", 0)
|
||||
}
|
||||
},
|
||||
|
||||
AutoReconnectDelay = TimeSpan.FromSeconds(1),
|
||||
Storage = ms
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var managedClient = new MqttFactory().CreateManagedMqttClient();
|
||||
managedClient.ApplicationMessageReceivedAsync += e =>
|
||||
{
|
||||
Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
await managedClient.StartAsync(options);
|
||||
|
||||
await managedClient.EnqueueAsync(topic: "Step", payload: "1");
|
||||
await managedClient.EnqueueAsync(topic: "Step", payload: "2", MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
|
||||
await managedClient.SubscribeAsync(topic: "xyz", qualityOfServiceLevel: MqttQualityOfServiceLevel.AtMostOnce);
|
||||
await managedClient.SubscribeAsync(topic: "abc", qualityOfServiceLevel: MqttQualityOfServiceLevel.AtMostOnce);
|
||||
|
||||
await managedClient.EnqueueAsync(topic: "Step", payload: "3");
|
||||
|
||||
Console.WriteLine("Managed client started.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class RandomPassword : IMqttClientCredentialsProvider
|
||||
{
|
||||
public string GetUserName(MqttClientOptions clientOptions)
|
||||
{
|
||||
return "the_static_user";
|
||||
}
|
||||
|
||||
public byte[] GetPassword(MqttClientOptions clientOptions)
|
||||
{
|
||||
return Guid.NewGuid().ToByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
public class ClientRetainedMessageHandler : IManagedMqttClientStorage
|
||||
{
|
||||
const string Filename = @"RetainedMessages.json";
|
||||
|
||||
public Task SaveQueuedMessagesAsync(IList<ManagedMqttApplicationMessage> messages)
|
||||
{
|
||||
File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
|
||||
return CompletedTask.Instance;
|
||||
}
|
||||
|
||||
public Task<IList<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync()
|
||||
{
|
||||
IList<ManagedMqttApplicationMessage> retainedMessages;
|
||||
if (File.Exists(Filename))
|
||||
{
|
||||
var json = File.ReadAllText(Filename);
|
||||
retainedMessages = JsonConvert.DeserializeObject<List<ManagedMqttApplicationMessage>>(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
retainedMessages = new List<ManagedMqttApplicationMessage>();
|
||||
}
|
||||
|
||||
return Task.FromResult(retainedMessages);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Server;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
@@ -77,8 +76,9 @@ namespace MQTTnet.TestApp
|
||||
new TopicGenerator().Generate(NumPublishers, NumTopicsPerPublisher, out _topicsByPublisher, out _singleWildcardTopicsByPublisher, out _multiWildcardTopicsByPublisher);
|
||||
|
||||
var serverOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
|
||||
var factory = new MqttFactory();
|
||||
_mqttServer = factory.CreateMqttServer(serverOptions);
|
||||
var mqttClientFactory = new MqttClientFactory();
|
||||
var mqttServerFactory = new MqttServerFactory();
|
||||
_mqttServer = mqttServerFactory.CreateMqttServer(serverOptions);
|
||||
await _mqttServer.StartAsync();
|
||||
|
||||
Console.WriteLine();
|
||||
@@ -90,7 +90,7 @@ namespace MQTTnet.TestApp
|
||||
foreach (var pt in _topicsByPublisher)
|
||||
{
|
||||
var publisherName = pt.Key;
|
||||
var mqttClient = factory.CreateMqttClient();
|
||||
var mqttClient = mqttClientFactory.CreateMqttClient();
|
||||
var publisherOptions = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("localhost")
|
||||
.WithClientId(publisherName)
|
||||
@@ -106,7 +106,7 @@ namespace MQTTnet.TestApp
|
||||
_mqttSubscriberClients = new List<IMqttClient>();
|
||||
for (var i = 0; i < NumSubscribers; ++i)
|
||||
{
|
||||
var mqttClient = factory.CreateMqttClient();
|
||||
var mqttClient = mqttClientFactory.CreateMqttClient();
|
||||
var subsriberOptions = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("localhost")
|
||||
.WithClientId("sub" + i)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
|
||||
namespace MQTTnet.TestApp
|
||||
{
|
||||
@@ -15,7 +15,7 @@ namespace MQTTnet.TestApp
|
||||
public static void ForwardToConsole(MqttNetEventLogger logger)
|
||||
{
|
||||
if (logger == null) throw new ArgumentNullException(nameof(logger));
|
||||
|
||||
|
||||
logger.LogMessagePublished -= PrintToConsole;
|
||||
logger.LogMessagePublished += PrintToConsole;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
|
||||
@@ -30,7 +29,7 @@ namespace MQTTnet.TestApp
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
var client = new MqttClientFactory().CreateMqttClient();
|
||||
client.ConnectAsync(options).GetAwaiter().GetResult();
|
||||
|
||||
var message = CreateMessage();
|
||||
@@ -62,9 +61,10 @@ namespace MQTTnet.TestApp
|
||||
{
|
||||
try
|
||||
{
|
||||
var mqttFactory = new MqttFactory();
|
||||
var mqttClientFactory = new MqttClientFactory();
|
||||
var mqttServerFactory = new MqttServerFactory();
|
||||
var mqttServerOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
|
||||
var mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions);
|
||||
var mqttServer = mqttServerFactory.CreateMqttServer(mqttServerOptions);
|
||||
await mqttServer.StartAsync().ConfigureAwait(false);
|
||||
|
||||
var options = new MqttClientOptions
|
||||
@@ -75,12 +75,12 @@ namespace MQTTnet.TestApp
|
||||
}
|
||||
};
|
||||
|
||||
var client = mqttFactory.CreateMqttClient();
|
||||
var client = mqttClientFactory.CreateMqttClient();
|
||||
await client.ConnectAsync(options).ConfigureAwait(false);
|
||||
|
||||
var message = new MqttApplicationMessageBuilder().WithTopic("t")
|
||||
.Build();
|
||||
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
@@ -122,7 +122,7 @@ namespace MQTTnet.TestApp
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
var client = new MqttClientFactory().CreateMqttClient();
|
||||
|
||||
try
|
||||
{
|
||||
@@ -214,7 +214,7 @@ namespace MQTTnet.TestApp
|
||||
{
|
||||
try
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(new MqttServerOptions());
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(new MqttServerOptions());
|
||||
await mqttServer.StartAsync();
|
||||
|
||||
var options = new MqttClientOptions
|
||||
@@ -226,7 +226,7 @@ namespace MQTTnet.TestApp
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
var client = new MqttClientFactory().CreateMqttClient();
|
||||
await client.ConnectAsync(options);
|
||||
|
||||
var message = new MqttApplicationMessage
|
||||
@@ -265,7 +265,7 @@ namespace MQTTnet.TestApp
|
||||
{
|
||||
try
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(new MqttServerOptions());
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(new MqttServerOptions());
|
||||
await mqttServer.StartAsync();
|
||||
|
||||
var options = new MqttClientOptions
|
||||
@@ -277,7 +277,7 @@ namespace MQTTnet.TestApp
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
var client = new MqttClientFactory().CreateMqttClient();
|
||||
await client.ConnectAsync(options);
|
||||
|
||||
var message = new MqttApplicationMessage
|
||||
@@ -328,7 +328,7 @@ namespace MQTTnet.TestApp
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
var client = new MqttClientFactory().CreateMqttClient();
|
||||
await client.ConnectAsync(options);
|
||||
|
||||
var message = new MqttApplicationMessage
|
||||
|
||||
@@ -2,99 +2,92 @@
|
||||
// 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.Diagnostics;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MQTTnet.TestApp
|
||||
namespace MQTTnet.TestApp;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
public static class Program
|
||||
public static void Main()
|
||||
{
|
||||
public static void Main()
|
||||
Console.WriteLine("MQTTnet - TestApp");
|
||||
Console.WriteLine("1 = Start client");
|
||||
Console.WriteLine("2 = Start server");
|
||||
Console.WriteLine("3 = Start performance test");
|
||||
Console.WriteLine("5 = Start public broker test");
|
||||
Console.WriteLine("6 = Start server & client");
|
||||
Console.WriteLine("7 = Client flow test");
|
||||
Console.WriteLine("8 = Start performance test (client only)");
|
||||
Console.WriteLine("9 = Start server (no trace)");
|
||||
Console.WriteLine("a = Start QoS 2 benchmark");
|
||||
Console.WriteLine("b = Start QoS 1 benchmark");
|
||||
Console.WriteLine("c = Start QoS 0 benchmark");
|
||||
Console.WriteLine("d = Start server with logging");
|
||||
Console.WriteLine("e = Start Message Throughput Test");
|
||||
Console.WriteLine("f = Start AsyncLock Test");
|
||||
|
||||
var pressedKey = Console.ReadKey(true);
|
||||
if (pressedKey.KeyChar == '1')
|
||||
{
|
||||
Console.WriteLine($"MQTTnet - TestApp.{TargetFrameworkProvider.TargetFramework}");
|
||||
Console.WriteLine("1 = Start client");
|
||||
Console.WriteLine("2 = Start server");
|
||||
Console.WriteLine("3 = Start performance test");
|
||||
Console.WriteLine("4 = Start managed client");
|
||||
Console.WriteLine("5 = Start public broker test");
|
||||
Console.WriteLine("6 = Start server & client");
|
||||
Console.WriteLine("7 = Client flow test");
|
||||
Console.WriteLine("8 = Start performance test (client only)");
|
||||
Console.WriteLine("9 = Start server (no trace)");
|
||||
Console.WriteLine("a = Start QoS 2 benchmark");
|
||||
Console.WriteLine("b = Start QoS 1 benchmark");
|
||||
Console.WriteLine("c = Start QoS 0 benchmark");
|
||||
Console.WriteLine("d = Start server with logging");
|
||||
Console.WriteLine("e = Start Message Throughput Test");
|
||||
Console.WriteLine("f = Start AsyncLock Test");
|
||||
|
||||
var pressedKey = Console.ReadKey(true);
|
||||
if (pressedKey.KeyChar == '1')
|
||||
{
|
||||
Task.Run(ClientTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '2')
|
||||
{
|
||||
Task.Run(ServerTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '3')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunClientAndServer);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '4')
|
||||
{
|
||||
Task.Run(ManagedClientTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '5')
|
||||
{
|
||||
Task.Run(PublicBrokerTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '6')
|
||||
{
|
||||
Task.Run(ServerAndClientTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '7')
|
||||
{
|
||||
Task.Run(ClientFlowTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '8')
|
||||
{
|
||||
PerformanceTest.RunClientOnly();
|
||||
return;
|
||||
}
|
||||
else if (pressedKey.KeyChar == '9')
|
||||
{
|
||||
ServerTest.RunEmptyServer();
|
||||
return;
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'a')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunQoS2Test);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'b')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunQoS1Test);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'c')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunQoS0Test);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'd')
|
||||
{
|
||||
Task.Run(ServerTest.RunEmptyServerWithLogging);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'e')
|
||||
{
|
||||
Task.Run(new MessageThroughputTest().Run);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'f')
|
||||
{
|
||||
Task.Run(new AsyncLockTest().Run);
|
||||
}
|
||||
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
Task.Run(ClientTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '2')
|
||||
{
|
||||
Task.Run(ServerTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '3')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunClientAndServer);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '5')
|
||||
{
|
||||
Task.Run(PublicBrokerTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '6')
|
||||
{
|
||||
Task.Run(ServerAndClientTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '7')
|
||||
{
|
||||
Task.Run(ClientFlowTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '8')
|
||||
{
|
||||
PerformanceTest.RunClientOnly();
|
||||
return;
|
||||
}
|
||||
else if (pressedKey.KeyChar == '9')
|
||||
{
|
||||
ServerTest.RunEmptyServer();
|
||||
return;
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'a')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunQoS2Test);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'b')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunQoS1Test);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'c')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunQoS0Test);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'd')
|
||||
{
|
||||
Task.Run(ServerTest.RunEmptyServerWithLogging);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'e')
|
||||
{
|
||||
Task.Run(new MessageThroughputTest().Run);
|
||||
}
|
||||
else if (pressedKey.KeyChar == 'f')
|
||||
{
|
||||
Task.Run(new AsyncLockTest().Run);
|
||||
}
|
||||
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,6 @@ using System;
|
||||
using System.Security.Authentication;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Extensions.WebSocket4Net;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Internal;
|
||||
using MQTTnet.Protocol;
|
||||
@@ -18,7 +16,6 @@ namespace MQTTnet.TestApp
|
||||
{
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
#if NET5_0_OR_GREATER
|
||||
// TLS13 is only available in Net5.0
|
||||
var unsafeTls13 = new MqttClientTlsOptions
|
||||
{
|
||||
@@ -29,7 +26,7 @@ namespace MQTTnet.TestApp
|
||||
IgnoreCertificateChainErrors = true,
|
||||
CertificateValidationHandler = _ => true
|
||||
};
|
||||
#endif
|
||||
|
||||
// Also defining TLS12 for servers that don't seem no to support TLS13.
|
||||
var unsafeTls12 = new MqttClientTlsOptions
|
||||
{
|
||||
@@ -50,16 +47,9 @@ namespace MQTTnet.TestApp
|
||||
"mqtt.eclipseprojects.io WS",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("mqtt.eclipseprojects.io:80/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||
|
||||
#if NET5_0_OR_GREATER
|
||||
await ExecuteTestsAsync("mqtt.eclipseprojects.io WS TLS13",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("mqtt.eclipseprojects.io:443/mqtt"))
|
||||
.WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls13).Build());
|
||||
|
||||
await ExecuteTestsAsync("mqtt.eclipseprojects.io WS TLS13 (WebSocket4Net)",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("mqtt.eclipseprojects.io:443/mqtt"))
|
||||
.WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls13).Build(),
|
||||
true);
|
||||
#endif
|
||||
|
||||
// test.mosquitto.org
|
||||
await ExecuteTestsAsync(
|
||||
@@ -74,11 +64,9 @@ namespace MQTTnet.TestApp
|
||||
"test.mosquitto.org TCP TLS12",
|
||||
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8883).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build());
|
||||
|
||||
#if NET5_0_OR_GREATER
|
||||
await ExecuteTestsAsync("test.mosquitto.org TCP TLS13",
|
||||
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8883)
|
||||
.WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls13).Build());
|
||||
#endif
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"test.mosquitto.org TCP TLS12 - Authenticated",
|
||||
@@ -92,20 +80,10 @@ namespace MQTTnet.TestApp
|
||||
"test.mosquitto.org WS",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("test.mosquitto.org:8080/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"test.mosquitto.org WS (WebSocket4Net)",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("test.mosquitto.org:8080/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).Build(),
|
||||
true);
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"test.mosquitto.org WS TLS12",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("test.mosquitto.org:8081/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build());
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"test.mosquitto.org WS TLS12 (WebSocket4Net)",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("test.mosquitto.org:8081/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build(),
|
||||
true);
|
||||
|
||||
// broker.emqx.io
|
||||
await ExecuteTestsAsync(
|
||||
"broker.emqx.io TCP",
|
||||
@@ -115,30 +93,18 @@ namespace MQTTnet.TestApp
|
||||
"broker.emqx.io TCP TLS12",
|
||||
new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io", 8883).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build());
|
||||
|
||||
#if NET5_0_OR_GREATER
|
||||
await ExecuteTestsAsync("broker.emqx.io TCP TLS13",
|
||||
new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io", 8883)
|
||||
.WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls13).Build());
|
||||
#endif
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"broker.emqx.io WS",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("broker.emqx.io:8083/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"broker.emqx.io WS (WebSocket4Net)",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("broker.emqx.io:8084/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).Build(),
|
||||
true);
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"broker.emqx.io WS TLS12",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("broker.emqx.io:8084/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build());
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"broker.emqx.io WS TLS12 (WebSocket4Net)",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("broker.emqx.io:8084/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).WithTlsOptions(unsafeTls12).Build(),
|
||||
true);
|
||||
|
||||
// broker.hivemq.com
|
||||
await ExecuteTestsAsync(
|
||||
"broker.hivemq.com TCP",
|
||||
@@ -148,11 +114,6 @@ namespace MQTTnet.TestApp
|
||||
"broker.hivemq.com WS",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("broker.hivemq.com:8000/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||
|
||||
await ExecuteTestsAsync(
|
||||
"broker.hivemq.com WS (WebSocket4Net)",
|
||||
new MqttClientOptionsBuilder().WithWebSocketServer(o => o.WithUri("broker.hivemq.com:8000/mqtt")).WithProtocolVersion(MqttProtocolVersion.V311).Build(),
|
||||
true);
|
||||
|
||||
// mqtt.swifitch.cz: Does not seem to operate any more
|
||||
// cloudmqtt.com: Cannot test because it does not offer a free plan any more.
|
||||
|
||||
@@ -160,26 +121,22 @@ namespace MQTTnet.TestApp
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static async Task ExecuteTestsAsync(string name, MqttClientOptions options, bool useWebSocket4Net = false)
|
||||
static async Task ExecuteTestsAsync(string name, MqttClientOptions options)
|
||||
{
|
||||
options.ProtocolVersion = MqttProtocolVersion.V311;
|
||||
await ExecuteTestAsync(name + " V3.1.1", options, useWebSocket4Net);
|
||||
await ExecuteTestAsync(name + " V3.1.1", options);
|
||||
|
||||
options.ProtocolVersion = MqttProtocolVersion.V500;
|
||||
await ExecuteTestAsync(name + " V5.0.0", options, useWebSocket4Net);
|
||||
await ExecuteTestAsync(name + " V5.0.0", options);
|
||||
}
|
||||
|
||||
static async Task ExecuteTestAsync(string name, MqttClientOptions options, bool useWebSocket4Net = false)
|
||||
|
||||
static async Task ExecuteTestAsync(string name, MqttClientOptions options)
|
||||
{
|
||||
try
|
||||
{
|
||||
Write("Testing '" + name + "'... ", ConsoleColor.Gray);
|
||||
|
||||
var factory = new MqttFactory();
|
||||
if (useWebSocket4Net)
|
||||
{
|
||||
factory.UseWebSocket4Net();
|
||||
}
|
||||
var factory = new MqttClientFactory();
|
||||
|
||||
using (var client = factory.CreateMqttClient())
|
||||
{
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet.TestApp
|
||||
@@ -17,9 +16,10 @@ namespace MQTTnet.TestApp
|
||||
var logger = new MqttNetEventLogger();
|
||||
MqttNetConsoleLogger.ForwardToConsole(logger);
|
||||
|
||||
var factory = new MqttFactory(logger);
|
||||
var server = factory.CreateMqttServer( new MqttServerOptionsBuilder().Build());
|
||||
var client = factory.CreateMqttClient();
|
||||
var mqttServerFactory = new MqttServerFactory();
|
||||
var mqttClientFactory = new MqttClientFactory(logger);
|
||||
var server = mqttServerFactory.CreateMqttServer( new MqttServerOptionsBuilder().Build());
|
||||
var client = mqttClientFactory.CreateMqttClient();
|
||||
|
||||
await server.StartAsync();
|
||||
|
||||
|
||||
@@ -5,10 +5,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.Internal;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
@@ -20,9 +19,9 @@ namespace MQTTnet.TestApp
|
||||
{
|
||||
public static void RunEmptyServer()
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(new MqttServerOptions());
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(new MqttServerOptions());
|
||||
mqttServer.StartAsync().GetAwaiter().GetResult();
|
||||
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
@@ -31,15 +30,15 @@ namespace MQTTnet.TestApp
|
||||
{
|
||||
var logger = new MqttNetEventLogger();
|
||||
MqttNetConsoleLogger.ForwardToConsole(logger);
|
||||
|
||||
var mqttFactory = new MqttFactory(logger);
|
||||
var mqttServer = mqttFactory.CreateMqttServer(new MqttServerOptions());
|
||||
|
||||
var mqttServerFactory = new MqttServerFactory(logger);
|
||||
var mqttServer = mqttServerFactory.CreateMqttServer(new MqttServerOptions());
|
||||
mqttServer.StartAsync().GetAwaiter().GetResult();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
try
|
||||
@@ -55,10 +54,10 @@ namespace MQTTnet.TestApp
|
||||
//options.DefaultEndpointOptions.IsEnabled = true;
|
||||
//options.TlsEndpointOptions.IsEnabled = false;
|
||||
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(options);
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
||||
|
||||
const string Filename = "C:\\MQTT\\RetainedMessages.json";
|
||||
|
||||
|
||||
mqttServer.RetainedMessageChangedAsync += e =>
|
||||
{
|
||||
var directory = Path.GetDirectoryName(Filename);
|
||||
@@ -70,13 +69,13 @@ namespace MQTTnet.TestApp
|
||||
File.WriteAllText(Filename, JsonConvert.SerializeObject(e.StoredRetainedMessages));
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
|
||||
mqttServer.RetainedMessagesClearedAsync += e =>
|
||||
{
|
||||
File.Delete(Filename);
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
|
||||
mqttServer.LoadingRetainedMessageAsync += e =>
|
||||
{
|
||||
List<MqttApplicationMessage> retainedMessages;
|
||||
@@ -99,7 +98,7 @@ namespace MQTTnet.TestApp
|
||||
{
|
||||
if (MqttTopicFilterComparer.Compare(e.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#") == MqttTopicFilterCompareResult.IsMatch)
|
||||
{
|
||||
// Replace the payload with the timestamp. But also extending a JSON
|
||||
// Replace the payload with the timestamp. But also extending a JSON
|
||||
// based payload with the timestamp is a suitable use case.
|
||||
e.ApplicationMessage.PayloadSegment = new ArraySegment<byte>(Encoding.UTF8.GetBytes(DateTime.Now.ToString("O")));
|
||||
}
|
||||
@@ -112,7 +111,7 @@ namespace MQTTnet.TestApp
|
||||
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
|
||||
mqttServer.ValidatingConnectionAsync += e =>
|
||||
{
|
||||
if (e.ClientId == "SpecialClient")
|
||||
@@ -141,18 +140,15 @@ namespace MQTTnet.TestApp
|
||||
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
|
||||
mqttServer.InterceptingPublishAsync += e =>
|
||||
{
|
||||
var payloadText = string.Empty;
|
||||
if (e.ApplicationMessage.PayloadSegment.Count > 0)
|
||||
if (e.ApplicationMessage.Payload.Length > 0)
|
||||
{
|
||||
payloadText = Encoding.UTF8.GetString(
|
||||
e.ApplicationMessage.PayloadSegment.Array,
|
||||
e.ApplicationMessage.PayloadSegment.Offset,
|
||||
e.ApplicationMessage.PayloadSegment.Count);
|
||||
payloadText = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||
}
|
||||
|
||||
|
||||
MqttNetConsoleLogger.PrintToConsole($"'{e.ClientId}' reported '{e.ApplicationMessage.Topic}' > '{payloadText}'", ConsoleColor.Magenta);
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user