Esempio n. 1
0
        public override byte[] Encode()
        {
            byte fixedHeader = (0x0A << Constants.Header.MessageTypeOffset) |
                               0x00 |
                               0x02 |
                               0x00;

            byte[] messageId = new byte[2];
            messageId[0] = (byte)((MessageId >> 8) & 0x00FF);
            messageId[1] = (byte)(MessageId & 0x00FF);

            ByteContainer topicContainer = new ByteContainer();
            int           index          = 0;

            while (index < Topics.Count)
            {
                topicContainer.Add(Topics[index]);
                index++;
            }

            byte[] payload = topicContainer.ToBytes();

            byte[] remainingLengthBytes = EncodeRemainingLength(2 + payload.Length);

            ByteContainer container = new ByteContainer();

            container.Add(fixedHeader);
            container.Add(remainingLengthBytes);
            container.Add(messageId);
            container.Add(payload);

            return(container.ToBytes());
        }
Esempio n. 2
0
        public override byte[] Encode()
        {
            byte fixedHeader = (byte)((0x09 << Constants.Header.MessageTypeOffset) |
                                      (byte)(0x00) |
                                      (byte)(0x00) |
                                      (byte)(0x00));

            byte[] messageId = new byte[2];
            messageId[0] = (byte)((this.MessageId >> 8) & 0x00FF); //MSB
            messageId[1] = (byte)(this.MessageId & 0x00FF);        //LSB

            ByteContainer qosContainer = new ByteContainer();
            int           index        = 0;

            while (index < this._qosLevels.Count)
            {
                byte qos = (byte)(int)this._qosLevels[index];
                qosContainer.Add(qos);
                index++;
            }

            byte[] payload = qosContainer.ToBytes();

            byte[] remainingLengthBytes = EncodeRemainingLength(2 + payload.Length);

            ByteContainer container = new ByteContainer();

            container.Add(fixedHeader);
            container.Add(remainingLengthBytes);
            container.Add(messageId);
            container.Add(payload);

            return(container.ToBytes());
        }
Esempio n. 3
0
        //public ushort MessageId { get; set; }

        public override byte[] Encode()
        {
            byte qos = Convert.ToByte((int)Enum.Parse(typeof(QualityOfServiceLevelType), this.QualityOfService.ToString(), false));

            //byte fixedHeader = (byte)((0x08 << Constants.Header.MessageTypeOffset) |
            //       (byte)(qos << Constants.Header.QosLevelOffset) |
            //       (byte)(this.Dup ? (byte)(0x01 << Constants.Header.DupFlagOffset) : (byte)0x00) |
            //       (byte)(this.Retain ? (byte)(0x01) : (byte)0x00));

            byte fixedHeader = (byte)((0x08 << Constants.Header.MessageTypeOffset) |
                                      (byte)(1 << Constants.Header.QosLevelOffset) |
                                      (byte)((byte)0x00) |
                                      (byte)((byte)0x00));

            byte[] messageId = new byte[2];
            messageId[0] = (byte)((this.MessageId >> 8) & 0x00FF); // MSB
            messageId[1] = (byte)(this.MessageId & 0x00FF);        // LSB

            ByteContainer payloadContainer = new ByteContainer();

            IEnumerator <KeyValuePair <string, QualityOfServiceLevelType> > en = this._topics.GetEnumerator();

            while (en.MoveNext())
            {
                string topic = en.Current.Key;
                QualityOfServiceLevelType qosLevel = this._topics[topic];
                payloadContainer.Add(topic);
                byte topicQos = Convert.ToByte((int)qosLevel);
                payloadContainer.Add(topicQos);
            }

            //int index = 0;
            //while (index < this._topics.Count)
            //{
            //    string topic = this._topics[index].Item1;
            //    QualityOfServiceLevelType qosLevel = this._topics[index].Item2;
            //    payloadContainer.Add(topic);
            //    byte topicQos = Convert.ToByte((int)qosLevel);
            //    payloadContainer.Add(topicQos);
            //    index++;
            //}

            byte[] payload = payloadContainer.ToBytes();

            byte[] remainingLengthBytes = EncodeRemainingLength(payload.Length + 2);

            ByteContainer container = new ByteContainer();

            container.Add(fixedHeader);
            container.Add(remainingLengthBytes);
            container.Add(messageId);
            container.Add(payload);

            return(container.ToBytes());
        }
Esempio n. 4
0
        //public byte[] Data { get; set; }

        public override byte[] Encode()
        {
            byte qos = (byte)(int)this.QualityOfServiceLevel;

            byte fixedHeader = (byte)((0x03 << Constants.Header.MessageTypeOffset) |
                                      (byte)(qos << Constants.Header.QosLevelOffset) |
                                      (byte)(this.Dup ? (byte)(0x01 << Constants.Header.DupFlagOffset) : (byte)0x00) |
                                      (byte)(this.Retain ? (byte)(0x01) : (byte)0x00));

            //PublishVariableHeader variableHeader = this.VariableHeader as PublishVariableHeader;
            //PublishPayload payload = this.Payload as PublishPayload;

            ByteContainer vhContainer = new ByteContainer();

            vhContainer.Add(this.Topic);

            if (qos > 0)
            {
                byte[] messageId = new byte[2];
                messageId[0] = (byte)((this.MessageId >> 8) & 0x00FF); // MSB
                messageId[1] = (byte)(this.MessageId & 0x00FF);        // LSB
                vhContainer.Add(messageId);
            }

            byte[] variableHeaderBytes = vhContainer.ToBytes();


            //byte[] variableHeaderBytes = variableHeader.Encode();
            //byte[] payloadBytes = payload.Encode();

            byte[] lengthSB = new byte[2];
            lengthSB[0] = (byte)((this.Payload.Length >> 8) & 0x00FF); // MSB
            lengthSB[1] = (byte)(this.Payload.Length & 0x00FF);        // LSB

            ByteContainer payloadContainer = new ByteContainer();

            //payloadContainer.Add(lengthSB);
            payloadContainer.Add(this.Payload);

            byte[] payloadBytes = payloadContainer.ToBytes();

            int remainingLength = variableHeaderBytes.Length + payloadBytes.Length;

            byte[] remainingLengthBytes = base.EncodeRemainingLength(remainingLength);

            ByteContainer container = new ByteContainer();

            container.Add(fixedHeader);
            container.Add(remainingLengthBytes);
            container.Add(variableHeaderBytes);
            container.Add(payloadBytes);

            return(container.ToBytes());
        }
Esempio n. 5
0
        //public ConnectMessage(ConnectVariableHeader variableHeader, ConnectPayload payload)
        //{
        //    this.VariableHeader = variableHeader;
        //    this.Payload = payload;
        //}

        public override byte[] Encode()
        {
            byte fixedHeader = (byte)((0x01 << Constants.Header.MessageTypeOffset) |
                                      (byte)(0x00 << Constants.Header.QosLevelOffset) |
                                      (byte)(0x00 << Constants.Header.DupFlagOffset) |
                                      (byte)(0x00));

            //ConnectVariableHeader variableHeader = this.VariableHeader as ConnectVariableHeader;
            //ConnectPayload payload = this.Payload as ConnectPayload;
            //variableHeader.SetConnectFlags(payload);

            SetConnectFlags();
            ByteContainer vhContainer = new ByteContainer();

            vhContainer.Add(this.ProtocolName);
            vhContainer.Add((byte)this.ProtocolVersion);
            vhContainer.Add(this.connectFlags);

            byte[] keepAlive = new byte[2];
            keepAlive[0] = (byte)((this.KeepAlive >> 8) & 0x00FF); // MSB
            keepAlive[1] = (byte)(this.KeepAlive & 0x00FF);        // LSB

            vhContainer.Add(keepAlive);

            byte[] variableHeaderBytes = vhContainer.ToBytes();

            ByteContainer payloadContainer = new ByteContainer();

            payloadContainer.Add(this.ClientId);
            payloadContainer.Add(this.WillTopic);
            payloadContainer.Add(this.WillMessage);
            payloadContainer.Add(this.Username);
            payloadContainer.Add(this.Password);

            byte[] payloadBytes = payloadContainer.ToBytes();

            //byte[] variableHeaderBytes = variableHeader.Encode();
            //byte[] payloadBytes = payload.Encode();

            int remainingLength = variableHeaderBytes.Length + payloadBytes.Length;

            byte[] remainingLengthBytes = base.EncodeRemainingLength(remainingLength);

            ByteContainer container = new ByteContainer();

            container.Add(fixedHeader);
            container.Add(remainingLengthBytes);
            container.Add(variableHeaderBytes);
            container.Add(payloadBytes);

            return(container.ToBytes());
        }
Esempio n. 6
0
        public override byte[] Encode()
        {
            byte fixedHeader = (0x0B << Constants.Header.MessageTypeOffset) |
                               0x00 |
                               0x00 |
                               0x00;

            byte[] messageId = new byte[2];
            messageId[0] = (byte)((MessageId >> 8) & 0x00FF);
            messageId[1] = (byte)(MessageId & 0x00FF);

            byte[] remainingLengthBytes = EncodeRemainingLength(2);

            ByteContainer container = new ByteContainer();

            container.Add(fixedHeader);
            container.Add(remainingLengthBytes);
            container.Add(messageId);

            return(container.ToBytes());
        }