From 1e68c2e8153e91af030451aa1b2c3a6deba5e482 Mon Sep 17 00:00:00 2001 From: logicaloud <47704763+logicaloud@users.noreply.github.com> Date: Sat, 30 Oct 2021 21:12:57 +1300 Subject: [PATCH] Fixes #1276 timestamp check in server keep-alive monitor (#1277) * Fix timestamp check in server keep-alive monitor * Retain the original client statistics property name - add comments to clarify meaning - add separate public "received" timestamp property * fix comment --- .../MqttClientConnectionStatistics.cs | 11 ++++++- .../Internal/MqttServerKeepAliveMonitor.cs | 4 +-- .../Client/MqttClient_Tests.cs | 33 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Source/MQTTnet/Server/Internal/MqttClientConnectionStatistics.cs b/Source/MQTTnet/Server/Internal/MqttClientConnectionStatistics.cs index 14923f1b..c9c16d1a 100644 --- a/Source/MQTTnet/Server/Internal/MqttClientConnectionStatistics.cs +++ b/Source/MQTTnet/Server/Internal/MqttClientConnectionStatistics.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading; using MQTTnet.Packets; using MQTTnet.Server.Status; @@ -32,8 +32,17 @@ namespace MQTTnet.Server.Internal _lastNonKeepAlivePacketReceivedTimestamp = _connectedTimestamp; } + /// + /// Timestamp of the last package that has been received from the client ("sent" from the client's perspective) + /// + public DateTime LastPacketSentTimestamp => _lastPacketSentTimestamp; + + /// + /// Timestamp of the last package that has been sent to the client ("received" from the client's perspective) + /// public DateTime LastPacketReceivedTimestamp => _lastPacketReceivedTimestamp; + public void HandleReceivedPacket(MqttBasePacket packet) { if (packet == null) throw new ArgumentNullException(nameof(packet)); diff --git a/Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs b/Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs index abcc1201..0eb073c0 100644 --- a/Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs +++ b/Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading; using System.Threading.Tasks; using MQTTnet.Client.Disconnecting; @@ -95,7 +95,7 @@ namespace MQTTnet.Server.Internal // If the client sends 1 sec. the server will allow up to 1.5 seconds. var maxDurationWithoutPacket = connection.KeepAlivePeriod * 1.5D; - var secondsWithoutPackage = (now - connection.Statistics.LastPacketReceivedTimestamp).TotalSeconds; + var secondsWithoutPackage = (now - connection.Statistics.LastPacketSentTimestamp).TotalSeconds; if (secondsWithoutPackage < maxDurationWithoutPacket) { // A packet was received before the timeout is affected. diff --git a/Tests/MQTTnet.Core.Tests/Client/MqttClient_Tests.cs b/Tests/MQTTnet.Core.Tests/Client/MqttClient_Tests.cs index 0e188f06..b6f32753 100644 --- a/Tests/MQTTnet.Core.Tests/Client/MqttClient_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/Client/MqttClient_Tests.cs @@ -647,6 +647,39 @@ namespace MQTTnet.Tests.Client } } + [TestMethod] + public async Task Publish_QoS_0_Over_Period_Exceeding_KeepAlive() + { + using (var testEnvironment = new TestEnvironment(TestContext)) + { + const int KeepAlivePeriodSecs = 3; + + await testEnvironment.StartServer(); + + var options = new MqttClientOptionsBuilder().WithKeepAlivePeriod(TimeSpan.FromSeconds(KeepAlivePeriodSecs)); + var client = await testEnvironment.ConnectClient(options); + var message = new MqttApplicationMessageBuilder().WithTopic("a").Build(); + + try + { + // Publish messages over a time period exceeding the keep alive period. + // This should not cause an exception because of, i.e. "Client disconnected". + + for (var count = 0; count < KeepAlivePeriodSecs * 3; ++count) + { + // Send Publish requests well before the keep alive period expires + await client.PublishAsync(message); + await Task.Delay(1000); + } + } + catch (Exception ex) + { + Assert.Fail(ex.Message); + } + } + } + + [TestMethod] public async Task Subscribe_In_Callback_Events() {