Skip to content

Use latest language features #2176

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions MQTTnet.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConvertIfStatementToSwitchStatement/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=InconsistentNaming/@EntryIndexedValue">SUGGESTION</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UncatchableException/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnthrowableException/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/BRACES_FOR_FOR/@EntryValue">Required</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/BRACES_FOR_FOREACH/@EntryValue">Required</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/BRACES_FOR_IFELSE/@EntryValue">Required</s:String>
Expand Down
72 changes: 34 additions & 38 deletions Samples/Client/Client_Publish_Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,23 @@ public static async Task Publish_Application_Message()

var mqttFactory = new MqttClientFactory();

using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("broker.hivemq.com")
.Build();
using var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("broker.hivemq.com")
.Build();

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

var applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic("samples/temperature/living_room")
.WithPayload("19.5")
.Build();
var applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic("samples/temperature/living_room")
.WithPayload("19.5")
.Build();

await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);

await mqttClient.DisconnectAsync();
await mqttClient.DisconnectAsync();

Console.WriteLine("MQTT application message is published.");
}
Console.WriteLine("MQTT application message is published.");
}

public static async Task Publish_Multiple_Application_Messages()
Expand All @@ -54,38 +52,36 @@ public static async Task Publish_Multiple_Application_Messages()

var mqttFactory = new MqttClientFactory();

using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("broker.hivemq.com")
.Build();
using var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("broker.hivemq.com")
.Build();

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

var applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic("samples/temperature/living_room")
.WithPayload("19.5")
.Build();
var applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic("samples/temperature/living_room")
.WithPayload("19.5")
.Build();

await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);

applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic("samples/temperature/living_room")
.WithPayload("20.0")
.Build();
applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic("samples/temperature/living_room")
.WithPayload("20.0")
.Build();

await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);

applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic("samples/temperature/living_room")
.WithPayload("21.0")
.Build();
applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic("samples/temperature/living_room")
.WithPayload("21.0")
.Build();

await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);

await mqttClient.DisconnectAsync();
await mqttClient.DisconnectAsync();

Console.WriteLine("MQTT application message is published.");
}
Console.WriteLine("MQTT application message is published.");
}
}
122 changes: 57 additions & 65 deletions Samples/Client/Client_Subscribe_Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,30 @@ public static async Task Handle_Received_Application_Message()

var mqttFactory = new MqttClientFactory();

using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
using var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();

// Setup message handling before connecting so that queued messages
// are also handled properly. When there is no event handler attached all
// received messages get lost.
mqttClient.ApplicationMessageReceivedAsync += e =>
{
Console.WriteLine("Received application message.");
e.DumpToConsole();
// Setup message handling before connecting so that queued messages
// are also handled properly. When there is no event handler attached all
// received messages get lost.
mqttClient.ApplicationMessageReceivedAsync += e =>
{
Console.WriteLine("Received application message.");
e.DumpToConsole();

return Task.CompletedTask;
};
return Task.CompletedTask;
};

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "2")).Build();
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "2")).Build();

await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);

Console.WriteLine("MQTT client subscribed to topic.");
Console.WriteLine("MQTT client subscribed to topic.");

Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}

public static async Task Send_Responses()
Expand All @@ -62,36 +60,34 @@ public static async Task Send_Responses()

var mqttFactory = new MqttClientFactory();

using (var mqttClient = mqttFactory.CreateMqttClient())
using var mqttClient = mqttFactory.CreateMqttClient();
mqttClient.ApplicationMessageReceivedAsync += delegate(MqttApplicationMessageReceivedEventArgs args)
{
mqttClient.ApplicationMessageReceivedAsync += delegate(MqttApplicationMessageReceivedEventArgs args)
{
// Do some work with the message...
// Do some work with the message...

// Now respond to the broker with a reason code other than success.
args.ReasonCode = MqttApplicationMessageReceivedReasonCode.ImplementationSpecificError;
args.ResponseReasonString = "That did not work!";
// Now respond to the broker with a reason code other than success.
args.ReasonCode = MqttApplicationMessageReceivedReasonCode.ImplementationSpecificError;
args.ResponseReasonString = "That did not work!";

// User properties require MQTT v5!
args.ResponseUserProperties.Add(new MqttUserProperty("My", "Data"));
// User properties require MQTT v5!
args.ResponseUserProperties.Add(new MqttUserProperty("My", "Data"));

// Now the broker will resend the message again.
return Task.CompletedTask;
};
// Now the broker will resend the message again.
return Task.CompletedTask;
};

var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "1")).Build();
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "1")).Build();

var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);

Console.WriteLine("MQTT client subscribed to topic.");
Console.WriteLine("MQTT client subscribed to topic.");

// The response contains additional data sent by the server after subscribing.
response.DumpToConsole();
}
// The response contains additional data sent by the server after subscribing.
response.DumpToConsole();
}

public static async Task Subscribe_Multiple_Topics()
Expand All @@ -102,27 +98,25 @@ public static async Task Subscribe_Multiple_Topics()

var mqttFactory = new MqttClientFactory();

using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
using var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

// Create the subscribe options including several topics with different options.
// It is also possible to all of these topics using a dedicated call of _SubscribeAsync_ per topic.
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
.WithTopicTemplate(sampleTemplate.WithParameter("id", "1"))
.WithTopicTemplate(sampleTemplate.WithParameter("id", "2"), noLocal: true)
.WithTopicTemplate(sampleTemplate.WithParameter("id", "3"), retainHandling: MqttRetainHandling.SendAtSubscribe)
.Build();
// Create the subscribe options including several topics with different options.
// It is also possible to all of these topics using a dedicated call of _SubscribeAsync_ per topic.
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
.WithTopicTemplate(sampleTemplate.WithParameter("id", "1"))
.WithTopicTemplate(sampleTemplate.WithParameter("id", "2"), noLocal: true)
.WithTopicTemplate(sampleTemplate.WithParameter("id", "3"), retainHandling: MqttRetainHandling.SendAtSubscribe)
.Build();

var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);

Console.WriteLine("MQTT client subscribed to topics.");
Console.WriteLine("MQTT client subscribed to topics.");

// The response contains additional data sent by the server after subscribing.
response.DumpToConsole();
}
// The response contains additional data sent by the server after subscribing.
response.DumpToConsole();
}

public static async Task Subscribe_Topic()
Expand All @@ -133,21 +127,19 @@ public static async Task Subscribe_Topic()

var mqttFactory = new MqttClientFactory();

using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
using var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "1")).Build();
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "1")).Build();

var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);

Console.WriteLine("MQTT client subscribed to topic.");
Console.WriteLine("MQTT client subscribed to topic.");

// The response contains additional data sent by the server after subscribing.
response.DumpToConsole();
}
// The response contains additional data sent by the server after subscribing.
response.DumpToConsole();
}

static void ConcurrentProcessingDisableAutoAcknowledge(CancellationToken shutdownToken, IMqttClient mqttClient)
Expand Down
30 changes: 13 additions & 17 deletions Samples/Diagnostics/Logger_Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ public static async Task Create_Custom_Logger()
.WithTcpServer("broker.hivemq.com")
.Build();

using (var mqttClient = mqttFactory.CreateMqttClient())
{
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
using var mqttClient = mqttFactory.CreateMqttClient();
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

Console.WriteLine("MQTT client is connected.");
Console.WriteLine("MQTT client is connected.");

var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder()
.Build();
var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder()
.Build();

await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
}
await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
}

public static async Task Use_Event_Logger()
Expand All @@ -50,7 +48,7 @@ public static async Task Use_Event_Logger()
// The logger ID is optional but can be set do distinguish different logger instances.
var mqttEventLogger = new MqttNetEventLogger("MyCustomLogger");

mqttEventLogger.LogMessagePublished += (sender, args) =>
mqttEventLogger.LogMessagePublished += (_, args) =>
{
var output = new StringBuilder();
output.AppendLine($">> [{args.LogMessage.Timestamp:O}] [{args.LogMessage.ThreadId}] [{args.LogMessage.Source}] [{args.LogMessage.Level}]: {args.LogMessage.Message}");
Expand All @@ -68,17 +66,15 @@ public static async Task Use_Event_Logger()
.WithTcpServer("broker.hivemq.com")
.Build();

using (var mqttClient = mqttFactory.CreateMqttClient())
{
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
using var mqttClient = mqttFactory.CreateMqttClient();
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

Console.WriteLine("MQTT client is connected.");
Console.WriteLine("MQTT client is connected.");

var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder()
.Build();
var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder()
.Build();

await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
}
await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
}

sealed class MyLogger : IMqttNetLogger
Expand Down
22 changes: 10 additions & 12 deletions Samples/Diagnostics/PackageInspection_Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,21 @@ public static async Task Inspect_Outgoing_Package()

var mqttFactory = new MqttClientFactory();

using (var mqttClient = mqttFactory.CreateMqttClient())
{
var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder()
.WithTcpServer("broker.hivemq.com")
.Build();
using var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder()
.WithTcpServer("broker.hivemq.com")
.Build();

mqttClient.InspectPacketAsync += OnInspectPacket;
mqttClient.InspectPacketAsync += OnInspectPacket;

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

Console.WriteLine("MQTT client is connected.");
Console.WriteLine("MQTT client is connected.");

var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder()
.Build();
var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder()
.Build();

await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
}
await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None);
}

static Task OnInspectPacket(InspectMqttPacketEventArgs eventArgs)
Expand Down
Loading
Loading