예제 #1
0
 static void DecodeConnAckPacket(DataReader buffer, FbnsConnAckPacket packet, ref int remainingLength)
 {
     packet.ConnAckFlags = buffer.ReadByte();
     packet.ReturnCode   = (ConnectReturnCode)buffer.ReadByte();
     remainingLength    -= 2;
     if (remainingLength > 0)
     {
         var authSize = buffer.ReadUInt16();
         packet.Authentication = buffer.ReadString(authSize);
         remainingLength      -= authSize + 2;
         if (remainingLength > 0)
         {
             Debug.WriteLine($"{nameof(StandalonePacketDecoder)} Unhandled data in the buffer. Length of remaining data = {remainingLength}");
         }
     }
 }
예제 #2
0
        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");
            }
        }