mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-11-01 11:42:38 +08:00
Fix comment typos/text, refactor if-else chains to switch statements, optimize return statements, and invert conditional logic (#1961)
* Fix comment typos and refactor if-else chains to switch statements. * Refactor code --------- Co-authored-by: Wilmar Arias <wilmar.arias@keylinx.se> Co-authored-by: christian <6939810+chkr1011@users.noreply.github.com>
This commit is contained in:
co-authored by
Wilmar Arias
christian
parent
4481869d42
commit
ed63aa4910
+108
-108
@@ -218,7 +218,7 @@ namespace MQTTnet.Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sending the DISCONNECT may fail due to connection issues. The resulting exception
|
// Sending the DISCONNECT may fail due to connection issues. The resulting exception
|
||||||
// must be throw to let the caller know that the disconnect is not a clean one.
|
// must be thrown to let the caller know that the disconnect was not clean.
|
||||||
var disconnectPacket = MqttPacketFactories.Disconnect.Create(options);
|
var disconnectPacket = MqttPacketFactories.Disconnect.Create(options);
|
||||||
|
|
||||||
if (cancellationToken.CanBeCanceled)
|
if (cancellationToken.CanBeCanceled)
|
||||||
@@ -409,29 +409,37 @@ namespace MQTTnet.Client
|
|||||||
|
|
||||||
Task AcknowledgeReceivedPublishPacket(MqttApplicationMessageReceivedEventArgs eventArgs, CancellationToken cancellationToken)
|
Task AcknowledgeReceivedPublishPacket(MqttApplicationMessageReceivedEventArgs eventArgs, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (eventArgs.PublishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce)
|
switch (eventArgs.PublishPacket.QualityOfServiceLevel)
|
||||||
{
|
{
|
||||||
// no response required
|
case MqttQualityOfServiceLevel.AtMostOnce:
|
||||||
}
|
|
||||||
else if (eventArgs.PublishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce)
|
|
||||||
{
|
|
||||||
if (!eventArgs.ProcessingFailed)
|
|
||||||
{
|
{
|
||||||
var pubAckPacket = MqttPacketFactories.PubAck.Create(eventArgs);
|
// no response required
|
||||||
return Send(pubAckPacket, cancellationToken);
|
break;
|
||||||
}
|
}
|
||||||
}
|
case MqttQualityOfServiceLevel.AtLeastOnce:
|
||||||
else if (eventArgs.PublishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.ExactlyOnce)
|
|
||||||
{
|
|
||||||
if (!eventArgs.ProcessingFailed)
|
|
||||||
{
|
{
|
||||||
var pubRecPacket = MqttPacketFactories.PubRec.Create(eventArgs);
|
if (!eventArgs.ProcessingFailed)
|
||||||
return Send(pubRecPacket, cancellationToken);
|
{
|
||||||
|
var pubAckPacket = MqttPacketFactories.PubAck.Create(eventArgs);
|
||||||
|
return Send(pubAckPacket, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case MqttQualityOfServiceLevel.ExactlyOnce:
|
||||||
|
{
|
||||||
|
if (!eventArgs.ProcessingFailed)
|
||||||
|
{
|
||||||
|
var pubRecPacket = MqttPacketFactories.PubRec.Create(eventArgs);
|
||||||
|
return Send(pubRecPacket, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw new MqttProtocolViolationException("Received a not supported QoS level.");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new MqttProtocolViolationException("Received a not supported QoS level.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return CompletedTask.Instance;
|
return CompletedTask.Instance;
|
||||||
@@ -448,21 +456,25 @@ namespace MQTTnet.Client
|
|||||||
|
|
||||||
var receivedPacket = await Receive(cancellationToken).ConfigureAwait(false);
|
var receivedPacket = await Receive(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
if (receivedPacket is MqttConnAckPacket connAckPacket)
|
switch (receivedPacket)
|
||||||
{
|
{
|
||||||
result = MqttClientResultFactory.ConnectResult.Create(connAckPacket, channelAdapter.PacketFormatterAdapter.ProtocolVersion);
|
case MqttConnAckPacket connAckPacket:
|
||||||
}
|
{
|
||||||
else if (receivedPacket is MqttAuthPacket)
|
result = MqttClientResultFactory.ConnectResult.Create(connAckPacket, channelAdapter.PacketFormatterAdapter.ProtocolVersion);
|
||||||
{
|
break;
|
||||||
throw new NotSupportedException("Extended authentication handler is not yet supported");
|
}
|
||||||
}
|
case MqttAuthPacket _:
|
||||||
else if (receivedPacket == null)
|
{
|
||||||
{
|
throw new NotSupportedException("Extended authentication handler is not yet supported");
|
||||||
throw new MqttCommunicationException("Connection closed.");
|
}
|
||||||
}
|
case null:
|
||||||
else
|
{
|
||||||
{
|
throw new MqttCommunicationException("Connection closed.");
|
||||||
throw new InvalidOperationException($"Received an unexpected MQTT packet ({receivedPacket}).");
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Received an unexpected MQTT packet ({receivedPacket}).");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
@@ -592,8 +604,8 @@ namespace MQTTnet.Client
|
|||||||
_disconnectUserProperties,
|
_disconnectUserProperties,
|
||||||
exception);
|
exception);
|
||||||
|
|
||||||
// This handler must be executed in a new thread because otherwise a dead lock may happen
|
// This handler must be executed in a new thread to prevent a deadlock
|
||||||
// when trying to reconnect in that handler etc.
|
// that may occur when attempting to reconnect within that handler, etc.
|
||||||
Task.Run(() => _events.DisconnectedEvent.InvokeAsync(eventArgs)).RunInBackground(_logger);
|
Task.Run(() => _events.DisconnectedEvent.InvokeAsync(eventArgs)).RunInBackground(_logger);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -602,12 +614,9 @@ namespace MQTTnet.Client
|
|||||||
{
|
{
|
||||||
var clientWasConnected = IsConnected;
|
var clientWasConnected = IsConnected;
|
||||||
|
|
||||||
if (!DisconnectIsPendingOrFinished())
|
return !DisconnectIsPendingOrFinished() ?
|
||||||
{
|
DisconnectCore(sender, exception, connectResult, clientWasConnected)
|
||||||
return DisconnectCore(sender, exception, connectResult, clientWasConnected);
|
: CompletedTask.Instance;
|
||||||
}
|
|
||||||
|
|
||||||
return CompletedTask.Instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DisconnectIsPendingOrFinished()
|
bool DisconnectIsPendingOrFinished()
|
||||||
@@ -660,24 +669,19 @@ namespace MQTTnet.Client
|
|||||||
|
|
||||||
Task OnConnected(MqttClientConnectResult connectResult)
|
Task OnConnected(MqttClientConnectResult connectResult)
|
||||||
{
|
{
|
||||||
if (_events.ConnectedEvent.HasHandlers)
|
if (!_events.ConnectedEvent.HasHandlers)
|
||||||
{
|
{
|
||||||
var eventArgs = new MqttClientConnectedEventArgs(connectResult);
|
return CompletedTask.Instance;
|
||||||
return _events.ConnectedEvent.InvokeAsync(eventArgs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return CompletedTask.Instance;
|
var eventArgs = new MqttClientConnectedEventArgs(connectResult);
|
||||||
|
return _events.ConnectedEvent.InvokeAsync(eventArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
Task ProcessReceivedAuthPacket(MqttAuthPacket authPacket)
|
Task ProcessReceivedAuthPacket(MqttAuthPacket authPacket)
|
||||||
{
|
{
|
||||||
var extendedAuthenticationExchangeHandler = Options.ExtendedAuthenticationExchangeHandler;
|
var extendedAuthenticationExchangeHandler = Options.ExtendedAuthenticationExchangeHandler;
|
||||||
if (extendedAuthenticationExchangeHandler != null)
|
return extendedAuthenticationExchangeHandler != null ? extendedAuthenticationExchangeHandler.HandleRequestAsync(new MqttExtendedAuthenticationExchangeContext(authPacket, this)) : CompletedTask.Instance;
|
||||||
{
|
|
||||||
return extendedAuthenticationExchangeHandler.HandleRequestAsync(new MqttExtendedAuthenticationExchangeContext(authPacket, this));
|
|
||||||
}
|
|
||||||
|
|
||||||
return CompletedTask.Instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Task ProcessReceivedDisconnectPacket(MqttDisconnectPacket disconnectPacket)
|
Task ProcessReceivedDisconnectPacket(MqttDisconnectPacket disconnectPacket)
|
||||||
@@ -728,15 +732,15 @@ namespace MQTTnet.Client
|
|||||||
|
|
||||||
Task ProcessReceivedPubRecPacket(MqttPubRecPacket pubRecPacket, CancellationToken cancellationToken)
|
Task ProcessReceivedPubRecPacket(MqttPubRecPacket pubRecPacket, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (!_packetDispatcher.TryDispatch(pubRecPacket))
|
if (_packetDispatcher.TryDispatch(pubRecPacket))
|
||||||
{
|
{
|
||||||
// The packet is unknown. Probably due to a restart of the client.
|
return CompletedTask.Instance;
|
||||||
// So wen send this to the server to trigger a full resend of the message.
|
|
||||||
var pubRelPacket = MqttPacketFactories.PubRel.Create(pubRecPacket, MqttApplicationMessageReceivedReasonCode.PacketIdentifierNotFound);
|
|
||||||
return Send(pubRelPacket, cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return CompletedTask.Instance;
|
// The packet is unknown, probably due to a restart of the client.
|
||||||
|
// So we send this to the server to trigger a full resend of the message.
|
||||||
|
var pubRelPacket = MqttPacketFactories.PubRel.Create(pubRecPacket, MqttApplicationMessageReceivedReasonCode.PacketIdentifierNotFound);
|
||||||
|
return Send(pubRelPacket, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
Task ProcessReceivedPubRelPacket(MqttPubRelPacket pubRelPacket, CancellationToken cancellationToken)
|
Task ProcessReceivedPubRelPacket(MqttPubRelPacket pubRelPacket, CancellationToken cancellationToken)
|
||||||
@@ -973,39 +977,36 @@ namespace MQTTnet.Client
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (packet is MqttPublishPacket publishPacket)
|
switch (packet)
|
||||||
{
|
{
|
||||||
EnqueueReceivedPublishPacket(publishPacket);
|
case MqttPublishPacket publishPacket:
|
||||||
}
|
EnqueueReceivedPublishPacket(publishPacket);
|
||||||
else if (packet is MqttPubRecPacket pubRecPacket)
|
break;
|
||||||
{
|
case MqttPubRecPacket pubRecPacket:
|
||||||
await ProcessReceivedPubRecPacket(pubRecPacket, cancellationToken).ConfigureAwait(false);
|
await ProcessReceivedPubRecPacket(pubRecPacket, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
break;
|
||||||
else if (packet is MqttPubRelPacket pubRelPacket)
|
case MqttPubRelPacket pubRelPacket:
|
||||||
{
|
await ProcessReceivedPubRelPacket(pubRelPacket, cancellationToken).ConfigureAwait(false);
|
||||||
await ProcessReceivedPubRelPacket(pubRelPacket, cancellationToken).ConfigureAwait(false);
|
break;
|
||||||
}
|
case MqttDisconnectPacket disconnectPacket:
|
||||||
else if (packet is MqttDisconnectPacket disconnectPacket)
|
await ProcessReceivedDisconnectPacket(disconnectPacket).ConfigureAwait(false);
|
||||||
{
|
break;
|
||||||
await ProcessReceivedDisconnectPacket(disconnectPacket).ConfigureAwait(false);
|
case MqttAuthPacket authPacket:
|
||||||
}
|
await ProcessReceivedAuthPacket(authPacket).ConfigureAwait(false);
|
||||||
else if (packet is MqttAuthPacket authPacket)
|
break;
|
||||||
{
|
case MqttPingRespPacket _:
|
||||||
await ProcessReceivedAuthPacket(authPacket).ConfigureAwait(false);
|
_packetDispatcher.TryDispatch(packet);
|
||||||
}
|
break;
|
||||||
else if (packet is MqttPingRespPacket)
|
case MqttPingReqPacket _:
|
||||||
{
|
throw new MqttProtocolViolationException("The PINGREQ Packet is sent from a client to the server only.");
|
||||||
_packetDispatcher.TryDispatch(packet);
|
default:
|
||||||
}
|
|
||||||
else if (packet is MqttPingReqPacket)
|
|
||||||
{
|
|
||||||
throw new MqttProtocolViolationException("The PINGREQ Packet is sent from a client to the server only.");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!_packetDispatcher.TryDispatch(packet))
|
|
||||||
{
|
{
|
||||||
throw new MqttProtocolViolationException($"Received packet '{packet}' at an unexpected time.");
|
if (!_packetDispatcher.TryDispatch(packet))
|
||||||
|
{
|
||||||
|
throw new MqttProtocolViolationException($"Received packet '{packet}' at an unexpected time.");
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1016,19 +1017,19 @@ namespace MQTTnet.Client
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exception is OperationCanceledException)
|
switch (exception)
|
||||||
{
|
{
|
||||||
}
|
case OperationCanceledException _:
|
||||||
else if (exception is MqttCommunicationException)
|
break;
|
||||||
{
|
case MqttCommunicationException _:
|
||||||
_logger.Warning(exception, "Communication error while receiving packets");
|
_logger.Warning(exception, "Communication error while receiving packets");
|
||||||
}
|
break;
|
||||||
else
|
default:
|
||||||
{
|
_logger.Error(exception, "Error while processing received {0} packet", packet.GetType().Name);
|
||||||
_logger.Error(exception, "Error while processing received {0} packet", packet.GetType().Name);
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The packet dispatcher may already be gone due to disconnect etc!
|
// The packet dispatcher may already be gone due to disconnect etc.!
|
||||||
_packetDispatcher?.FailAll(exception);
|
_packetDispatcher?.FailAll(exception);
|
||||||
|
|
||||||
await DisconnectInternal(_packetReceiverTask, exception, null).ConfigureAwait(false);
|
await DisconnectInternal(_packetReceiverTask, exception, null).ConfigureAwait(false);
|
||||||
@@ -1071,17 +1072,16 @@ namespace MQTTnet.Client
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exception is OperationCanceledException)
|
switch (exception)
|
||||||
{
|
{
|
||||||
return;
|
case OperationCanceledException _:
|
||||||
}
|
return;
|
||||||
else if (exception is MqttCommunicationException)
|
case MqttCommunicationException _:
|
||||||
{
|
_logger.Warning(exception, "Communication error while sending/receiving keep alive packets");
|
||||||
_logger.Warning(exception, "Communication error while sending/receiving keep alive packets");
|
break;
|
||||||
}
|
default:
|
||||||
else
|
_logger.Error(exception, "Error exception while sending/receiving keep alive packets");
|
||||||
{
|
break;
|
||||||
_logger.Error(exception, "Error exception while sending/receiving keep alive packets");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await DisconnectInternal(_keepAlivePacketsSenderTask, exception, null).ConfigureAwait(false);
|
await DisconnectInternal(_keepAlivePacketsSenderTask, exception, null).ConfigureAwait(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user