/// <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)); }
/// <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)); }
internal override int GetWriteLength() { int length = 0; var enc = new MqttEncoding(); length += enc.GetByteCount(ClientIdentifier); if (this.variableHeader.ConnectFlags.WillFlag) { length += enc.GetByteCount(WillTopic); length += enc.GetByteCount(WillMessage); } if (variableHeader.ConnectFlags.UsernameFlag) { length += enc.GetByteCount(Username); } if (variableHeader.ConnectFlags.PasswordFlag) { length += enc.GetByteCount(Password); } return(length); }
/// <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); }
/// <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); }
/// <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; MqttEncoding enc = new MqttEncoding(); foreach (KeyValuePair <string, MqttQos> sub in subscriptions) { length += enc.GetByteCount(sub.Key); length += sizeof(MqttQos); } return(length); }
/// <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); }
/// <summary> /// Writes the MQTT string. /// </summary> /// <param name="stringStream">The stream containing the string to write.</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); }