private static Packet DecodePacketInternal(DataReader reader, int packetSignature, ref uint remainingLength) { if (Signatures.IsPublish(packetSignature)) { var qualityOfService = (QualityOfService)((packetSignature >> 1) & 0x3); // take bits #1 and #2 ONLY and convert them into QoS value if (qualityOfService == QualityOfService.Reserved) { throw new DecoderException( $"Unexpected QoS value of {(int)qualityOfService} for {PacketType.PUBLISH} packet."); } bool duplicate = (packetSignature & 0x8) == 0x8; // test bit#3 bool retain = (packetSignature & 0x1) != 0; // test bit#0 var packet = new PublishPacket(qualityOfService, duplicate, retain); DecodePublishPacket(reader, packet, ref remainingLength); return(packet); } switch (packetSignature & 240) // We don't care about flags for these packets { case Signatures.Subscribe& 240: var subscribePacket = new SubscribePacket(); DecodePacketIdVariableHeader(reader, subscribePacket, ref remainingLength); DecodeSubscribePayload(reader, subscribePacket, ref remainingLength); return(subscribePacket); case Signatures.Connect: var connectPacket = new FbnsConnectPacket(); DecodeConnectPacket(reader, connectPacket, ref remainingLength); return(connectPacket); case Signatures.ConnAck: var connAckPacket = new FbnsConnAckPacket(); DecodeConnAckPacket(reader, connAckPacket, ref remainingLength); return(connAckPacket); case Signatures.SubAck: var subAckPacket = new SubAckPacket(); DecodePacketIdVariableHeader(reader, subAckPacket, ref remainingLength); DecodeSubAckPayload(reader, subAckPacket, ref remainingLength); return(subAckPacket); case Signatures.Unsubscribe& 240: var unsubscribePacket = new UnsubscribePacket(); DecodePacketIdVariableHeader(reader, unsubscribePacket, ref remainingLength); DecodeUnsubscribePayload(reader, unsubscribePacket, ref remainingLength); return(unsubscribePacket); case Signatures.PingResp: return(PingRespPacket.Instance); case Signatures.PubAck: var pubAckPacket = new PubAckPacket(); DecodePacketIdVariableHeader(reader, pubAckPacket, ref remainingLength); return(pubAckPacket); default: return(null); //default: // throw new DecoderException($"Packet type {packetSignature} not supported"); } }
public static Packet DecodePacket(DataReader reader) { reader.ByteOrder = ByteOrder.BigEndian; int signature = reader.ReadByte(); int remainingLength = DecodeRemainingLength(reader); if (Signatures.IsPublish(signature)) { var qualityOfService = (QualityOfService)((signature >> 1) & 0x3); // take bits #1 and #2 ONLY and convert them into QoS value if (qualityOfService == QualityOfService.Reserved) { throw new Exception( $"Unexpected QoS value of {(int)qualityOfService} for {PacketType.PUBLISH} packet."); } bool duplicate = (signature & 0x8) == 0x8; // test bit#3 bool retain = (signature & 0x1) != 0; // test bit#0 var packet = new PublishPacket(qualityOfService, duplicate, retain); DecodePublishPacket(reader, packet, ref remainingLength); return(packet); } switch (signature & 240) // We don't care about flags for these packets { // case Signatures.Subscribe & 240: // var subscribePacket = new SubscribePacket(); // DecodePacketIdVariableHeader(reader, subscribePacket, ref remainingLength); // DecodeSubscribePayload(buffer, subscribePacket, ref remainingLength); // return subscribePacket; case Signatures.Connect: var connectPacket = new ConnectPacket(); DecodeConnectPacket(reader, connectPacket, ref remainingLength); return(connectPacket); case Signatures.PubAck: var pubAckPacket = new PubAckPacket(); DecodePacketIdVariableHeader(reader, pubAckPacket, ref remainingLength); return(pubAckPacket); case Signatures.ConnAck: var connAckPacket = new FbnsConnAckPacket(); DecodeConnAckPacket(reader, connAckPacket, ref remainingLength); return(connAckPacket); case Signatures.SubAck: var subAckPacket = new SubAckPacket(); DecodePacketIdVariableHeader(reader, subAckPacket, ref remainingLength); DecodeSubAckPayload(reader, subAckPacket, ref remainingLength); return(subAckPacket); case Signatures.UnsubAck: var unsubAckPacket = new UnsubAckPacket(); DecodePacketIdVariableHeader(reader, unsubAckPacket, ref remainingLength); return(unsubAckPacket); case Signatures.PingResp: return(PingRespPacket.Instance); default: throw new Exception($"Packet type {signature} not supported"); } }