Пример #1
0
        public override void Encode(IByteBuffer buffer)
        {
            var buf = Unpooled.Buffer();

            try
            {
                if (SessionPresent)
                {
                    buf.WriteByte(1);  // 7 reserved 0-bits and SP = 1
                }
                else
                {
                    buf.WriteByte(0);  // 7 reserved 0-bits and SP = 0
                }
                buf.WriteByte((byte)ConnectReturnCode);

                FixedHeader.RemaingLength = buf.ReadableBytes;
                FixedHeader.WriteTo(buffer);
                buffer.WriteBytes(buf);
                buf = null;
            }
            finally
            {
                buf?.SafeRelease();
            }
        }
Пример #2
0
        public override void Encode(IByteBuffer buffer)
        {
            var buf = Unpooled.Buffer();

            try
            {
                //variable header
                buf.WriteString(ProtocolName);        //byte 1 - 8
                buf.WriteByte(ProtocolLevel);         //byte 9

                //connect flags;                      //byte 10
                var flags = UsernameFlag.ToByte() << 7;
                flags |= PasswordFlag.ToByte() << 6;
                flags |= WillRetain.ToByte() << 5;
                flags |= ((byte)WillQos) << 3;
                flags |= WillFlag.ToByte() << 2;
                flags |= CleanSession.ToByte() << 1;
                buf.WriteByte((byte)flags);

                //keep alive
                buf.WriteShort(KeepAlive);            //byte 11 - 12

                //payload
                buf.WriteString(ClientId);
                if (WillFlag)
                {
                    buf.WriteString(WillTopic);
                    buf.WriteBytes(WillMessage);
                }
                if (UsernameFlag && PasswordFlag)
                {
                    buf.WriteString(UserName);
                    buf.WriteString(Password);
                }

                FixedHeader.RemaingLength = buf.ReadableBytes;
                FixedHeader.WriteTo(buffer);
                buffer.WriteBytes(buf);
                buf = null;
            }
            finally
            {
                buf?.SafeRelease();
            }
        }
Пример #3
0
        /// <summary>
        /// EncodePacketIdVariableHeader
        /// </summary>
        /// <param name="buffer"></param>
        public override void Encode(IByteBuffer buffer)
        {
            var buf = Unpooled.Buffer();

            try
            {
                EncodePacketId(buf);

                FixedHeader.RemaingLength = buf.ReadableBytes;
                FixedHeader.WriteTo(buffer);
                buffer.WriteBytes(buf);
                buf = null;
            }
            finally
            {
                buf?.Release();
            }
        }
Пример #4
0
        public override void Encode(IByteBuffer buffer)
        {
            var buf = Unpooled.Buffer();

            try
            {
                buf.WriteString(TopicName);
                EncodePacketId(buf);
                buf.WriteBytes(Payload, 0, Payload.Length);

                FixedHeader.RemaingLength = buf.ReadableBytes;
                FixedHeader.WriteTo(buffer);
                buffer.WriteBytes(buf);
            }
            finally
            {
                buf?.Release();
            }
        }
Пример #5
0
        public override void Encode(IByteBuffer buffer)
        {
            var buf = Unpooled.Buffer();

            try
            {
                buf.WriteShort(PacketId);

                foreach (var item in _topics)
                {
                    buf.WriteString(item);
                }

                FixedHeader.RemaingLength = buf.ReadableBytes;
                FixedHeader.WriteTo(buffer);
                buffer.WriteBytes(buf);
            }
            finally
            {
                buf?.Release();
            }
        }
Пример #6
0
        public override void Encode(IByteBuffer buffer)
        {
            var buf = Unpooled.Buffer();

            try
            {
                buf.WriteUnsignedShort(PacketId);

                foreach (var item in _subscribeTopics)
                {
                    buf.WriteString(item.Topic);
                    buf.WriteByte((byte)item.Qos);
                }

                FixedHeader.RemaingLength = buf.ReadableBytes;
                FixedHeader.WriteTo(buffer);
                buffer.WriteBytes(buf);
                buf = null;
            }
            finally
            {
                buf?.Release();
            }
        }