mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-21 04:03:05 +08:00
Refactoring
This commit is contained in:
@@ -55,7 +55,7 @@ namespace MQTTnet.Core.Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var r = await Task.Run(() =>
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var iis = new int[0];
|
||||
return iis[1];
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Protocol;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class MqttApplicationMessageFactoryTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void CreateApplicationMessage_TopicOnly()
|
||||
{
|
||||
var message = new MqttApplicationMessageFactory().CreateApplicationMessage("Abc", MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
Assert.AreEqual("Abc", message.Topic);
|
||||
Assert.IsFalse(message.Retain);
|
||||
Assert.AreEqual(MqttQualityOfServiceLevel.AtLeastOnce, message.QualityOfServiceLevel);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CreateApplicationMessage_TimeStampPayload()
|
||||
{
|
||||
var message = new MqttApplicationMessageFactory().CreateApplicationMessage("xyz", TimeSpan.FromSeconds(360));
|
||||
Assert.AreEqual("xyz", message.Topic);
|
||||
Assert.IsFalse(message.Retain);
|
||||
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
|
||||
Assert.AreEqual(Encoding.UTF8.GetString(message.Payload), "00:06:00");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ namespace MQTTnet.Core.Tests
|
||||
_adapter = adapter;
|
||||
}
|
||||
|
||||
public IMqttCommunicationAdapter CreateMqttCommunicationAdapter(MqttClientOptions options)
|
||||
public IMqttCommunicationAdapter CreateMqttCommunicationAdapter(IMqttClientOptions options)
|
||||
{
|
||||
return _adapter;
|
||||
}
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Adapter;
|
||||
using MQTTnet.Core.Channel;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Serializer;
|
||||
@@ -387,45 +385,6 @@ namespace MQTTnet.Core.Tests
|
||||
DeserializeAndCompare(p, "sAIAew==");
|
||||
}
|
||||
|
||||
|
||||
public class TestChannel : IMqttCommunicationChannel
|
||||
{
|
||||
private readonly MemoryStream _stream = new MemoryStream();
|
||||
|
||||
public Stream ReceiveStream => _stream;
|
||||
|
||||
public Stream RawReceiveStream => _stream;
|
||||
|
||||
public Stream SendStream => _stream;
|
||||
|
||||
public bool IsConnected { get; } = true;
|
||||
|
||||
public TestChannel()
|
||||
{
|
||||
}
|
||||
|
||||
public TestChannel(byte[] initialData)
|
||||
{
|
||||
_stream.Write(initialData, 0, initialData.Length);
|
||||
_stream.Position = 0;
|
||||
}
|
||||
|
||||
public Task ConnectAsync()
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task DisconnectAsync()
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
return _stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static void SerializeAndCompare(MqttBasePacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
|
||||
{
|
||||
var serializer = new MqttPacketSerializer { ProtocolVersion = protocolVersion };
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
<TargetFrameworks>netcoreapp2.0;net45</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Frameworks\MQTTnet.Netstandard\MQTTnet.NetStandard.csproj" />
|
||||
<ProjectReference Include="..\..\MQTTnet.Core\MQTTnet.Core.csproj" />
|
||||
|
||||
@@ -113,7 +113,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
.Select(i => CreateMessage())
|
||||
.ToList();
|
||||
|
||||
if (true)
|
||||
Console.WriteLine("Press 'c' for concurrent sends. Otherwise in one batch.");
|
||||
if (Console.ReadKey().KeyChar == 'c')
|
||||
{
|
||||
//send concurrent (test for raceconditions)
|
||||
var sendTasks = msgs
|
||||
@@ -161,10 +162,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
private static Task PublishSingleMessage(IMqttClient client, MqttApplicationMessage applicationMessage, ref int count)
|
||||
{
|
||||
Interlocked.Increment(ref count);
|
||||
return Task.Run(() =>
|
||||
{
|
||||
return client.PublishAsync(applicationMessage);
|
||||
});
|
||||
return Task.Run(() => client.PublishAsync(applicationMessage));
|
||||
}
|
||||
|
||||
private static async Task RunServerAsync()
|
||||
|
||||
@@ -6,16 +6,18 @@ using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
public static void Main()
|
||||
{
|
||||
Console.WriteLine("MQTTnet - TestApp.NetFramework");
|
||||
Console.WriteLine("1 = Start client");
|
||||
@@ -24,12 +26,12 @@ namespace MQTTnet.TestApp.NetCore
|
||||
var pressedKey = Console.ReadKey(true);
|
||||
if (pressedKey.Key == ConsoleKey.D1)
|
||||
{
|
||||
Task.Run(() => RunClientAsync(args));
|
||||
Task.Run(RunClientAsync);
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
else if (pressedKey.Key == ConsoleKey.D2)
|
||||
{
|
||||
Task.Run(() => RunServerAsync(args));
|
||||
Task.Run(() => RunServerAsync());
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
else if (pressedKey.Key == ConsoleKey.D3)
|
||||
@@ -39,7 +41,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task RunClientAsync(string[] arguments)
|
||||
private static async Task RunClientAsync()
|
||||
{
|
||||
MqttNetTrace.TraceMessagePublished += (s, e) =>
|
||||
{
|
||||
@@ -128,7 +130,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
}
|
||||
|
||||
private static void RunServerAsync(string[] arguments)
|
||||
private static Task RunServerAsync()
|
||||
{
|
||||
MqttNetTrace.TraceMessagePublished += (s, e) =>
|
||||
{
|
||||
@@ -157,10 +159,21 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
};
|
||||
|
||||
options.Storage = new RetainedMessageHandler();
|
||||
|
||||
|
||||
var certificate = new X509Certificate(@"C:\certs\test\test.cer", "");
|
||||
options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert);
|
||||
options.ConnectionBacklog = 5;
|
||||
options.DefaultEndpointOptions.IsEnabled = true;
|
||||
options.TlsEndpointOptions.IsEnabled = false;
|
||||
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
||||
mqttServer.ClientDisconnected += (s, e) =>
|
||||
{
|
||||
Console.Write("Client disconnected event fired.");
|
||||
};
|
||||
|
||||
mqttServer.StartAsync();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
@@ -174,6 +187,34 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
|
||||
Console.ReadLine();
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class RetainedMessageHandler : IMqttServerStorage
|
||||
{
|
||||
private const string Filename = "C:\\MQTT\\RetainedMessages.json";
|
||||
|
||||
public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
|
||||
{
|
||||
File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
|
||||
{
|
||||
IList<MqttApplicationMessage> retainedMessages;
|
||||
if (File.Exists(Filename))
|
||||
{
|
||||
var json = File.ReadAllText(Filename);
|
||||
retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
retainedMessages = new List<MqttApplicationMessage>();
|
||||
}
|
||||
|
||||
return Task.FromResult(retainedMessages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.ApplicationModel;
|
||||
using Windows.ApplicationModel.Activation;
|
||||
using Windows.Foundation;
|
||||
using Windows.Foundation.Collections;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Controls.Primitives;
|
||||
using Windows.UI.Xaml.Data;
|
||||
using Windows.UI.Xaml.Input;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Navigation;
|
||||
|
||||
namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides application-specific behavior to supplement the default Application class.
|
||||
/// </summary>
|
||||
sealed partial class App : Application
|
||||
sealed partial class App
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the singleton application object. This is the first line of authored code
|
||||
/// executed, and as such is the logical equivalent of main() or WinMain().
|
||||
/// </summary>
|
||||
public App()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
this.Suspending += OnSuspending;
|
||||
InitializeComponent();
|
||||
Suspending += OnSuspending;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -39,11 +22,9 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
/// <param name="e">Details about the launch request and process.</param>
|
||||
protected override void OnLaunched(LaunchActivatedEventArgs e)
|
||||
{
|
||||
Frame rootFrame = Window.Current.Content as Frame;
|
||||
|
||||
// Do not repeat app initialization when the Window already has content,
|
||||
// just ensure that the window is active
|
||||
if (rootFrame == null)
|
||||
if (!(Window.Current.Content is Frame rootFrame))
|
||||
{
|
||||
// Create a Frame to act as the navigation context and navigate to the first page
|
||||
rootFrame = new Frame();
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
|
||||
<TextBlock>QoS:</TextBlock>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<RadioButton Margin="0,0,10,0" x:Name="QoS0" IsChecked="True" GroupName="qos">0 (At most once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" IsChecked="True" GroupName="qos">0 (At most once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="QoS1" GroupName="qos">1 (At least once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="QoS2" GroupName="qos">2 (Exactly once)</RadioButton>
|
||||
</StackPanel>
|
||||
@@ -73,7 +73,7 @@
|
||||
|
||||
<TextBlock>QoS:</TextBlock>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<RadioButton Margin="0,0,10,0" x:Name="SubscribeQoS0" IsChecked="True" GroupName="sqos">0 (At most once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" IsChecked="True" GroupName="sqos">0 (At most once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="SubscribeQoS1" GroupName="sqos">1 (At least once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="SubscribeQoS2" GroupName="sqos">2 (Exactly once)</RadioButton>
|
||||
</StackPanel>
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
|
||||
private async void Connect(object sender, RoutedEventArgs e)
|
||||
{
|
||||
MqttClientOptions options = null;
|
||||
BaseMqttClientOptions options = null;
|
||||
if (UseTcp.IsChecked == true)
|
||||
{
|
||||
options = new MqttClientTcpOptions
|
||||
@@ -195,6 +195,8 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
}
|
||||
}
|
||||
|
||||
// This code is for the Wiki at GitHub!
|
||||
// ReSharper disable once UnusedMember.Local
|
||||
private async Task WikiCode()
|
||||
{
|
||||
var mqttClient = new MqttClientFactory().CreateMqttClient();
|
||||
|
||||
Reference in New Issue
Block a user