mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 19:13:11 +08:00
Use the data converter only when creating packets.
This commit is contained in:
@@ -13,6 +13,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
@@ -42,7 +43,7 @@ namespace MQTTnet.Server
|
||||
long _sentPacketsCount = 1; // Start with 1 because the CONNECT packet is not counted anywhere.
|
||||
long _receivedApplicationMessagesCount;
|
||||
long _sentApplicationMessagesCount;
|
||||
MqttDisconnectReasonCode _disconnectReason;
|
||||
MqttClientDisconnectReason _disconnectReason;
|
||||
|
||||
public MqttClientConnection(MqttConnectPacket connectPacket,
|
||||
IMqttChannelAdapter channelAdapter,
|
||||
@@ -84,12 +85,12 @@ namespace MQTTnet.Server
|
||||
|
||||
public MqttClientSession Session { get; }
|
||||
|
||||
public async Task StopAsync(MqttDisconnectReasonCode reason)
|
||||
public async Task StopAsync(MqttClientDisconnectReason reason)
|
||||
{
|
||||
Status = MqttClientConnectionStatus.Finalizing;
|
||||
_disconnectReason = reason;
|
||||
|
||||
if (reason == MqttDisconnectReasonCode.SessionTakenOver || reason == MqttDisconnectReasonCode.KeepAliveTimeout)
|
||||
if (reason == MqttClientDisconnectReason.SessionTakenOver || reason == MqttClientDisconnectReason.KeepAliveTimeout)
|
||||
{
|
||||
// Is is very important to send the DISCONNECT packet here BEFORE cancelling the
|
||||
// token because the entire connection is closed (disposed) as soon as the cancellation
|
||||
@@ -97,10 +98,18 @@ namespace MQTTnet.Server
|
||||
// at the client!
|
||||
try
|
||||
{
|
||||
await _channelAdapter.SendPacketAsync(new MqttDisconnectPacket
|
||||
var disconnectOptions = new MqttClientDisconnectOptions
|
||||
{
|
||||
ReasonCode = reason
|
||||
}, CancellationToken.None).ConfigureAwait(false);
|
||||
ReasonCode = reason,
|
||||
ReasonString = reason.ToString()
|
||||
};
|
||||
|
||||
var disconnectPacket = _channelAdapter.PacketFormatterAdapter.DataConverter.CreateDisconnectPacket(disconnectOptions);
|
||||
|
||||
using (var timeout = new CancellationTokenSource(_serverOptions.DefaultCommunicationTimeout))
|
||||
{
|
||||
await _channelAdapter.SendPacketAsync(disconnectPacket, timeout.Token).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@@ -235,7 +244,7 @@ namespace MQTTnet.Server
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_disconnectReason == MqttDisconnectReasonCode.SessionTakenOver)
|
||||
if (_disconnectReason == MqttClientDisconnectReason.SessionTakenOver)
|
||||
{
|
||||
disconnectType = MqttClientDisconnectType.Takeover;
|
||||
}
|
||||
@@ -277,12 +286,7 @@ namespace MQTTnet.Server
|
||||
|
||||
Task HandleIncomingPubRelPacketAsync(MqttPubRelPacket pubRelPacket, CancellationToken cancellationToken)
|
||||
{
|
||||
var pubCompPacket = new MqttPubCompPacket
|
||||
{
|
||||
PacketIdentifier = pubRelPacket.PacketIdentifier,
|
||||
ReasonCode = MqttPubCompReasonCode.Success
|
||||
};
|
||||
|
||||
var pubCompPacket = _channelAdapter.PacketFormatterAdapter.DataConverter.CreatePubCompPacket(pubRelPacket, MqttApplicationMessageReceivedReasonCode.Success);
|
||||
return SendAsync(pubCompPacket, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -327,12 +331,12 @@ namespace MQTTnet.Server
|
||||
}
|
||||
case MqttQualityOfServiceLevel.AtLeastOnce:
|
||||
{
|
||||
var pubAckPacket = _dataConverter.CreatePubAckPacket(publishPacket);
|
||||
var pubAckPacket = _dataConverter.CreatePubAckPacket(publishPacket, MqttApplicationMessageReceivedReasonCode.Success);
|
||||
return SendAsync(pubAckPacket, cancellationToken);
|
||||
}
|
||||
case MqttQualityOfServiceLevel.ExactlyOnce:
|
||||
{
|
||||
var pubRecPacket = _dataConverter.CreatePubRecPacket(publishPacket);
|
||||
var pubRecPacket = _dataConverter.CreatePubRecPacket(publishPacket, MqttApplicationMessageReceivedReasonCode.Success);
|
||||
return SendAsync(pubRecPacket, cancellationToken);
|
||||
}
|
||||
default:
|
||||
@@ -441,13 +445,10 @@ namespace MQTTnet.Server
|
||||
using (var awaiter2 = _packetDispatcher.AddAwaiter<MqttPubCompPacket>(publishPacket.PacketIdentifier))
|
||||
{
|
||||
await SendAsync(publishPacket, cancellationToken).ConfigureAwait(false);
|
||||
await awaiter1.WaitOneAsync(_serverOptions.DefaultCommunicationTimeout).ConfigureAwait(false);
|
||||
var pubRecPacket = await awaiter1.WaitOneAsync(_serverOptions.DefaultCommunicationTimeout).ConfigureAwait(false);
|
||||
|
||||
await SendAsync(new MqttPubRelPacket
|
||||
{
|
||||
PacketIdentifier = publishPacket.PacketIdentifier,
|
||||
ReasonCode = MqttPubRelReasonCode.Success
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
var pubRelPacket = _channelAdapter.PacketFormatterAdapter.DataConverter.CreatePubRelPacket(pubRecPacket, MqttApplicationMessageReceivedReasonCode.Success);
|
||||
await SendAsync(pubRelPacket, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await awaiter2.WaitOneAsync(_serverOptions.DefaultCommunicationTimeout).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
using OperationCanceledException = System.OperationCanceledException;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
@@ -121,7 +122,7 @@ namespace MQTTnet.Server
|
||||
|
||||
foreach (var connection in connections)
|
||||
{
|
||||
await connection.StopAsync(MqttDisconnectReasonCode.NormalDisconnection).ConfigureAwait(false);
|
||||
await connection.StopAsync(MqttClientDisconnectReason.NormalDisconnection).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,7 +212,7 @@ namespace MQTTnet.Server
|
||||
|
||||
if (connection != null)
|
||||
{
|
||||
await connection.StopAsync(MqttDisconnectReasonCode.NormalDisconnection).ConfigureAwait(false);
|
||||
await connection.StopAsync(MqttClientDisconnectReason.NormalDisconnection).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_logger.Verbose("Session for client '{0}' deleted.", clientId);
|
||||
@@ -301,7 +302,7 @@ namespace MQTTnet.Server
|
||||
{
|
||||
if (clientConnection != null)
|
||||
{
|
||||
await clientConnection.StopAsync(MqttDisconnectReasonCode.NormalDisconnection).ConfigureAwait(false);
|
||||
await clientConnection.StopAsync(MqttClientDisconnectReason.NormalDisconnection).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -424,7 +425,7 @@ namespace MQTTnet.Server
|
||||
_connections[connectPacket.ClientId] = connection;
|
||||
}
|
||||
|
||||
existingConnection?.StopAsync(MqttDisconnectReasonCode.SessionTakenOver).GetAwaiter().GetResult();
|
||||
existingConnection?.StopAsync(MqttClientDisconnectReason.SessionTakenOver).GetAwaiter().GetResult();
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ using MQTTnet.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
using MQTTnet.Implementations;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
@@ -108,7 +108,7 @@ namespace MQTTnet.Server
|
||||
// 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);
|
||||
var _ = connection.StopAsync(MqttClientDisconnectReason.KeepAliveTimeout);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using MQTTnet.Formatter;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
|
||||
namespace MQTTnet.Server.Status
|
||||
{
|
||||
@@ -46,7 +46,7 @@ namespace MQTTnet.Server.Status
|
||||
|
||||
public Task DisconnectAsync()
|
||||
{
|
||||
return _connection.StopAsync(MqttDisconnectReasonCode.NormalDisconnection);
|
||||
return _connection.StopAsync(MqttClientDisconnectReason.NormalDisconnection);
|
||||
}
|
||||
|
||||
public void ResetStatistics()
|
||||
|
||||
Reference in New Issue
Block a user