Minor Changes

This commit is contained in:
BlubbFish 2018-08-11 23:13:47 +00:00
parent 9dc5a45051
commit 1e963d2639

View File

@ -18,45 +18,29 @@ using System;
using System.Text; using System.Text;
using uPLibrary.Networking.M2Mqtt.Exceptions; using uPLibrary.Networking.M2Mqtt.Exceptions;
namespace uPLibrary.Networking.M2Mqtt.Messages namespace uPLibrary.Networking.M2Mqtt.Messages {
{
/// <summary> /// <summary>
/// Class for PUBLISH message from client to broker /// Class for PUBLISH message from client to broker
/// </summary> /// </summary>
public class MqttMsgPublish : MqttMsgBase public class MqttMsgPublish : MqttMsgBase {
{
#region Properties... #region Properties...
/// <summary> /// <summary>
/// Message topic /// Message topic
/// </summary> /// </summary>
public string Topic public String Topic { get; set; }
{
get { return this.topic; }
set { this.topic = value; }
}
/// <summary> /// <summary>
/// Message data /// Message data
/// </summary> /// </summary>
public byte[] Message public Byte[] Message { get; set; }
{
get { return this.message; }
set { this.message = value; }
}
#endregion #endregion
// message topic
private string topic;
// message data
private byte[] message;
/// <summary> /// <summary>
/// Constructor /// Constructor
/// </summary> /// </summary>
public MqttMsgPublish() public MqttMsgPublish() {
{
this.type = MQTT_MSG_PUBLISH_TYPE; this.type = MQTT_MSG_PUBLISH_TYPE;
} }
@ -65,9 +49,7 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
/// </summary> /// </summary>
/// <param name="topic">Message topic</param> /// <param name="topic">Message topic</param>
/// <param name="message">Message data</param> /// <param name="message">Message data</param>
public MqttMsgPublish(string topic, byte[] message) : public MqttMsgPublish(String topic, Byte[] message) : this(topic, message, false, QOS_LEVEL_AT_MOST_ONCE, false) {
this(topic, message, false, QOS_LEVEL_AT_MOST_ONCE, false)
{
} }
/// <summary> /// <summary>
@ -78,110 +60,103 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
/// <param name="dupFlag">Duplicate flag</param> /// <param name="dupFlag">Duplicate flag</param>
/// <param name="qosLevel">Quality of Service level</param> /// <param name="qosLevel">Quality of Service level</param>
/// <param name="retain">Retain flag</param> /// <param name="retain">Retain flag</param>
public MqttMsgPublish(string topic, public MqttMsgPublish(String topic, Byte[] message, Boolean dupFlag, Byte qosLevel, Boolean retain) : base() {
byte[] message,
bool dupFlag,
byte qosLevel,
bool retain) : base()
{
this.type = MQTT_MSG_PUBLISH_TYPE; this.type = MQTT_MSG_PUBLISH_TYPE;
this.Topic = topic;
this.topic = topic; this.Message = message;
this.message = message;
this.dupFlag = dupFlag; this.dupFlag = dupFlag;
this.qosLevel = qosLevel; this.qosLevel = qosLevel;
this.retain = retain; this.retain = retain;
this.messageId = 0; this.messageId = 0;
} }
public override byte[] GetBytes(byte protocolVersion) public override Byte[] GetBytes(Byte protocolVersion) {
{ Int32 fixedHeaderSize = 0;
int fixedHeaderSize = 0; Int32 varHeaderSize = 0;
int varHeaderSize = 0; Int32 payloadSize = 0;
int payloadSize = 0; Int32 remainingLength = 0;
int remainingLength = 0; Byte[] buffer;
byte[] buffer; Int32 index = 0;
int index = 0;
// topic can't contain wildcards // topic can't contain wildcards
if ((this.topic.IndexOf('#') != -1) || (this.topic.IndexOf('+') != -1)) if ((this.Topic.IndexOf('#') != -1) || (this.Topic.IndexOf('+') != -1)) {
throw new MqttClientException(MqttClientErrorCode.TopicWildcard); throw new MqttClientException(MqttClientErrorCode.TopicWildcard);
}
// check topic length // check topic length
if ((this.topic.Length < MIN_TOPIC_LENGTH) || (this.topic.Length > MAX_TOPIC_LENGTH)) if ((this.Topic.Length < MIN_TOPIC_LENGTH) || (this.Topic.Length > MAX_TOPIC_LENGTH)) {
throw new MqttClientException(MqttClientErrorCode.TopicLength); throw new MqttClientException(MqttClientErrorCode.TopicLength);
}
// check wrong QoS level (both bits can't be set 1) // check wrong QoS level (both bits can't be set 1)
if (this.qosLevel > QOS_LEVEL_EXACTLY_ONCE) if (this.qosLevel > QOS_LEVEL_EXACTLY_ONCE) {
throw new MqttClientException(MqttClientErrorCode.QosNotAllowed); throw new MqttClientException(MqttClientErrorCode.QosNotAllowed);
}
byte[] topicUtf8 = Encoding.UTF8.GetBytes(this.topic); Byte[] topicUtf8 = Encoding.UTF8.GetBytes(this.Topic);
// topic name // topic name
varHeaderSize += topicUtf8.Length + 2; varHeaderSize += topicUtf8.Length + 2;
// message id is valid only with QOS level 1 or QOS level 2 // message id is valid only with QOS level 1 or QOS level 2
if ((this.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) || if ((this.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) || (this.qosLevel == QOS_LEVEL_EXACTLY_ONCE)) {
(this.qosLevel == QOS_LEVEL_EXACTLY_ONCE))
{
varHeaderSize += MESSAGE_ID_SIZE; varHeaderSize += MESSAGE_ID_SIZE;
} }
// check on message with zero length // check on message with zero length
if (this.message != null) if (this.Message != null) {
// message data // message data
payloadSize += this.message.Length; payloadSize += this.Message.Length;
}
remainingLength += (varHeaderSize + payloadSize); remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header // first byte of fixed header
fixedHeaderSize = 1; fixedHeaderSize = 1;
int temp = remainingLength; Int32 temp = remainingLength;
// increase fixed header size based on remaining length // increase fixed header size based on remaining length
// (each remaining length byte can encode until 128) // (each remaining length byte can encode until 128)
do do {
{
fixedHeaderSize++; fixedHeaderSize++;
temp = temp / 128; temp = temp / 128;
} while (temp > 0); } while (temp > 0);
// allocate buffer for message // allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize]; buffer = new Byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte // first fixed header byte
buffer[index] = (byte)((MQTT_MSG_PUBLISH_TYPE << MSG_TYPE_OFFSET) | buffer[index] = (Byte)((MQTT_MSG_PUBLISH_TYPE << MSG_TYPE_OFFSET) | (this.qosLevel << QOS_LEVEL_OFFSET));
(this.qosLevel << QOS_LEVEL_OFFSET)); buffer[index] |= this.dupFlag ? (Byte)(1 << DUP_FLAG_OFFSET) : (Byte)0x00;
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00; buffer[index] |= this.retain ? (Byte)(1 << RETAIN_FLAG_OFFSET) : (Byte)0x00;
buffer[index] |= this.retain ? (byte)(1 << RETAIN_FLAG_OFFSET) : (byte)0x00;
index++; index++;
// encode remaining length // encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index); index = this.encodeRemainingLength(remainingLength, buffer, index);
// topic name // topic name
buffer[index++] = (byte)((topicUtf8.Length >> 8) & 0x00FF); // MSB buffer[index++] = (Byte)((topicUtf8.Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(topicUtf8.Length & 0x00FF); // LSB buffer[index++] = (Byte)(topicUtf8.Length & 0x00FF); // LSB
Array.Copy(topicUtf8, 0, buffer, index, topicUtf8.Length); Array.Copy(topicUtf8, 0, buffer, index, topicUtf8.Length);
index += topicUtf8.Length; index += topicUtf8.Length;
// message id is valid only with QOS level 1 or QOS level 2 // message id is valid only with QOS level 1 or QOS level 2
if ((this.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) || if ((this.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) ||
(this.qosLevel == QOS_LEVEL_EXACTLY_ONCE)) (this.qosLevel == QOS_LEVEL_EXACTLY_ONCE)) {
{
// check message identifier assigned // check message identifier assigned
if (this.messageId == 0) if (this.messageId == 0) {
throw new MqttClientException(MqttClientErrorCode.WrongMessageId); throw new MqttClientException(MqttClientErrorCode.WrongMessageId);
buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB }
buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
buffer[index++] = (Byte)((this.messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (Byte)(this.messageId & 0x00FF); // LSB
} }
// check on message with zero length // check on message with zero length
if (this.message != null) if (this.Message != null) {
{
// message data // message data
Array.Copy(this.message, 0, buffer, index, this.message.Length); Array.Copy(this.Message, 0, buffer, index, this.Message.Length);
index += this.message.Length; index += this.Message.Length;
} }
return buffer; return buffer;
@ -194,34 +169,34 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
/// <param name="protocolVersion">Protocol Version</param> /// <param name="protocolVersion">Protocol Version</param>
/// <param name="channel">Channel connected to the broker</param> /// <param name="channel">Channel connected to the broker</param>
/// <returns>PUBLISH message instance</returns> /// <returns>PUBLISH message instance</returns>
public static MqttMsgPublish Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) public static MqttMsgPublish Parse(Byte fixedHeaderFirstByte, Byte protocolVersion, IMqttNetworkChannel channel) {
{ Byte[] buffer;
byte[] buffer; Int32 index = 0;
int index = 0; Byte[] topicUtf8;
byte[] topicUtf8; Int32 topicUtf8Length;
int topicUtf8Length;
MqttMsgPublish msg = new MqttMsgPublish(); MqttMsgPublish msg = new MqttMsgPublish();
// get remaining length and allocate buffer // get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel); Int32 remainingLength = decodeRemainingLength(channel);
buffer = new byte[remainingLength]; buffer = new Byte[remainingLength];
// read bytes from socket... // read bytes from socket...
int received = channel.Receive(buffer); Int32 received = channel.Receive(buffer);
// topic name // topic name
topicUtf8Length = ((buffer[index++] << 8) & 0xFF00); topicUtf8Length = ((buffer[index++] << 8) & 0xFF00);
topicUtf8Length |= buffer[index++]; topicUtf8Length |= buffer[index++];
topicUtf8 = new byte[topicUtf8Length]; topicUtf8 = new Byte[topicUtf8Length];
Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length); Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length);
index += topicUtf8Length; index += topicUtf8Length;
msg.topic = new String(Encoding.UTF8.GetChars(topicUtf8)); msg.Topic = new String(Encoding.UTF8.GetChars(topicUtf8));
// read QoS level from fixed header // read QoS level from fixed header
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET); msg.qosLevel = (Byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
// check wrong QoS level (both bits can't be set 1) // check wrong QoS level (both bits can't be set 1)
if (msg.qosLevel > QOS_LEVEL_EXACTLY_ONCE) if (msg.qosLevel > QOS_LEVEL_EXACTLY_ONCE) {
throw new MqttClientException(MqttClientErrorCode.QosNotAllowed); throw new MqttClientException(MqttClientErrorCode.QosNotAllowed);
}
// read DUP flag from fixed header // read DUP flag from fixed header
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01); msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
// read retain flag from fixed header // read retain flag from fixed header
@ -229,32 +204,30 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
// message id is valid only with QOS level 1 or QOS level 2 // message id is valid only with QOS level 1 or QOS level 2
if ((msg.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) || if ((msg.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) ||
(msg.qosLevel == QOS_LEVEL_EXACTLY_ONCE)) (msg.qosLevel == QOS_LEVEL_EXACTLY_ONCE)) {
{
// message id // message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00); msg.messageId = (UInt16)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]); msg.messageId |= (buffer[index++]);
} }
// get payload with message data // get payload with message data
int messageSize = remainingLength - index; Int32 messageSize = remainingLength - index;
int remaining = messageSize; Int32 remaining = messageSize;
int messageOffset = 0; Int32 messageOffset = 0;
msg.message = new byte[messageSize]; msg.Message = new Byte[messageSize];
// BUG FIX 26/07/2013 : receiving large payload // BUG FIX 26/07/2013 : receiving large payload
// copy first part of payload data received // copy first part of payload data received
Array.Copy(buffer, index, msg.message, messageOffset, received - index); Array.Copy(buffer, index, msg.Message, messageOffset, received - index);
remaining -= (received - index); remaining -= (received - index);
messageOffset += (received - index); messageOffset += (received - index);
// if payload isn't finished // if payload isn't finished
while (remaining > 0) while (remaining > 0) {
{
// receive other payload data // receive other payload data
received = channel.Receive(buffer); received = channel.Receive(buffer);
Array.Copy(buffer, 0, msg.message, messageOffset, received); Array.Copy(buffer, 0, msg.Message, messageOffset, received);
remaining -= received; remaining -= received;
messageOffset += received; messageOffset += received;
} }
@ -262,13 +235,12 @@ namespace uPLibrary.Networking.M2Mqtt.Messages
return msg; return msg;
} }
public override string ToString() public override String ToString() {
{
#if TRACE #if TRACE
return this.GetTraceString( return this.GetTraceString(
"PUBLISH", "PUBLISH",
new object[] { "messageId", "topic", "message" }, new Object[] { "messageId", "topic", "message" },
new object[] { this.messageId, this.topic, this.message }); new Object[] { this.messageId, this.Topic, this.Message });
#else #else
return base.ToString(); return base.ToString();
#endif #endif