Minor Changes

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

View File

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