Пример #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;
            }
        }
Пример #2
0
        public override void Encode(IByteBuffer buffer)
        {
            var buf = Unpooled.Buffer();

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

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

            try
            {
                buf.WriteShort(PacketIdentifier);

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

                FixedHeader.RemaingLength = buf.WriterIndex;
                FixedHeader.WriteTo(buffer);
                buffer.WriteBytes(buf);
                buf = null;
            }
            finally
            {
                buf?.Release();
            }
        }
Пример #4
0
        public Packet()
        {
            var att = (PacketTypeAttribute)Attribute.GetCustomAttribute(GetType(), typeof(PacketTypeAttribute));

            FixedHeader = new FixedHeader(att.PacketType);
        }