From 3954777d200922f3bf111f42db9a622d364a769d Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Sat, 21 Oct 2017 20:12:38 +0200 Subject: [PATCH] Refactoring --- .../Serializer/MqttPacketSerializer.cs | 89 ++++--------------- 1 file changed, 18 insertions(+), 71 deletions(-) diff --git a/MQTTnet.Core/Serializer/MqttPacketSerializer.cs b/MQTTnet.Core/Serializer/MqttPacketSerializer.cs index 833d798..aa4d3b9 100644 --- a/MQTTnet.Core/Serializer/MqttPacketSerializer.cs +++ b/MQTTnet.Core/Serializer/MqttPacketSerializer.cs @@ -48,77 +48,24 @@ namespace MQTTnet.Core.Serializer private byte SerializePacket(MqttBasePacket packet, MqttPacketWriter writer) { - if (packet is MqttConnectPacket connectPacket) - { - return Serialize(connectPacket, writer); - } - - if (packet is MqttConnAckPacket connAckPacket) - { - return Serialize(connAckPacket, writer); - } - - if (packet is MqttDisconnectPacket) - { - return SerializeEmptyPacket(MqttControlPacketType.Disconnect); - } - - if (packet is MqttPingReqPacket) - { - return SerializeEmptyPacket(MqttControlPacketType.PingReq); - } - - if (packet is MqttPingRespPacket) - { - return SerializeEmptyPacket(MqttControlPacketType.PingResp); - } - - if (packet is MqttPublishPacket publishPacket) - { - return Serialize(publishPacket, writer); - } - - if (packet is MqttPubAckPacket pubAckPacket) - { - return Serialize(pubAckPacket, writer); - } - - if (packet is MqttPubRecPacket pubRecPacket) - { - return Serialize(pubRecPacket, writer); - } - - if (packet is MqttPubRelPacket pubRelPacket) - { - return Serialize(pubRelPacket, writer); - } - - if (packet is MqttPubCompPacket pubCompPacket) - { - return Serialize(pubCompPacket, writer); - } - - if (packet is MqttSubscribePacket subscribePacket) - { - return Serialize(subscribePacket, writer); - } - - if (packet is MqttSubAckPacket subAckPacket) - { - return Serialize(subAckPacket, writer); - } - - if (packet is MqttUnsubscribePacket unsubscribePacket) - { - return Serialize(unsubscribePacket, writer); - } - - if (packet is MqttUnsubAckPacket unsubAckPacket) - { - return Serialize(unsubAckPacket, writer); - } - - throw new MqttProtocolViolationException("Packet type invalid."); + switch (packet) + { + case MqttConnectPacket connectPacket: return Serialize(connectPacket, writer); + case MqttConnAckPacket connAckPacket: return Serialize(connAckPacket, writer); + case MqttDisconnectPacket _: return SerializeEmptyPacket(MqttControlPacketType.Disconnect); + case MqttPingReqPacket _: return SerializeEmptyPacket(MqttControlPacketType.PingReq); + case MqttPingRespPacket _: return SerializeEmptyPacket(MqttControlPacketType.PingResp); + case MqttPublishPacket publishPacket: return Serialize(publishPacket, writer); + case MqttPubAckPacket pubAckPacket: return Serialize(pubAckPacket, writer); + case MqttPubRecPacket pubRecPacket: return Serialize(pubRecPacket, writer); + case MqttPubRelPacket pubRelPacket: return Serialize(pubRelPacket, writer); + case MqttPubCompPacket pubCompPacket: return Serialize(pubCompPacket, writer); + case MqttSubscribePacket subscribePacket: return Serialize(subscribePacket, writer); + case MqttSubAckPacket subAckPacket: return Serialize(subAckPacket, writer); + case MqttUnsubscribePacket unsubscribePacket: return Serialize(unsubscribePacket, writer); + case MqttUnsubAckPacket unsubAckPacket: return Serialize(unsubAckPacket, writer); + default: throw new MqttProtocolViolationException("Packet type invalid."); + } } private static MqttBasePacket Deserialize(MqttPacketHeader header, MqttPacketReader reader)