mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 08:33:01 +08:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cea751ff9d | ||
|
|
e88b280eb0 |
@@ -1,8 +1 @@
|
||||
* [Core] Fixed several dead locks by implementing a new async signal (#1552)
|
||||
* [Client] All waiting tasks will now fail when the client connection gets closed. This fixes several freezes (#1561)
|
||||
* [ManagedClient] The topic is no longer declared as invalid if a topic alias is used (#1395)
|
||||
* [Server] Fixed duplicated invocation of the event _ClientAcknowledgedPublishPacketAsync_ for QoS level 2 (#1550)
|
||||
* [Server] Fixed issue in upgrading and downgrading of QoS levels for subscriptions and retained messages (#1560)
|
||||
* [Server] Fixed memory leak when old sessions are discarded (#1553)
|
||||
* [Server] When using MQTTv5 the reason code _NoMatchingSubscribers_ is returned when no client is subscribed to a published topic (#802)
|
||||
* [Server] Fix sending of duplicate messages with QoS > 0 when session is recovered and session is reused (#1546)
|
||||
|
||||
|
||||
@@ -5,13 +5,78 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
|
||||
namespace MQTTnet
|
||||
{
|
||||
public static class MqttApplicationMessageExtensions
|
||||
{
|
||||
public static short ConvertPayloadToByte(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
if (applicationMessage.Payload.Length != 1)
|
||||
{
|
||||
throw new ArgumentException("Payload requires to be 1 bytes long.");
|
||||
}
|
||||
|
||||
return applicationMessage.Payload[0];
|
||||
}
|
||||
|
||||
public static short ConvertPayloadToInt16(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
if (applicationMessage.Payload.Length != 2)
|
||||
{
|
||||
throw new ArgumentException("Payload requires to be 2 bytes long.");
|
||||
}
|
||||
|
||||
return BitConverter.ToInt16(applicationMessage.Payload, 0);
|
||||
}
|
||||
|
||||
public static int ConvertPayloadToInt32(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
if (applicationMessage.Payload?.Length != 4)
|
||||
{
|
||||
throw new ArgumentException("Payload requires to be 4 bytes long.");
|
||||
}
|
||||
|
||||
return BitConverter.ToInt32(applicationMessage.Payload, 0);
|
||||
}
|
||||
|
||||
public static long ConvertPayloadToInt64(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
if (applicationMessage.Payload.Length != 8)
|
||||
{
|
||||
throw new ArgumentException("Payload requires to be 8 bytes long.");
|
||||
}
|
||||
|
||||
return BitConverter.ToInt64(applicationMessage.Payload, 0);
|
||||
}
|
||||
|
||||
public static string ConvertPayloadToString(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
if (applicationMessage.Payload == null)
|
||||
{
|
||||
@@ -25,5 +90,65 @@ namespace MQTTnet
|
||||
|
||||
return Encoding.UTF8.GetString(applicationMessage.Payload, 0, applicationMessage.Payload.Length);
|
||||
}
|
||||
|
||||
public static decimal ParsePayloadToDecimal(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
return decimal.Parse(ConvertPayloadToString(applicationMessage));
|
||||
}
|
||||
|
||||
public static double ParsePayloadToDouble(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
return double.Parse(ConvertPayloadToString(applicationMessage));
|
||||
}
|
||||
|
||||
public static short ParsePayloadToInt16(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
return short.Parse(ConvertPayloadToString(applicationMessage));
|
||||
}
|
||||
|
||||
public static int ParsePayloadToInt32(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
return int.Parse(ConvertPayloadToString(applicationMessage));
|
||||
}
|
||||
|
||||
public static long ParsePayloadToInt64(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
return long.Parse(ConvertPayloadToString(applicationMessage));
|
||||
}
|
||||
|
||||
public static float ParsePayloadToSingle(this MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
|
||||
return float.Parse(ConvertPayloadToString(applicationMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user