Ejemplo n.º 1
0
        public static SubscribePacket DecodeSubscribe(byte[] data)
        {
            var r    = new Reader(data);
            var type = r.ReadByte();

            Guard.Assert((byte)PacketType.Subscribe, type);

            var length = r.ReadInt32Vlq(out var _);

            if (length + 2 > data.Length)
            {
                throw new InvalidOperationException("Packet too short");
            }

            var topicLength = r.ReadInt16BigEndian();
            var topic       = Encoding.GetString(r.ReadBytes(topicLength));
            var qos         = r.ReadByte();

            var result = new SubscribePacket(topic)
            {
                QoS = qos
            };

            ProcessTTR(r, result);

            Guard.Assert(r.Position, data.Length);

            return(result);
        }
Ejemplo n.º 2
0
        public static Span <byte> EncodeSubscribe(SubscribePacket packet)
        {
            var w = new Writer();

            w.Write((byte)PacketType.Subscribe);

            var topicBytes = Encoding.GetBytes(packet.Topic);
            var length     = topicBytes.Length + 2;

            w.WriteVlq(length);

            //Topic
            w.WriteBigEndian((short)topicBytes.Length);
            w.Write(topicBytes);
            w.Write((byte)packet.QoS);

            return(w.Buffer.AsSpan(0, w.Position));
        }