Ejemplo n.º 1
0
        /// <summary>
        /// Reads an MQTT string from the underlying stream.
        /// </summary>
        /// <param name="stream">The stream to read the string from.</param>
        /// <returns>The Mqtt String.</returns>
        public static string ReadMqttString(this Stream stringStream)
        {
            // read and check the length
            var lengthBytes = new byte[2];
            int bytesRead = stringStream.Read(lengthBytes, 0, 2);
            if (bytesRead < 2)
            {
                throw new ArgumentException(
                    "The stream did not have enough bytes to describe the length of the string",
                    "stringStream");
            }

            System.Text.Encoding enc = new MqttEncoding();
            short stringLength = (short)enc.GetCharCount(lengthBytes);

            // read the bytes from the string, validate we have enough etc.
            var stringBytes = new byte[stringLength];
            bytesRead = stringStream.Read(stringBytes, 0, stringLength);
            if (bytesRead < stringLength)
            {
                throw new ArgumentException("stream",
                    String.Format("The stream did not have enough bytes to match the defined string length {0}", stringLength));
            }

            return enc.GetString(stringBytes);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Reads an MQTT string from the underlying stream.
        /// </summary>
        /// <param name="stringStream">The stream to read the string from.</param>
        /// <returns>The Mqtt String.</returns>
        public static string ReadMqttString(this Stream stringStream)
        {
            // read and check the length
            var lengthBytes = new byte[2];
            var bytesRead = stringStream.Read(lengthBytes, 0, 2);
            if (bytesRead < 2)
            {
                throw new ArgumentException(
                    "The stream did not have enough bytes to describe the length of the string",
                    "stringStream");
            }

            System.Text.Encoding enc = new MqttEncoding();
            var stringLength = (ushort)enc.GetCharCount(lengthBytes);

            // read the bytes from the string, validate we have enough etc.
            var stringBytes = new byte[stringLength];
            var readBuffer = new byte[1 << 10]; // 1KB read buffer
            var totalRead = 0;

            // Keep reading until we have all. Intentionally synchronous
            while (totalRead < stringLength) {
                var remainingBytes = stringLength - totalRead;
                var nextReadSize = remainingBytes > readBuffer.Length ? readBuffer.Length : remainingBytes;
                bytesRead = stringStream.Read(readBuffer, 0, nextReadSize);
                Array.Copy(readBuffer, 0, stringBytes, totalRead, bytesRead);
                totalRead += bytesRead;
            }

            return enc.GetString(stringBytes);
        }
Ejemplo n.º 3
0
        internal override int GetWriteLength()
        {
            int length = 0;
            MqttEncoding enc = new MqttEncoding();

            length += enc.GetByteCount(ClientIdentifier);

            if (this.variableHeader.ConnectFlags.WillFlag)
            {
                length += enc.GetByteCount(WillTopic);
                length += enc.GetByteCount(WillMessage);
            }

            return length;
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Gets the length of the write data when WriteTo will be called.
        /// </summary>
        /// <returns>The length of data witten by the call to GetWriteLength</returns>
        public virtual int GetWriteLength() {
            int headerLength = 0;
            var enc = new MqttEncoding();

            if ((WriteFlags & ReadWriteFlags.ProtocolName) == ReadWriteFlags.ProtocolName) {
                headerLength += enc.GetByteCount(ProtocolName);
            }
            if ((WriteFlags & ReadWriteFlags.ProtocolVersion) == ReadWriteFlags.ProtocolVersion) {
                headerLength += sizeof (byte);
            }
            if ((WriteFlags & ReadWriteFlags.ConnectFlags) == ReadWriteFlags.ConnectFlags) {
                headerLength += MqttConnectFlags.GetWriteLength();
            }
            if ((WriteFlags & ReadWriteFlags.KeepAlive) == ReadWriteFlags.KeepAlive) {
                headerLength += sizeof (short);
            }
            if ((WriteFlags & ReadWriteFlags.ReturnCode) == ReadWriteFlags.ReturnCode) {
                headerLength += sizeof (byte);
            }
            if ((WriteFlags & ReadWriteFlags.TopicName) == ReadWriteFlags.TopicName) {
                headerLength += enc.GetByteCount(TopicName.ToString());
            }
            if ((WriteFlags & ReadWriteFlags.MessageIdentifier) == ReadWriteFlags.MessageIdentifier) {
                headerLength += sizeof (short);
            }

            return headerLength;
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Writes the MQTT string.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="stringToWrite">The string to write.</param>
 public static void WriteMqttString(this Stream stringStream, string stringToWrite)
 {
     System.Text.Encoding enc = new MqttEncoding();
     byte[] stringBytes = enc.GetBytes(stringToWrite);
     stringStream.Write(stringBytes, 0, stringBytes.Length);
 }
Ejemplo n.º 6
0
        /// <summary>
        ///     Gets the length of the payload in bytes when written to a stream.
        /// </summary>
        /// <returns>The length of the payload in bytes.</returns>
        internal override int GetWriteLength() {
            int length = 0;
            var enc = new MqttEncoding();

            foreach (var sub in subscriptions) {
                length += enc.GetByteCount(sub.Key);
                length += sizeof (MqttQos);
            }

            return length;
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Gets the length of the payload in bytes when written to a stream.
        /// </summary>
        /// <returns>The length of the payload in bytes.</returns>
        internal override int GetWriteLength() {
            int length = 0;
            var enc = new MqttEncoding();

            foreach (var topic in subscriptions) {
                length += enc.GetByteCount(topic);
            }

            return length;
        }