浏览代码

Refactoring

release/3.x.x
Christian Kratky 7 年前
父节点
当前提交
3954777d20
共有 1 个文件被更改,包括 18 次插入71 次删除
  1. +18
    -71
      MQTTnet.Core/Serializer/MqttPacketSerializer.cs

+ 18
- 71
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)


正在加载...
取消
保存