Skip to content

Commit fd220fa

Browse files
authored
Refactoring, Examples and Warning cleanup (#95)
1 parent 83b4560 commit fd220fa

File tree

16 files changed

+108
-40
lines changed

16 files changed

+108
-40
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ csharp_style_unused_value_expression_statement_preference = discard_variable:sug
186186
dotnet_diagnostic.IDE0058.severity = suggestion
187187
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
188188
dotnet_diagnostic.IDE0059.severity = suggestion
189+
dotnet_diagnostic.IDE0002.severity = none
190+
dotnet_diagnostic.IDE0010.severity = none
191+
dotnet_diagnostic.IDE0021.severity = none
192+
dotnet_diagnostic.IDE0028.severity = none
193+
dotnet_diagnostic.IDE0049.severity = none
194+
dotnet_diagnostic.IDE0053.severity = none
195+
dotnet_diagnostic.IDE0090.severity = none
196+
dotnet_diagnostic.IDE0290.severity = none
197+
dotnet_diagnostic.SA1508.severity = none
189198

190199
##########################################
191200
# Formatting Rules

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
],
55
"cSpell.words": [
66
"CONNACK",
7+
"hivemq",
8+
"hivemqtt",
79
"PUBACK",
810
"PUBCOMP",
911
"PUBREC",

Examples/HiveMQtt-CLI/HiveMQtt-CLI/HiveMQtt-CLI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net6.0</TargetFramework>
66
<RootNamespace>HiveMQtt_CLI</RootNamespace>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<Nullable>enable</Nullable>

Examples/Reconnect/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"preLaunchTask": "build",
1212
// If you have changed target frameworks, make sure to update the program path.
13-
"program": "${workspaceFolder}/bin/Debug/net7.0/Reconnect.dll",
13+
"program": "${workspaceFolder}/bin/Debug/net6.0/Reconnect.dll",
1414
"args": [],
1515
"cwd": "${workspaceFolder}",
1616
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console

Examples/Reconnect/NLog.config

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
5+
<!-- optional, add some variables
6+
https://github.com/nlog/NLog/wiki/Configuration-file#variables
7+
-->
8+
<variable name="myvar" value="myvalue"/>
9+
10+
<!--
11+
See https://github.com/nlog/nlog/wiki/Configuration-file
12+
for information on customizing logging rules and outputs.
13+
-->
14+
<targets>
15+
16+
<!--
17+
add your targets here
18+
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
19+
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
20+
-->
21+
22+
<!--
23+
Write events to a file with the date in the filename.
24+
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
25+
layout="${longdate} ${uppercase:${level}} ${message}" />
26+
-->
27+
<target name="logfile" xsi:type="File" fileName="Reconnect.log" />
28+
<target name="logconsole" xsi:type="Console" />
29+
</targets>
30+
31+
<rules>
32+
<!-- add your logging rules here -->
33+
34+
<!--
35+
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
36+
<logger name="*" minlevel="Debug" writeTo="f" />
37+
-->
38+
<logger name="HiveMQtt.*" minlevel="Trace" writeTo="logconsole" />
39+
</rules>
40+
</nlog>

Examples/Reconnect/Program.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
using HiveMQtt.Client;
2+
using HiveMQtt.Client.Exceptions;
23
using HiveMQtt.Client.Options;
34
using System.Text.Json;
5+
using System.Diagnostics;
46

57
var topic = "hivemqtt/waiting/game";
68

7-
var options = new HiveMQClientOptions();
8-
options.Host = "127.0.0.1";
9-
options.Port = 1883;
9+
var options = new HiveMQClientOptions
10+
{
11+
Host = "127.0.0.1",
12+
Port = 1883,
13+
};
1014

1115
var client = new HiveMQClient(options);
1216

@@ -74,12 +78,12 @@
7478
break;
7579
}
7680
}
77-
catch (Exception ex)
81+
catch (HiveMQttClientException ex)
7882
{
79-
Console.WriteLine($"--> Failed to connect: {ex.Message}");
83+
Console.WriteLine($"--> Failed to reconnect: {ex.Message}");
8084

8185
// Double the delay with each failed retry to a maximum
82-
delay = Math.Min(delay * 2, 10000);
86+
delay = Math.Min(delay * 2, 15000);
8387
Console.WriteLine($"--> Will delay for {delay / 1000} seconds until next try.");
8488
}
8589
}
@@ -123,7 +127,6 @@
123127
})
124128
).ConfigureAwait(false);
125129

126-
127130
while (true)
128131
{
129132
await Task.Delay(2000).ConfigureAwait(false);

Examples/Reconnect/Reconnect.csproj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net6.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
@@ -13,8 +13,16 @@
1313
</PropertyGroup>
1414

1515
<!-- Update the version to match -->
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<WarningLevel>4</WarningLevel>
18+
</PropertyGroup>
1619
<ItemGroup>
17-
<PackageReference Include="HiveMQtt" Version="0.4.1" />
20+
<PackageReference Include="HiveMQtt" Version="0.4.2" />
1821
</ItemGroup>
1922

23+
<ItemGroup>
24+
<None Update="NLog.config">
25+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26+
</None>
27+
</ItemGroup>
2028
</Project>

Source/HiveMQtt/Client/HiveMQClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public HiveMQClient(HiveMQClientOptions? options = null)
4242
{
4343
options ??= new HiveMQClientOptions();
4444
this.Options = options;
45+
this.cancellationSource = new CancellationTokenSource();
4546
}
4647

4748
/// <inheritdoc />

Source/HiveMQtt/Client/HiveMQClientEvents.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
namespace HiveMQtt.Client;
1717

1818
using System;
19-
using System.Diagnostics;
2019
using HiveMQtt.Client.Events;
2120
using HiveMQtt.Client.Options;
2221
using HiveMQtt.Client.Results;

Source/HiveMQtt/Client/HiveMQClientSocket.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ namespace HiveMQtt.Client;
2929
/// <inheritdoc />
3030
public partial class HiveMQClient : IDisposable, IHiveMQClient
3131
{
32+
private readonly CancellationTokenSource cancellationSource;
3233
private Socket? socket;
3334
private Stream? stream;
3435
private PipeReader? reader;
3536
private PipeWriter? writer;
36-
private CancellationTokenSource cancellationSource;
3737
private CancellationToken outFlowCancellationToken;
3838
private CancellationToken infoFlowCancellationToken;
3939

@@ -131,7 +131,6 @@ internal async Task<bool> ConnectSocketAsync()
131131
this.writer = PipeWriter.Create(this.stream);
132132

133133
// Setup the cancellation tokens
134-
this.cancellationSource = new CancellationTokenSource();
135134
this.outFlowCancellationToken = this.cancellationSource.Token;
136135
this.infoFlowCancellationToken = this.cancellationSource.Token;
137136

0 commit comments

Comments
 (0)