[NF] Mqtt library changed
This commit is contained in:
+119
-20
@@ -1,29 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
#if SSL && !WINDOWS_PHONE
|
||||
#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
|
||||
using Microsoft.SPOT.Net.Security;
|
||||
#else
|
||||
using System.Net.Security;
|
||||
#endif
|
||||
#endif
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
@@ -38,6 +30,9 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
internal const byte MSG_TYPE_MASK = 0xF0;
|
||||
internal const byte MSG_TYPE_OFFSET = 0x04;
|
||||
internal const byte MSG_TYPE_SIZE = 0x04;
|
||||
internal const byte MSG_FLAG_BITS_MASK = 0x0F; // [v3.1.1]
|
||||
internal const byte MSG_FLAG_BITS_OFFSET = 0x00; // [v3.1.1]
|
||||
internal const byte MSG_FLAG_BITS_SIZE = 0x04; // [v3.1.1]
|
||||
internal const byte DUP_FLAG_MASK = 0x08;
|
||||
internal const byte DUP_FLAG_OFFSET = 0x03;
|
||||
internal const byte DUP_FLAG_SIZE = 0x01;
|
||||
@@ -64,11 +59,30 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
internal const byte MQTT_MSG_PINGRESP_TYPE = 0x0D;
|
||||
internal const byte MQTT_MSG_DISCONNECT_TYPE = 0x0E;
|
||||
|
||||
// [v3.1.1] MQTT flag bits
|
||||
internal const byte MQTT_MSG_CONNECT_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_CONNACK_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_PUBLISH_FLAG_BITS = 0x00; // just defined as 0x00 but depends on publish props (dup, qos, retain)
|
||||
internal const byte MQTT_MSG_PUBACK_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_PUBREC_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_PUBREL_FLAG_BITS = 0x02;
|
||||
internal const byte MQTT_MSG_PUBCOMP_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_SUBSCRIBE_FLAG_BITS = 0x02;
|
||||
internal const byte MQTT_MSG_SUBACK_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_UNSUBSCRIBE_FLAG_BITS = 0x02;
|
||||
internal const byte MQTT_MSG_UNSUBACK_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_PINGREQ_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_PINGRESP_FLAG_BITS = 0x00;
|
||||
internal const byte MQTT_MSG_DISCONNECT_FLAG_BITS = 0x00;
|
||||
|
||||
// QOS levels
|
||||
public const byte QOS_LEVEL_AT_MOST_ONCE = 0x00;
|
||||
public const byte QOS_LEVEL_AT_LEAST_ONCE = 0x01;
|
||||
public const byte QOS_LEVEL_EXACTLY_ONCE = 0x02;
|
||||
|
||||
// SUBSCRIBE QoS level granted failure [v3.1.1]
|
||||
public const byte QOS_LEVEL_GRANTED_FAILURE = 0x80;
|
||||
|
||||
internal const ushort MAX_TOPIC_LENGTH = 65535;
|
||||
internal const ushort MIN_TOPIC_LENGTH = 1;
|
||||
internal const byte MESSAGE_ID_SIZE = 2;
|
||||
@@ -113,6 +127,15 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
set { this.retain = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier for the message
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// message type
|
||||
@@ -123,12 +146,15 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
protected byte qosLevel;
|
||||
// retain flag
|
||||
protected bool retain;
|
||||
// message identifier
|
||||
protected ushort messageId;
|
||||
|
||||
/// <summary>
|
||||
/// Returns message bytes rapresentation
|
||||
/// </summary>
|
||||
/// <param name="protocolVersion">Protocol version</param>
|
||||
/// <returns>Bytes rapresentation</returns>
|
||||
public abstract byte[] GetBytes();
|
||||
public abstract byte[] GetBytes(byte protocolVersion);
|
||||
|
||||
/// <summary>
|
||||
/// Encode remaining length and insert it into message buffer
|
||||
@@ -172,5 +198,78 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
} while ((digit & 128) != 0);
|
||||
return value;
|
||||
}
|
||||
|
||||
#if TRACE
|
||||
/// <summary>
|
||||
/// Returns a string representation of the message for tracing
|
||||
/// </summary>
|
||||
/// <param name="name">Message name</param>
|
||||
/// <param name="fieldNames">Message fields name</param>
|
||||
/// <param name="fieldValues">Message fields value</param>
|
||||
/// <returns>String representation of the message</returns>
|
||||
protected string GetTraceString(string name, object[] fieldNames, object[] fieldValues)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append(name);
|
||||
|
||||
if ((fieldNames != null) && (fieldValues != null))
|
||||
{
|
||||
sb.Append("(");
|
||||
bool addComma = false;
|
||||
for (int i = 0; i < fieldValues.Length; i++)
|
||||
{
|
||||
if (fieldValues[i] != null)
|
||||
{
|
||||
if (addComma)
|
||||
{
|
||||
sb.Append(",");
|
||||
}
|
||||
|
||||
sb.Append(fieldNames[i]);
|
||||
sb.Append(":");
|
||||
sb.Append(GetStringObject(fieldValues[i]));
|
||||
addComma = true;
|
||||
}
|
||||
}
|
||||
sb.Append(")");
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
object GetStringObject(object value)
|
||||
{
|
||||
byte[] binary = value as byte[];
|
||||
if (binary != null)
|
||||
{
|
||||
string hexChars = "0123456789ABCDEF";
|
||||
StringBuilder sb = new StringBuilder(binary.Length * 2);
|
||||
for (int i = 0; i < binary.Length; ++i)
|
||||
{
|
||||
sb.Append(hexChars[binary[i] >> 4]);
|
||||
sb.Append(hexChars[binary[i] & 0x0F]);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
object[] list = value as object[];
|
||||
if (list != null)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append('[');
|
||||
for (int i = 0; i < list.Length; ++i)
|
||||
{
|
||||
if (i > 0) sb.Append(',');
|
||||
sb.Append(list[i]);
|
||||
}
|
||||
sb.Append(']');
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
@@ -37,6 +36,13 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
private const byte TOPIC_NAME_COMP_RESP_BYTE_OFFSET = 0;
|
||||
private const byte TOPIC_NAME_COMP_RESP_BYTE_SIZE = 1;
|
||||
// [v3.1.1] connect acknowledge flags replace "old" topic name compression respone (not used in 3.1)
|
||||
private const byte CONN_ACK_FLAGS_BYTE_OFFSET = 0;
|
||||
private const byte CONN_ACK_FLAGS_BYTE_SIZE = 1;
|
||||
// [v3.1.1] session present flag
|
||||
private const byte SESSION_PRESENT_FLAG_MASK = 0x01;
|
||||
private const byte SESSION_PRESENT_FLAG_OFFSET = 0x00;
|
||||
private const byte SESSION_PRESENT_FLAG_SIZE = 0x01;
|
||||
private const byte CONN_RETURN_CODE_BYTE_OFFSET = 1;
|
||||
private const byte CONN_RETURN_CODE_BYTE_SIZE = 1;
|
||||
|
||||
@@ -44,6 +50,16 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
#region Properties...
|
||||
|
||||
// [v3.1.1] session present flag
|
||||
/// <summary>
|
||||
/// Session present flag
|
||||
/// </summary>
|
||||
public bool SessionPresent
|
||||
{
|
||||
get { return this.sessionPresent; }
|
||||
set { this.sessionPresent = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return Code
|
||||
/// </summary>
|
||||
@@ -55,6 +71,9 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
#endregion
|
||||
|
||||
// [v3.1.1] session present flag
|
||||
private bool sessionPresent;
|
||||
|
||||
// return code for CONNACK message
|
||||
private byte returnCode;
|
||||
|
||||
@@ -70,26 +89,39 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a CONNACK message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>CONNACK message instance</returns>
|
||||
public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
MqttMsgConnack msg = new MqttMsgConnack();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_CONNACK_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
|
||||
// read bytes from socket...
|
||||
channel.Receive(buffer);
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] ... set session present flag ...
|
||||
msg.sessionPresent = (buffer[CONN_ACK_FLAGS_BYTE_OFFSET] & SESSION_PRESENT_FLAG_MASK) != 0x00;
|
||||
}
|
||||
// ...and set return code from broker
|
||||
msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET];
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte ProtocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -98,8 +130,12 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
|
||||
// topic name compression response and connect return code
|
||||
varHeaderSize += (TOPIC_NAME_COMP_RESP_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
|
||||
if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
// flags byte and connect return code
|
||||
varHeaderSize += (CONN_ACK_FLAGS_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
|
||||
else
|
||||
// topic name compression response and connect return code
|
||||
varHeaderSize += (TOPIC_NAME_COMP_RESP_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
|
||||
|
||||
remainingLength += (varHeaderSize + payloadSize);
|
||||
|
||||
@@ -119,19 +155,37 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index] = (byte)(MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET);
|
||||
index++;
|
||||
|
||||
if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_CONNACK_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (byte)(MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET);
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
|
||||
// topic name compression response (reserved values. not used);
|
||||
buffer[index++] = 0x00;
|
||||
if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
// [v3.1.1] session present flag
|
||||
buffer[index++] = this.sessionPresent ? (byte)(1 << SESSION_PRESENT_FLAG_OFFSET) : (byte)0x00;
|
||||
else
|
||||
// topic name compression response (reserved values. not used);
|
||||
buffer[index++] = 0x00;
|
||||
|
||||
// connect return code
|
||||
buffer[index++] = this.returnCode;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"CONNACK",
|
||||
new object[] { "returnCode" },
|
||||
new object[] { this.returnCode });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
@@ -30,19 +28,22 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
#region Constants...
|
||||
|
||||
// protocol name supported
|
||||
internal const string PROTOCOL_NAME = "MQIsdp";
|
||||
|
||||
// max length for client id
|
||||
internal const string PROTOCOL_NAME_V3_1 = "MQIsdp";
|
||||
internal const string PROTOCOL_NAME_V3_1_1 = "MQTT"; // [v.3.1.1]
|
||||
|
||||
// max length for client id (removed in 3.1.1)
|
||||
internal const int CLIENT_ID_MAX_LENGTH = 23;
|
||||
|
||||
// variable header fields
|
||||
internal const byte PROTOCOL_NAME_LEN_SIZE = 2;
|
||||
internal const byte PROTOCOL_NAME_SIZE = 6;
|
||||
internal const byte PROTOCOL_VERSION_NUMBER_SIZE = 1;
|
||||
internal const byte PROTOCOL_NAME_V3_1_SIZE = 6;
|
||||
internal const byte PROTOCOL_NAME_V3_1_1_SIZE = 4; // [v.3.1.1]
|
||||
internal const byte PROTOCOL_VERSION_SIZE = 1;
|
||||
internal const byte CONNECT_FLAGS_SIZE = 1;
|
||||
internal const byte KEEP_ALIVE_TIME_SIZE = 2;
|
||||
|
||||
internal const byte PROTOCOL_VERSION = 0x03;
|
||||
internal const byte PROTOCOL_VERSION_V3_1 = 0x03;
|
||||
internal const byte PROTOCOL_VERSION_V3_1_1 = 0x04; // [v.3.1.1]
|
||||
internal const ushort KEEP_ALIVE_PERIOD_DEFAULT = 60; // seconds
|
||||
internal const ushort MAX_KEEP_ALIVE = 65535; // 16 bit
|
||||
|
||||
@@ -65,6 +66,10 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
internal const byte CLEAN_SESSION_FLAG_MASK = 0x02;
|
||||
internal const byte CLEAN_SESSION_FLAG_OFFSET = 0x01;
|
||||
internal const byte CLEAN_SESSION_FLAG_SIZE = 0x01;
|
||||
// [v.3.1.1] lsb (reserved) must be now 0
|
||||
internal const byte RESERVED_FLAG_MASK = 0x01;
|
||||
internal const byte RESERVED_FLAG_OFFSET = 0x00;
|
||||
internal const byte RESERVED_FLAG_SIZE = 0x01;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -218,7 +223,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// </summary>
|
||||
/// <param name="clientId">Client identifier</param>
|
||||
public MqttMsgConnect(string clientId) :
|
||||
this(clientId, null, null, false, QOS_LEVEL_AT_LEAST_ONCE, false, null, null, true, KEEP_ALIVE_PERIOD_DEFAULT)
|
||||
this(clientId, null, null, false, QOS_LEVEL_AT_LEAST_ONCE, false, null, null, true, KEEP_ALIVE_PERIOD_DEFAULT, PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -235,6 +240,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// <param name="willMessage">Will message</param>
|
||||
/// <param name="cleanSession">Clean sessione flag</param>
|
||||
/// <param name="keepAlivePeriod">Keep alive period</param>
|
||||
/// <param name="protocolVersion">Protocol version</param>
|
||||
public MqttMsgConnect(string clientId,
|
||||
string username,
|
||||
string password,
|
||||
@@ -244,7 +250,8 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
string willTopic,
|
||||
string willMessage,
|
||||
bool cleanSession,
|
||||
ushort keepAlivePeriod
|
||||
ushort keepAlivePeriod,
|
||||
byte protocolVersion
|
||||
)
|
||||
{
|
||||
this.type = MQTT_MSG_CONNECT_TYPE;
|
||||
@@ -259,15 +266,19 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
this.willMessage = willMessage;
|
||||
this.cleanSession = cleanSession;
|
||||
this.keepAlivePeriod = keepAlivePeriod;
|
||||
// [v.3.1.1] added new protocol name and version
|
||||
this.protocolVersion = protocolVersion;
|
||||
this.protocolName = (this.protocolVersion == PROTOCOL_VERSION_V3_1_1) ? PROTOCOL_NAME_V3_1_1 : PROTOCOL_NAME_V3_1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse bytes for a CONNECT message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>CONNECT message instance</returns>
|
||||
public static MqttMsgConnect Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgConnect Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
@@ -302,11 +313,20 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
index += protNameUtf8Length;
|
||||
msg.protocolName = new String(Encoding.UTF8.GetChars(protNameUtf8));
|
||||
|
||||
// [v3.1.1] wrong protocol name
|
||||
if (!msg.protocolName.Equals(PROTOCOL_NAME_V3_1) && !msg.protocolName.Equals(PROTOCOL_NAME_V3_1_1))
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidProtocolName);
|
||||
|
||||
// protocol version
|
||||
msg.protocolVersion = buffer[index];
|
||||
index += PROTOCOL_VERSION_NUMBER_SIZE;
|
||||
index += PROTOCOL_VERSION_SIZE;
|
||||
|
||||
// connect flags
|
||||
// [v3.1.1] check lsb (reserved) must be 0
|
||||
if ((msg.protocolVersion == PROTOCOL_VERSION_V3_1_1) &&
|
||||
((buffer[index] & RESERVED_FLAG_MASK) != 0x00))
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidConnectFlags);
|
||||
|
||||
isUsernameFlag = (buffer[index] & USERNAME_FLAG_MASK) != 0x00;
|
||||
isPasswordFlag = (buffer[index] & PASSWORD_FLAG_MASK) != 0x00;
|
||||
msg.willRetain = (buffer[index] & WILL_RETAIN_FLAG_MASK) != 0x00;
|
||||
@@ -319,13 +339,16 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
msg.keepAlivePeriod = (ushort)((buffer[index++] << 8) & 0xFF00);
|
||||
msg.keepAlivePeriod |= buffer[index++];
|
||||
|
||||
// client identifier
|
||||
// client identifier [v3.1.1] it may be zero bytes long (empty string)
|
||||
clientIdUtf8Length = ((buffer[index++] << 8) & 0xFF00);
|
||||
clientIdUtf8Length |= buffer[index++];
|
||||
clientIdUtf8 = new byte[clientIdUtf8Length];
|
||||
Array.Copy(buffer, index, clientIdUtf8, 0, clientIdUtf8Length);
|
||||
index += clientIdUtf8Length;
|
||||
msg.clientId = new String(Encoding.UTF8.GetChars(clientIdUtf8));
|
||||
// [v3.1.1] if client identifier is zero bytes long, clean session must be true
|
||||
if ((msg.protocolVersion == PROTOCOL_VERSION_V3_1_1) && (clientIdUtf8Length == 0) && (!msg.cleanSession))
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidClientId);
|
||||
|
||||
// will topic and will message
|
||||
if (msg.willFlag)
|
||||
@@ -370,7 +393,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -380,21 +403,49 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
int index = 0;
|
||||
|
||||
byte[] clientIdUtf8 = Encoding.UTF8.GetBytes(this.clientId);
|
||||
byte[] willTopicUtf8 = (this.willTopic != null) ? Encoding.UTF8.GetBytes(this.willTopic) : null;
|
||||
byte[] willMessageUtf8 = (this.willMessage != null) ? Encoding.UTF8.GetBytes(this.willMessage) : null;
|
||||
byte[] usernameUtf8 = (this.username != null) ? Encoding.UTF8.GetBytes(this.username) : null;
|
||||
byte[] passwordUtf8 = (this.password != null) ? Encoding.UTF8.GetBytes(this.password) : null;
|
||||
byte[] willTopicUtf8 = (this.willFlag && (this.willTopic != null)) ? Encoding.UTF8.GetBytes(this.willTopic) : null;
|
||||
byte[] willMessageUtf8 = (this.willFlag && (this.willMessage != null)) ? Encoding.UTF8.GetBytes(this.willMessage) : null;
|
||||
byte[] usernameUtf8 = ((this.username != null) && (this.username.Length > 0)) ? Encoding.UTF8.GetBytes(this.username) : null;
|
||||
byte[] passwordUtf8 = ((this.password != null) && (this.password.Length > 0)) ? Encoding.UTF8.GetBytes(this.password) : null;
|
||||
|
||||
// [v3.1.1]
|
||||
if (this.protocolVersion == PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// will flag set, will topic and will message MUST be present
|
||||
if (this.willFlag && ((this.willQosLevel >= 0x03) ||
|
||||
(willTopicUtf8 == null) || (willMessageUtf8 == null) ||
|
||||
((willTopicUtf8 != null) && (willTopicUtf8.Length == 0)) ||
|
||||
((willMessageUtf8 != null) && (willMessageUtf8.Length == 0))))
|
||||
throw new MqttClientException(MqttClientErrorCode.WillWrong);
|
||||
// willflag not set, retain must be 0 and will topic and message MUST NOT be present
|
||||
else if (!this.willFlag && ((this.willRetain) ||
|
||||
(willTopicUtf8 != null) || (willMessageUtf8 != null) ||
|
||||
((willTopicUtf8 != null) && (willTopicUtf8.Length != 0)) ||
|
||||
((willMessageUtf8 != null) && (willMessageUtf8.Length != 0))))
|
||||
throw new MqttClientException(MqttClientErrorCode.WillWrong);
|
||||
}
|
||||
|
||||
// will flag set but will topic wrong
|
||||
if (this.willFlag && (willTopicUtf8.Length == 0))
|
||||
throw new MqttClientException(MqttClientErrorCode.WillTopicWrong);
|
||||
if (this.keepAlivePeriod > MAX_KEEP_ALIVE)
|
||||
throw new MqttClientException(MqttClientErrorCode.KeepAliveWrong);
|
||||
|
||||
// check on will QoS Level
|
||||
if ((this.willQosLevel < MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE) ||
|
||||
(this.willQosLevel > MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE))
|
||||
throw new MqttClientException(MqttClientErrorCode.WillWrong);
|
||||
|
||||
// protocol name field size
|
||||
varHeaderSize += (PROTOCOL_NAME_LEN_SIZE + PROTOCOL_NAME_SIZE);
|
||||
// protocol version number field size
|
||||
varHeaderSize += PROTOCOL_VERSION_NUMBER_SIZE;
|
||||
// MQTT version 3.1
|
||||
if (this.protocolVersion == PROTOCOL_VERSION_V3_1)
|
||||
{
|
||||
varHeaderSize += (PROTOCOL_NAME_LEN_SIZE + PROTOCOL_NAME_V3_1_SIZE);
|
||||
}
|
||||
// MQTT version 3.1.1
|
||||
else
|
||||
{
|
||||
varHeaderSize += (PROTOCOL_NAME_LEN_SIZE + PROTOCOL_NAME_V3_1_1_SIZE);
|
||||
}
|
||||
// protocol level field size
|
||||
varHeaderSize += PROTOCOL_VERSION_SIZE;
|
||||
// connect flags field size
|
||||
varHeaderSize += CONNECT_FLAGS_SIZE;
|
||||
// keep alive timer field size
|
||||
@@ -429,30 +480,40 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index++] = (MQTT_MSG_CONNECT_TYPE << MSG_TYPE_OFFSET);
|
||||
buffer[index++] = (MQTT_MSG_CONNECT_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_CONNECT_FLAG_BITS; // [v.3.1.1]
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
|
||||
// protocol name
|
||||
buffer[index++] = 0; // MSB protocol name size
|
||||
buffer[index++] = PROTOCOL_NAME_SIZE; // LSB protocol name size
|
||||
buffer[index++] = (byte)'M';
|
||||
buffer[index++] = (byte)'Q';
|
||||
buffer[index++] = (byte)'I';
|
||||
buffer[index++] = (byte)'s';
|
||||
buffer[index++] = (byte)'d';
|
||||
buffer[index++] = (byte)'p';
|
||||
|
||||
// protocol version
|
||||
buffer[index++] = PROTOCOL_VERSION;
|
||||
|
||||
// MQTT version 3.1
|
||||
if (this.protocolVersion == PROTOCOL_VERSION_V3_1)
|
||||
{
|
||||
buffer[index++] = PROTOCOL_NAME_V3_1_SIZE; // LSB protocol name size
|
||||
Array.Copy(Encoding.UTF8.GetBytes(PROTOCOL_NAME_V3_1), 0, buffer, index, PROTOCOL_NAME_V3_1_SIZE);
|
||||
index += PROTOCOL_NAME_V3_1_SIZE;
|
||||
// protocol version
|
||||
buffer[index++] = PROTOCOL_VERSION_V3_1;
|
||||
}
|
||||
// MQTT version 3.1.1
|
||||
else
|
||||
{
|
||||
buffer[index++] = PROTOCOL_NAME_V3_1_1_SIZE; // LSB protocol name size
|
||||
Array.Copy(Encoding.UTF8.GetBytes(PROTOCOL_NAME_V3_1_1), 0, buffer, index, PROTOCOL_NAME_V3_1_1_SIZE);
|
||||
index += PROTOCOL_NAME_V3_1_1_SIZE;
|
||||
// protocol version
|
||||
buffer[index++] = PROTOCOL_VERSION_V3_1_1;
|
||||
}
|
||||
|
||||
// connect flags
|
||||
byte connectFlags = 0x00;
|
||||
connectFlags |= (this.username != null) ? (byte)(1 << USERNAME_FLAG_OFFSET) : (byte)0x00;
|
||||
connectFlags |= (this.password != null) ? (byte)(1 << PASSWORD_FLAG_OFFSET) : (byte)0x00;
|
||||
connectFlags |= (usernameUtf8 != null) ? (byte)(1 << USERNAME_FLAG_OFFSET) : (byte)0x00;
|
||||
connectFlags |= (passwordUtf8 != null) ? (byte)(1 << PASSWORD_FLAG_OFFSET) : (byte)0x00;
|
||||
connectFlags |= (this.willRetain) ? (byte)(1 << WILL_RETAIN_FLAG_OFFSET) : (byte)0x00;
|
||||
connectFlags |= (byte)(this.willQosLevel << WILL_QOS_FLAG_OFFSET);
|
||||
// only if will flag is set, we have to use will QoS level (otherwise is MUST be 0)
|
||||
if (this.willFlag)
|
||||
connectFlags |= (byte)(this.willQosLevel << WILL_QOS_FLAG_OFFSET);
|
||||
connectFlags |= (this.willFlag) ? (byte)(1 << WILL_FLAG_OFFSET) : (byte)0x00;
|
||||
connectFlags |= (this.cleanSession) ? (byte)(1 << CLEAN_SESSION_FLAG_OFFSET) : (byte)0x00;
|
||||
buffer[index++] = connectFlags;
|
||||
@@ -468,7 +529,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
index += clientIdUtf8.Length;
|
||||
|
||||
// will topic
|
||||
if (this.willFlag && (this.willTopic != null))
|
||||
if (this.willFlag && (willTopicUtf8 != null))
|
||||
{
|
||||
buffer[index++] = (byte)((willTopicUtf8.Length >> 8) & 0x00FF); // MSB
|
||||
buffer[index++] = (byte)(willTopicUtf8.Length & 0x00FF); // LSB
|
||||
@@ -477,7 +538,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
}
|
||||
|
||||
// will message
|
||||
if (this.willFlag && (this.willMessage != null))
|
||||
if (this.willFlag && (willMessageUtf8 != null))
|
||||
{
|
||||
buffer[index++] = (byte)((willMessageUtf8.Length >> 8) & 0x00FF); // MSB
|
||||
buffer[index++] = (byte)(willMessageUtf8.Length & 0x00FF); // LSB
|
||||
@@ -486,7 +547,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
}
|
||||
|
||||
// username
|
||||
if (this.username != null)
|
||||
if (usernameUtf8 != null)
|
||||
{
|
||||
buffer[index++] = (byte)((usernameUtf8.Length >> 8) & 0x00FF); // MSB
|
||||
buffer[index++] = (byte)(usernameUtf8.Length & 0x00FF); // LSB
|
||||
@@ -495,7 +556,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
}
|
||||
|
||||
// password
|
||||
if (this.password != null)
|
||||
if (passwordUtf8 != null)
|
||||
{
|
||||
buffer[index++] = (byte)((passwordUtf8.Length >> 8) & 0x00FF); // MSB
|
||||
buffer[index++] = (byte)(passwordUtf8.Length & 0x00FF); // LSB
|
||||
@@ -505,5 +566,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"CONNECT",
|
||||
new object[] { "protocolName", "protocolVersion", "clientId", "willFlag", "willRetain", "willQosLevel", "willTopic", "willMessage", "username", "password", "cleanSession", "keepAlivePeriod" },
|
||||
new object[] { this.protocolName, this.protocolVersion, this.clientId, this.willFlag, this.willRetain, this.willQosLevel, this.willTopic, this.willMessage, this.username, this.password, this.cleanSession, this.keepAlivePeriod });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
@@ -50,6 +48,14 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Attempt (for retry)
|
||||
/// </summary>
|
||||
public int Attempt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Unique key
|
||||
/// </summary>
|
||||
public string Key
|
||||
{
|
||||
get { return this.Flow + "_" + this.Message.MessageId; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -128,6 +134,18 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// </summary>
|
||||
SendPuback,
|
||||
|
||||
// [v3.1.1] SUBSCRIBE isn't "officially" QOS = 1
|
||||
/// <summary>
|
||||
/// Send SUBSCRIBE message
|
||||
/// </summary>
|
||||
SendSubscribe,
|
||||
|
||||
// [v3.1.1] UNSUBSCRIBE isn't "officially" QOS = 1
|
||||
/// <summary>
|
||||
/// Send UNSUBSCRIBE message
|
||||
/// </summary>
|
||||
SendUnsubscribe,
|
||||
|
||||
/// <summary>
|
||||
/// (QOS = 1), SUBSCRIBE sent, wait for SUBACK
|
||||
/// </summary>
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
@@ -36,12 +35,20 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a DISCONNECT message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>DISCONNECT message instance</returns>
|
||||
public static MqttMsgDisconnect Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgDisconnect Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
MqttMsgDisconnect msg = new MqttMsgDisconnect();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_DISCONNECT_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
// NOTE : remainingLength must be 0
|
||||
@@ -49,16 +56,31 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
byte[] buffer = new byte[2];
|
||||
int index = 0;
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index++] = (MQTT_MSG_DISCONNECT_TYPE << MSG_TYPE_OFFSET);
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_DISCONNECT_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_DISCONNECT_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (MQTT_MSG_DISCONNECT_TYPE << MSG_TYPE_OFFSET);
|
||||
buffer[index++] = 0x00;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"DISCONNECT",
|
||||
null,
|
||||
null);
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
@@ -32,13 +31,16 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
this.type = MQTT_MSG_PINGREQ_TYPE;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
byte[] buffer = new byte[2];
|
||||
int index = 0;
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index++] = (MQTT_MSG_PINGREQ_TYPE << MSG_TYPE_OFFSET);
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_PINGREQ_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PINGREQ_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (MQTT_MSG_PINGREQ_TYPE << MSG_TYPE_OFFSET);
|
||||
buffer[index++] = 0x00;
|
||||
|
||||
return buffer;
|
||||
@@ -48,17 +50,37 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a PINGREQ message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>PINGREQ message instance</returns>
|
||||
public static MqttMsgPingReq Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgPingReq Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
MqttMsgPingReq msg = new MqttMsgPingReq();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PINGREQ_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// already know remaininglength is zero (MQTT specification),
|
||||
// so it isn't necessary to read other data from socket
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"PINGREQ",
|
||||
null,
|
||||
null);
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
@@ -37,12 +36,20 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a PINGRESP message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>PINGRESP message instance</returns>
|
||||
public static MqttMsgPingResp Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgPingResp Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
MqttMsgPingResp msg = new MqttMsgPingResp();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PINGRESP_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// already know remaininglength is zero (MQTT specification),
|
||||
// so it isn't necessary to read other data from socket
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
@@ -50,16 +57,31 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
byte[] buffer = new byte[2];
|
||||
int index = 0;
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index++] = (MQTT_MSG_PINGRESP_TYPE << MSG_TYPE_OFFSET);
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_PINGRESP_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PINGRESP_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (MQTT_MSG_PINGRESP_TYPE << MSG_TYPE_OFFSET);
|
||||
buffer[index++] = 0x00;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"PINGRESP",
|
||||
null,
|
||||
null);
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
/// <summary>
|
||||
@@ -23,23 +23,6 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// </summary>
|
||||
public class MqttMsgPuback : MqttMsgBase
|
||||
{
|
||||
#region Properties...
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier for the publish message
|
||||
/// that is acknowledged
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// message identifier
|
||||
private ushort messageId;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -48,7 +31,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
this.type = MQTT_MSG_PUBACK_TYPE;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -78,7 +61,10 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index++] = (MQTT_MSG_PUBACK_TYPE << MSG_TYPE_OFFSET);
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_PUBACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PUBACK_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (MQTT_MSG_PUBACK_TYPE << MSG_TYPE_OFFSET);
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
@@ -94,14 +80,22 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a PUBACK message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>PUBACK message instance</returns>
|
||||
public static MqttMsgPuback Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgPuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
MqttMsgPuback msg = new MqttMsgPuback();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PUBACK_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
@@ -115,5 +109,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"PUBACK",
|
||||
new object[] { "messageId" },
|
||||
new object[] { this.messageId });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
/// <summary>
|
||||
@@ -23,22 +23,6 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// </summary>
|
||||
public class MqttMsgPubcomp : MqttMsgBase
|
||||
{
|
||||
#region Properties...
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier for the acknowledged publish message
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// message identifier
|
||||
private ushort messageId;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -47,7 +31,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
this.type = MQTT_MSG_PUBCOMP_TYPE;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -77,7 +61,10 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index++] = (MQTT_MSG_PUBCOMP_TYPE << MSG_TYPE_OFFSET);
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_PUBCOMP_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PUBCOMP_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (MQTT_MSG_PUBCOMP_TYPE << MSG_TYPE_OFFSET);
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
@@ -93,14 +80,22 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a PUBCOMP message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>PUBCOMP message instance</returns>
|
||||
public static MqttMsgPubcomp Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgPubcomp Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
MqttMsgPubcomp msg = new MqttMsgPubcomp();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PUBCOMP_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
@@ -114,5 +109,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"PUBCOMP",
|
||||
new object[] { "messageId" },
|
||||
new object[] { this.messageId });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
@@ -47,24 +45,13 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
set { this.message = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// message topic
|
||||
private string topic;
|
||||
// message data
|
||||
private byte[] message;
|
||||
// message identifier
|
||||
ushort messageId;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -107,7 +94,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
this.messageId = 0;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -124,6 +111,10 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
if ((this.topic.Length < MIN_TOPIC_LENGTH) || (this.topic.Length > MAX_TOPIC_LENGTH))
|
||||
throw new MqttClientException(MqttClientErrorCode.TopicLength);
|
||||
|
||||
// check wrong QoS level (both bits can't be set 1)
|
||||
if (this.qosLevel > QOS_LEVEL_EXACTLY_ONCE)
|
||||
throw new MqttClientException(MqttClientErrorCode.QosNotAllowed);
|
||||
|
||||
byte[] topicUtf8 = Encoding.UTF8.GetBytes(this.topic);
|
||||
|
||||
// topic name
|
||||
@@ -200,9 +191,10 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a PUBLISH message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>PUBLISH message instance</returns>
|
||||
public static MqttMsgPublish Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgPublish Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
@@ -227,6 +219,9 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
// read QoS level from fixed header
|
||||
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
|
||||
// check wrong QoS level (both bits can't be set 1)
|
||||
if (msg.qosLevel > QOS_LEVEL_EXACTLY_ONCE)
|
||||
throw new MqttClientException(MqttClientErrorCode.QosNotAllowed);
|
||||
// read DUP flag from fixed header
|
||||
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
|
||||
// read retain flag from fixed header
|
||||
@@ -266,5 +261,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"PUBLISH",
|
||||
new object[] { "messageId", "topic", "message" },
|
||||
new object[] { this.messageId, this.topic, this.message });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
|
||||
@@ -40,18 +38,41 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
internal set { this.messageId = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Message published (or failed due to retries)
|
||||
/// </summary>
|
||||
public bool IsPublished
|
||||
{
|
||||
get { return this.isPublished; }
|
||||
internal set { this.isPublished = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// message identifier
|
||||
ushort messageId;
|
||||
|
||||
// published flag
|
||||
bool isPublished;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor (published message)
|
||||
/// </summary>
|
||||
/// <param name="messageId">Message identifier published</param>
|
||||
public MqttMsgPublishedEventArgs(ushort messageId)
|
||||
: this(messageId, true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="messageId">Message identifier published</param>
|
||||
public MqttMsgPublishedEventArgs(ushort messageId)
|
||||
/// <param name="messageId">Message identifier</param>
|
||||
/// <param name="isPublished">Publish flag</param>
|
||||
public MqttMsgPublishedEventArgs(ushort messageId, bool isPublished)
|
||||
{
|
||||
this.messageId = messageId;
|
||||
this.isPublished = isPublished;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
/// <summary>
|
||||
@@ -23,22 +23,6 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// </summary>
|
||||
public class MqttMsgPubrec : MqttMsgBase
|
||||
{
|
||||
#region Properties...
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier for the acknowledged publish message
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// message identifier
|
||||
private ushort messageId;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -47,7 +31,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
this.type = MQTT_MSG_PUBREC_TYPE;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -77,7 +61,10 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index++] = (MQTT_MSG_PUBREC_TYPE << MSG_TYPE_OFFSET);
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_PUBREC_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PUBREC_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (MQTT_MSG_PUBREC_TYPE << MSG_TYPE_OFFSET);
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
@@ -93,14 +80,22 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a PUBREC message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>PUBREC message instance</returns>
|
||||
public static MqttMsgPubrec Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgPubrec Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
MqttMsgPubrec msg = new MqttMsgPubrec();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PUBREC_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
@@ -114,5 +109,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"PUBREC",
|
||||
new object[] { "messageId" },
|
||||
new object[] { this.messageId });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
/// <summary>
|
||||
@@ -23,33 +23,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// </summary>
|
||||
public class MqttMsgPubrel : MqttMsgBase
|
||||
{
|
||||
#region Properties...
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier for the acknowledged publish message
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// message identifier
|
||||
private ushort messageId;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public MqttMsgPubrel()
|
||||
{
|
||||
this.type = MQTT_MSG_PUBREL_TYPE;
|
||||
// PUBREL message use QoS Level 1
|
||||
// PUBREL message use QoS Level 1 (not "officially" in 3.1.1)
|
||||
this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -79,10 +63,15 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index] = (byte)((MQTT_MSG_PUBREL_TYPE << MSG_TYPE_OFFSET) |
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_PUBREL_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PUBREL_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
{
|
||||
buffer[index] = (byte)((MQTT_MSG_PUBREL_TYPE << MSG_TYPE_OFFSET) |
|
||||
(this.qosLevel << QOS_LEVEL_OFFSET));
|
||||
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
|
||||
index++;
|
||||
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
|
||||
index++;
|
||||
}
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
@@ -98,14 +87,22 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a PUBREL message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>PUBREL message instance</returns>
|
||||
public static MqttMsgPubrel Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgPubrel Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
MqttMsgPubrel msg = new MqttMsgPubrel();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PUBREL_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
@@ -113,10 +110,15 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
// read bytes from socket...
|
||||
channel.Receive(buffer);
|
||||
|
||||
// read QoS level from fixed header (would be QoS Level 1)
|
||||
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
|
||||
// read DUP flag from fixed header
|
||||
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1)
|
||||
{
|
||||
// only 3.1.0
|
||||
|
||||
// read QoS level from fixed header (would be QoS Level 1)
|
||||
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
|
||||
// read DUP flag from fixed header
|
||||
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
|
||||
}
|
||||
|
||||
// message id
|
||||
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
|
||||
@@ -124,5 +126,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"PUBREL",
|
||||
new object[] { "messageId" },
|
||||
new object[] { this.messageId });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
@@ -27,16 +26,6 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
#region Properties...
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier for the subscribe message
|
||||
/// that is acknowledged
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// List of granted QOS Levels
|
||||
/// </summary>
|
||||
@@ -48,8 +37,6 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
#endregion
|
||||
|
||||
// message identifier
|
||||
private ushort messageId;
|
||||
// granted QOS levels
|
||||
byte[] grantedQosLevels;
|
||||
|
||||
@@ -65,14 +52,22 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a SUBACK message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>SUBACK message instance</returns>
|
||||
public static MqttMsgSuback Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgSuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
MqttMsgSuback msg = new MqttMsgSuback();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_SUBACK_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
@@ -95,7 +90,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -131,9 +126,11 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index] = (byte)(MQTT_MSG_SUBACK_TYPE << MSG_TYPE_OFFSET);
|
||||
index++;
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_SUBACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_SUBACK_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (byte)(MQTT_MSG_SUBACK_TYPE << MSG_TYPE_OFFSET);
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
|
||||
@@ -149,5 +146,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"SUBACK",
|
||||
new object[] { "messageId", "grantedQosLevels" },
|
||||
new object[] { this.messageId, this.grantedQosLevels });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
@@ -52,24 +50,13 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
set { this.qosLevels = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// topics to subscribe
|
||||
string[] topics;
|
||||
// QOS levels related to topics
|
||||
byte[] qosLevels;
|
||||
// message identifier
|
||||
ushort messageId;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -90,7 +77,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
this.topics = topics;
|
||||
this.qosLevels = qosLevels;
|
||||
|
||||
// SUBSCRIBE message uses QoS Level 1
|
||||
// SUBSCRIBE message uses QoS Level 1 (not "officially" in 3.1.1)
|
||||
this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
|
||||
}
|
||||
|
||||
@@ -98,9 +85,10 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a SUBSCRIBE message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>SUBSCRIBE message instance</returns>
|
||||
public static MqttMsgSubscribe Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgSubscribe Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
@@ -108,6 +96,13 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
int topicUtf8Length;
|
||||
MqttMsgSubscribe msg = new MqttMsgSubscribe();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_SUBSCRIBE_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
@@ -115,12 +110,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
// read bytes from socket...
|
||||
int received = channel.Receive(buffer);
|
||||
|
||||
// read QoS level from fixed header
|
||||
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
|
||||
// read DUP flag from fixed header
|
||||
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
|
||||
// retain flag not used
|
||||
msg.retain = false;
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1)
|
||||
{
|
||||
// only 3.1.0
|
||||
|
||||
// read QoS level from fixed header
|
||||
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
|
||||
// read DUP flag from fixed header
|
||||
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
|
||||
// retain flag not used
|
||||
msg.retain = false;
|
||||
}
|
||||
|
||||
// message id
|
||||
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
|
||||
@@ -165,7 +165,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -222,11 +222,16 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index] = (byte)((MQTT_MSG_SUBSCRIBE_TYPE << MSG_TYPE_OFFSET) |
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_SUBSCRIBE_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_SUBSCRIBE_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
{
|
||||
buffer[index] = (byte)((MQTT_MSG_SUBSCRIBE_TYPE << MSG_TYPE_OFFSET) |
|
||||
(this.qosLevel << QOS_LEVEL_OFFSET));
|
||||
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
|
||||
index++;
|
||||
|
||||
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
|
||||
index++;
|
||||
}
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
|
||||
@@ -251,5 +256,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"SUBSCRIBE",
|
||||
new object[] { "messageId", "topics", "qosLevels" },
|
||||
new object[] { this.messageId, this.topics, this.qosLevels });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
using uPLibrary.Networking.M2Mqtt.Exceptions;
|
||||
|
||||
namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
{
|
||||
@@ -25,23 +24,6 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// </summary>
|
||||
public class MqttMsgUnsuback : MqttMsgBase
|
||||
{
|
||||
#region Properties...
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier for the unsubscribe message
|
||||
/// that is acknowledged
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// message identifier
|
||||
private ushort messageId;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -54,14 +36,22 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a UNSUBACK message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>UNSUBACK message instance</returns>
|
||||
public static MqttMsgUnsuback Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgUnsuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
MqttMsgUnsuback msg = new MqttMsgUnsuback();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_UNSUBACK_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
@@ -76,7 +66,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -106,9 +96,11 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index] = (byte)(MQTT_MSG_UNSUBACK_TYPE << MSG_TYPE_OFFSET);
|
||||
index++;
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_UNSUBACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_UNSUBACK_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
buffer[index++] = (byte)(MQTT_MSG_UNSUBACK_TYPE << MSG_TYPE_OFFSET);
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
|
||||
@@ -118,5 +110,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"UNSUBACK",
|
||||
new object[] { "messageId" },
|
||||
new object[] { this.messageId });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
using System;
|
||||
@@ -43,22 +41,11 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
set { this.topics = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Message identifier
|
||||
/// </summary>
|
||||
public ushort MessageId
|
||||
{
|
||||
get { return this.messageId; }
|
||||
set { this.messageId = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// topics to unsubscribe
|
||||
string[] topics;
|
||||
// message identifier
|
||||
ushort messageId;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -77,7 +64,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
this.topics = topics;
|
||||
|
||||
// UNSUBSCRIBE message uses QoS Level 1
|
||||
// UNSUBSCRIBE message uses QoS Level 1 (not "officially" in 3.1.1)
|
||||
this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
|
||||
}
|
||||
|
||||
@@ -85,9 +72,10 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
/// Parse bytes for a UNSUBSCRIBE message
|
||||
/// </summary>
|
||||
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
|
||||
/// <param name="protocolVersion">Protocol Version</param>
|
||||
/// <param name="channel">Channel connected to the broker</param>
|
||||
/// <returns>UNSUBSCRIBE message instance</returns>
|
||||
public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
|
||||
public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
|
||||
{
|
||||
byte[] buffer;
|
||||
int index = 0;
|
||||
@@ -95,6 +83,13 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
int topicUtf8Length;
|
||||
MqttMsgUnsubscribe msg = new MqttMsgUnsubscribe();
|
||||
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
{
|
||||
// [v3.1.1] check flag bits
|
||||
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_UNSUBSCRIBE_FLAG_BITS)
|
||||
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
|
||||
}
|
||||
|
||||
// get remaining length and allocate buffer
|
||||
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
|
||||
buffer = new byte[remainingLength];
|
||||
@@ -102,12 +97,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
// read bytes from socket...
|
||||
int received = channel.Receive(buffer);
|
||||
|
||||
// read QoS level from fixed header
|
||||
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
|
||||
// read DUP flag from fixed header
|
||||
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
|
||||
// retain flag not used
|
||||
msg.retain = false;
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1)
|
||||
{
|
||||
// only 3.1.0
|
||||
|
||||
// read QoS level from fixed header
|
||||
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
|
||||
// read DUP flag from fixed header
|
||||
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
|
||||
// retain flag not used
|
||||
msg.retain = false;
|
||||
}
|
||||
|
||||
// message id
|
||||
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
|
||||
@@ -144,7 +144,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
return msg;
|
||||
}
|
||||
|
||||
public override byte[] GetBytes()
|
||||
public override byte[] GetBytes(byte protocolVersion)
|
||||
{
|
||||
int fixedHeaderSize = 0;
|
||||
int varHeaderSize = 0;
|
||||
@@ -192,11 +192,16 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
|
||||
|
||||
// first fixed header byte
|
||||
buffer[index] = (byte)((MQTT_MSG_UNSUBSCRIBE_TYPE << MSG_TYPE_OFFSET) |
|
||||
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
|
||||
buffer[index++] = (MQTT_MSG_UNSUBSCRIBE_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_UNSUBSCRIBE_FLAG_BITS; // [v.3.1.1]
|
||||
else
|
||||
{
|
||||
buffer[index] = (byte)((MQTT_MSG_UNSUBSCRIBE_TYPE << MSG_TYPE_OFFSET) |
|
||||
(this.qosLevel << QOS_LEVEL_OFFSET));
|
||||
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
|
||||
index++;
|
||||
|
||||
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
|
||||
index++;
|
||||
}
|
||||
|
||||
// encode remaining length
|
||||
index = this.encodeRemainingLength(remainingLength, buffer, index);
|
||||
|
||||
@@ -218,5 +223,17 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
#if TRACE
|
||||
return this.GetTraceString(
|
||||
"UNSUBSCRIBE",
|
||||
new object[] { "messageId", "topics" },
|
||||
new object[] { this.messageId, this.topics });
|
||||
#else
|
||||
return base.ToString();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
/*
|
||||
M2Mqtt - MQTT Client Library for .Net
|
||||
Copyright (c) 2014, Paolo Patierno, All rights reserved.
|
||||
Copyright (c) 2013, 2014 Paolo Patierno
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
Contributors:
|
||||
Paolo Patierno - initial API and implementation and/or initial documentation
|
||||
*/
|
||||
|
||||
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
|
||||
|
||||
Reference in New Issue
Block a user