Skip to content

Commit

Permalink
Fixes #1276 timestamp check in server keep-alive monitor (#1277)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
logicaloud authored Oct 30, 2021
1 parent d7e1c89 commit 1e68c2e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Source/MQTTnet/Server/Internal/MqttClientConnectionStatistics.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using MQTTnet.Packets;
using MQTTnet.Server.Status;
Expand Down Expand Up @@ -32,8 +32,17 @@ public MqttClientConnectionStatistics()
_lastNonKeepAlivePacketReceivedTimestamp = _connectedTimestamp;
}

/// <summary>
/// Timestamp of the last package that has been received from the client ("sent" from the client's perspective)
/// </summary>
public DateTime LastPacketSentTimestamp => _lastPacketSentTimestamp;

/// <summary>
/// Timestamp of the last package that has been sent to the client ("received" from the client's perspective)
/// </summary>
public DateTime LastPacketReceivedTimestamp => _lastPacketReceivedTimestamp;


public void HandleReceivedPacket(MqttBasePacket packet)
{
if (packet == null) throw new ArgumentNullException(nameof(packet));
Expand Down
4 changes: 2 additions & 2 deletions Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Client.Disconnecting;
Expand Down Expand Up @@ -95,7 +95,7 @@ void TryMaintainConnection(MqttClientConnection connection, DateTime now)
// 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.
Expand Down
33 changes: 33 additions & 0 deletions Tests/MQTTnet.Core.Tests/Client/MqttClient_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,39 @@ public async Task Publish_QoS_1_In_ApplicationMessageReceiveHandler()
}
}

[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()
{
Expand Down

0 comments on commit 1e68c2e

Please sign in to comment.