示例#1
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.WriteString(WillMessage);
                }
                if (UsernameFlag)
                {
                    buf.WriteString(UserName);
                }
                if (PasswordFlag)
                {
                    buf.WriteString(Password);
                }

                FixedHeader.RemaingLength = buf.WriterIndex;
                FixedHeader.WriteTo(buffer);
                buffer.WriteBytes(buf);
            }
            finally
            {
                buf?.Release();
                buf = null;
            }
        }
        /// <summary>
        /// CONNECT报文的可变报头按下列次序包含四个字段:
        /// 协议名(Protocol Name)
        /// 协议级别(Protocol Level)
        /// 连接标志(Connect Flags)
        /// 保持连接(Keep Alive)
        /// </summary>
        /// <param name="buffer"></param>
        public void Encode(IByteBuffer buffer)
        {
            // 协议名 Protocol Name
            // 协议级别 Protocol Level
            buffer.WriteString(ProtocolName);
            buffer.WriteByte(ProtocolLevel);

            // 连接标志 Connect Flags
            var connectFlags = UsernameFlag.ToByte() << 7;

            connectFlags |= PasswordFlag.ToByte() << 6;
            connectFlags |= WillRetain.ToByte() << 5;
            connectFlags |= ((byte)WillQos) << 3;
            connectFlags |= WillFlag.ToByte() << 2;
            connectFlags |= CleanSession.ToByte() << 1;
            buffer.WriteByte(connectFlags);

            // 保持连接 Keep Alive
            buffer.WriteShort(KeepAlive);
        }
示例#3
0
        public override void Encode(Stream stream)
        {
            using (var body = new MemoryStream())
            {
                //variable header
                body.WriteString(ProtocolName);       //byte 1 - 8
                body.WriteByte(ProtocolVersion);      //byte 9

                //ConnectFlags.WriteTo(body);         //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;
                body.WriteByte((byte)flags);

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

                //payload
                body.WriteString(ClientId);
                if (WillFlag)
                {
                    body.WriteString(WillTopic);
                    body.WriteString(WillMessage);
                }
                if (UsernameFlag)
                {
                    body.WriteString(Username);
                }
                if (PasswordFlag)
                {
                    body.WriteString(Password);
                }

                FixedHeader.RemaingLength = (int)body.Length;
                FixedHeader.WriteTo(stream);
                body.WriteTo(stream);
            }
        }