Remove not used timeout for receiving.

This commit is contained in:
Christian Kratky
2020-11-02 21:45:23 +01:00
parent 44b7133a1c
commit fb2be79673
15 changed files with 49 additions and 42 deletions
@@ -167,7 +167,7 @@ namespace MQTTnet.Server
{
Status = MqttClientConnectionStatus.Running;
var packet = await _channelAdapter.ReceivePacketAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(false);
var packet = await _channelAdapter.ReceivePacketAsync(cancellationToken).ConfigureAwait(false);
if (packet == null)
{
// The client has closed the connection gracefully.
@@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OperationCanceledException = System.OperationCanceledException;
namespace MQTTnet.Server
{
@@ -59,14 +60,24 @@ namespace MQTTnet.Server
MqttConnectPacket connectPacket;
try
{
var firstPacket = await channelAdapter.ReceivePacketAsync(_options.DefaultCommunicationTimeout, cancellationToken).ConfigureAwait(false);
connectPacket = firstPacket as MqttConnectPacket;
if (connectPacket == null)
using (var timeoutToken = new CancellationTokenSource(_options.DefaultCommunicationTimeout))
{
_logger.Warning(null, "The first packet from client '{0}' was no 'CONNECT' packet [MQTT-3.1.0-1].", channelAdapter.Endpoint);
return;
var firstPacket = await channelAdapter.ReceivePacketAsync(timeoutToken.Token).ConfigureAwait(false);
connectPacket = firstPacket as MqttConnectPacket;
if (connectPacket == null)
{
_logger.Warning(null,
"The first packet from client '{0}' was no 'CONNECT' packet [MQTT-3.1.0-1].",
channelAdapter.Endpoint);
return;
}
}
}
catch (OperationCanceledException)
{
_logger.Warning(null, "Client '{0}' connected but did not sent a CONNECT packet.", channelAdapter.Endpoint);
return;
}
catch (MqttCommunicationTimedOutException)
{
_logger.Warning(null, "Client '{0}' connected but did not sent a CONNECT packet.", channelAdapter.Endpoint);
@@ -105,7 +105,10 @@ namespace MQTTnet.Server
// Execute the disconnection in background so that the keep alive monitor can continue
// with checking other connections.
Task.Run(() => connection.StopAsync(MqttDisconnectReasonCode.KeepAliveTimeout));
// We do not need to wait for the task so no await is needed.
// Also the internal state of the connection must be swapped to "Finalizing" because the
// next iteration of the keep alive timer happens.
var _ = connection.StopAsync(MqttDisconnectReasonCode.KeepAliveTimeout);
}
catch (Exception exception)
{