Пример #1
0
 static void DecodeConnAckPacket(DataReader reader, FbnsConnAckPacket packet, ref uint remainingLength)
 {
     packet.ConnAckFlags = reader.ReadByte();
     packet.ReturnCode   = (ConnectReturnCode)reader.ReadByte();
     remainingLength    -= 2;
     if (remainingLength > 0)
     {
         var authSize = reader.ReadUInt16();
         packet.Authentication = reader.ReadString(authSize);
         remainingLength      -= authSize + 2u;
         if (remainingLength > 0)
         {
             DebugLogger.Log(nameof(FbnsPacketDecoder),
                             $"Unhandled data in the buffer. Length of remaining data = {remainingLength}");
         }
     }
 }
Пример #2
0
 static void DecodeConnAckPacket(IByteBuffer buffer, FbnsConnAckPacket packet, ref int remainingLength)
 {
     packet.ConnAckFlags = buffer.ReadByte();
     packet.ReturnCode   = (ConnectReturnCode)buffer.ReadByte();
     remainingLength    -= 2;
     if (remainingLength > 0)
     {
         var authSize = buffer.ReadUnsignedShort();
         packet.Authentication = buffer.ReadString(authSize, Encoding.UTF8);
         remainingLength      -= authSize + 2;
         if (remainingLength > 0)
         {
             Debug.WriteLine(
                 $"FbnsPacketDecoder: Unhandled data in the buffer. Length of remaining data = {remainingLength}",
                 "Warning");
         }
     }
 }
Пример #3
0
        private Packet DecodePacketInternal(IByteBuffer buffer, int packetSignature, ref int 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(buffer, 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(buffer, subscribePacket, ref remainingLength);
                DecodeSubscribePayload(buffer, subscribePacket, ref remainingLength);
                return(subscribePacket);

            case Signatures.Connect:
                var connectPacket = new FbnsConnectPacket();
                DecodeConnectPacket(buffer, connectPacket, ref remainingLength);
                return(connectPacket);

            case Signatures.PubAck:
                var pubAckPacket = new PubAckPacket();
                DecodePacketIdVariableHeader(buffer, pubAckPacket, ref remainingLength);
                return(pubAckPacket);

            case Signatures.ConnAck:
                var connAckPacket = new FbnsConnAckPacket();
                DecodeConnAckPacket(buffer, connAckPacket, ref remainingLength);
                return(connAckPacket);

            case Signatures.SubAck:
                var subAckPacket = new SubAckPacket();
                DecodePacketIdVariableHeader(buffer, subAckPacket, ref remainingLength);
                DecodeSubAckPayload(buffer, subAckPacket, ref remainingLength);
                return(subAckPacket);

            case Signatures.Unsubscribe& 240:
                var unsubscribePacket = new UnsubscribePacket();
                DecodePacketIdVariableHeader(buffer, unsubscribePacket, ref remainingLength);
                DecodeUnsubscribePayload(buffer, unsubscribePacket, ref remainingLength);
                return(unsubscribePacket);

            case Signatures.PingResp:
                return(PingRespPacket.Instance);

            default:
                throw new DecoderException($"Packet type {packetSignature} not supported");
            }
        }