From 32cc1ff7b151dd1b5800b91fcd158c1de61aaec9 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat, 26 Nov 2022 14:49:28 +0100 Subject: [PATCH] Refactor code and update release notes --- .github/workflows/ReleaseNotes.md | 1 + MQTTnet.sln.DotSettings | 1 + .../Server/Internal/MqttClientStatistics.cs | 60 ++++++++++--------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index c041e0d0..7166dbea 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1 +1,2 @@ [Core] Fixed a memory leak in _AsyncSignal_ implementation (#1586, thanks to @mario-zelger). +[Server] Fix not properly reset statistics (#1587, thanks to @damikun). diff --git a/MQTTnet.sln.DotSettings b/MQTTnet.sln.DotSettings index 83eed4bd..2dc673c8 100644 --- a/MQTTnet.sln.DotSettings +++ b/MQTTnet.sln.DotSettings @@ -240,6 +240,7 @@ See the LICENSE file in the project root for more information. True True True + True True True True diff --git a/Source/MQTTnet/Server/Internal/MqttClientStatistics.cs b/Source/MQTTnet/Server/Internal/MqttClientStatistics.cs index 52718cce..28f8e9cf 100644 --- a/Source/MQTTnet/Server/Internal/MqttClientStatistics.cs +++ b/Source/MQTTnet/Server/Internal/MqttClientStatistics.cs @@ -10,15 +10,15 @@ namespace MQTTnet.Server { public sealed class MqttClientStatistics { + long _receivedApplicationMessagesCount; + // Start with 1 because the CONNACK packet is not counted here. long _receivedPacketsCount = 1; + long _sentApplicationMessagesCount; // Start with 1 because the CONNECT packet is not counted here. long _sentPacketsCount = 1; - long _receivedApplicationMessagesCount; - long _sentApplicationMessagesCount; - public MqttClientStatistics() { ConnectedTimestamp = DateTime.UtcNow; @@ -28,36 +28,36 @@ namespace MQTTnet.Server LastNonKeepAlivePacketReceivedTimestamp = ConnectedTimestamp; } - + public DateTime ConnectedTimestamp { get; } - /// - /// Timestamp of the last package that has been sent to the client ("received" from the client's perspective) - /// - public DateTime LastPacketReceivedTimestamp { get; private set; } - - /// - /// Timestamp of the last package that has been received from the client ("sent" from the client's perspective) - /// - public DateTime LastPacketSentTimestamp { get; private set; } - public DateTime LastNonKeepAlivePacketReceivedTimestamp { get; private set; } - public long SentApplicationMessagesCount => Interlocked.Read(ref _sentApplicationMessagesCount); - + /// + /// Timestamp of the last package that has been sent to the client ("received" from the client's perspective) + /// + public DateTime LastPacketReceivedTimestamp { get; private set; } + + /// + /// Timestamp of the last package that has been received from the client ("sent" from the client's perspective) + /// + public DateTime LastPacketSentTimestamp { get; private set; } + public long ReceivedApplicationMessagesCount => Interlocked.Read(ref _receivedApplicationMessagesCount); - - public long SentPacketsCount => Interlocked.Read(ref _sentPacketsCount); - + public long ReceivedPacketsCount => Interlocked.Read(ref _receivedPacketsCount); - + + public long SentApplicationMessagesCount => Interlocked.Read(ref _sentApplicationMessagesCount); + + public long SentPacketsCount => Interlocked.Read(ref _sentPacketsCount); + public void HandleReceivedPacket(MqttPacket packet) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } - + // This class is tracking all values from Clients perspective! LastPacketSentTimestamp = DateTime.UtcNow; @@ -73,20 +73,14 @@ namespace MQTTnet.Server LastNonKeepAlivePacketReceivedTimestamp = LastPacketReceivedTimestamp; } } - public void ResetStatistics(){ - Interlocked.Exchange(ref _sentApplicationMessagesCount,0); - Interlocked.Exchange(ref _receivedApplicationMessagesCount,0); - Interlocked.Exchange(ref _sentPacketsCount,0); - Interlocked.Exchange(ref _receivedPacketsCount,0); - } - + public void HandleSentPacket(MqttPacket packet) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } - + // This class is tracking all values from Clients perspective! LastPacketReceivedTimestamp = DateTime.UtcNow; @@ -97,5 +91,13 @@ namespace MQTTnet.Server Interlocked.Increment(ref _receivedApplicationMessagesCount); } } + + public void ResetStatistics() + { + Interlocked.Exchange(ref _sentApplicationMessagesCount, 0); + Interlocked.Exchange(ref _receivedApplicationMessagesCount, 0); + Interlocked.Exchange(ref _sentPacketsCount, 0); + Interlocked.Exchange(ref _receivedPacketsCount, 0); + } } } \ No newline at end of file