Browse Source

Fix wrong keep alive delay calculation.

release/3.x.x
Christian Kratky 4 years ago
parent
commit
08c9ecb011
2 changed files with 8 additions and 4 deletions
  1. +3
    -3
      Source/MQTTnet/Adapter/MqttChannelAdapter.cs
  2. +5
    -1
      Source/MQTTnet/Client/MqttClient.cs

+ 3
- 3
Source/MQTTnet/Adapter/MqttChannelAdapter.cs View File

@@ -25,7 +25,7 @@ namespace MQTTnet.Adapter


readonly byte[] _fixedHeaderBuffer = new byte[2]; readonly byte[] _fixedHeaderBuffer = new byte[2];


SemaphoreSlim _writerSemaphore = new SemaphoreSlim(1, 1);
readonly SemaphoreSlim _writerSemaphore = new SemaphoreSlim(1, 1);


long _bytesReceived; long _bytesReceived;
long _bytesSent; long _bytesSent;
@@ -111,6 +111,8 @@ namespace MQTTnet.Adapter


public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout, CancellationToken cancellationToken) public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout, CancellationToken cancellationToken)
{ {
ThrowIfDisposed();

await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
try try
{ {
@@ -211,9 +213,7 @@ namespace MQTTnet.Adapter
if (disposing) if (disposing)
{ {
_channel?.Dispose(); _channel?.Dispose();

_writerSemaphore?.Dispose(); _writerSemaphore?.Dispose();
_writerSemaphore = null;
} }


base.Dispose(disposing); base.Dispose(disposing);


+ 5
- 1
Source/MQTTnet/Client/MqttClient.cs View File

@@ -446,7 +446,11 @@ namespace MQTTnet.Client
await SendAndReceiveAsync<MqttPingRespPacket>(new MqttPingReqPacket(), cancellationToken).ConfigureAwait(false); await SendAndReceiveAsync<MqttPingRespPacket>(new MqttPingReqPacket(), cancellationToken).ConfigureAwait(false);
} }


await Task.Delay(keepAlivePeriod, cancellationToken).ConfigureAwait(false);
// Wait a fixed time in all cases. Calculation of the remaining time is complicated
// due to some edge cases and was buggy in the past. Now we wait half a second because the
// min keep alive value is one second so that the server will wait 1.5 seconds for a PING
// packet.
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken).ConfigureAwait(false);
} }
} }
catch (Exception exception) catch (Exception exception)


Loading…
Cancel
Save