-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6922e27
commit 0b441f5
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace TurboMqtt.Core.PacketTypes; | ||
|
||
/// <summary> | ||
/// Base for all MQTT packets. | ||
/// </summary> | ||
public abstract class MqttPacket | ||
{ | ||
public abstract MqttPacketType PacketType { get; } | ||
|
||
public virtual bool Duplicate => false; | ||
|
||
public virtual QualityOfService QualityOfService => QualityOfService.AtMostOnce; | ||
|
||
public virtual bool RetainRequested => false; | ||
|
||
public override string ToString() | ||
{ | ||
return $"{GetType().Name}[Type={PacketType}, QualityOfService={QualityOfService}, Duplicate={Duplicate}, Retain={RetainRequested}]"; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Base for MQTT packets that require a packet identifier. | ||
/// </summary> | ||
public abstract class MqttPacketWithId : MqttPacket | ||
{ | ||
/// <summary> | ||
/// The unique identifier assigned to the packet. | ||
/// </summary> | ||
/// <remarks> | ||
/// Not all packets require an identifier. | ||
/// </remarks> | ||
public int PacketId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace TurboMqtt.Core.PacketTypes; | ||
|
||
/// <summary> | ||
/// Packet sent to the client by the server in response to a <see cref="PingReqPacket"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// Used to keep the connection alive. | ||
/// </remarks> | ||
public sealed class PingReqPacket : MqttPacket | ||
{ | ||
public static readonly PingReqPacket Instance = new PingReqPacket(); | ||
|
||
private PingReqPacket(){} | ||
public override MqttPacketType PacketType => MqttPacketType.PingReq; | ||
|
||
public override string ToString() | ||
{ | ||
return "PingReq"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace TurboMqtt.Core.PacketTypes; | ||
|
||
/// <summary> | ||
/// Packet sent to the client by the server in response to a <see cref="PingReqPacket"/>. | ||
/// </summary> | ||
public sealed class PingRespPacket : MqttPacket | ||
{ | ||
public static readonly PingRespPacket Instance = new PingRespPacket(); | ||
|
||
private PingRespPacket(){} | ||
public override MqttPacketType PacketType => MqttPacketType.PingResp; | ||
|
||
public override string ToString() | ||
{ | ||
return "PingResp"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
namespace TurboMqtt.Core.PacketTypes; | ||
|
||
/// <summary> | ||
/// Used to send data to the server or client. | ||
/// </summary> | ||
/// <param name="PacketId">The unique id of this packet - may not be needed with <see cref="QualityOfService.AtMostOnce"/>.</param> | ||
/// <param name="Qos">The delivery guarantee for this packet.</param> | ||
/// <param name="Duplicate">Is this packet a duplicate?</param> | ||
/// <param name="RetainRequested">Indicates whether or not this value has been retained by the MQTT broker.</param> | ||
public sealed class PublishPacket(int PacketId, QualityOfService Qos, bool Duplicate, bool RetainRequested) : MqttPacketWithId | ||
Check warning on line 10 in src/TurboMqtt.Core/PacketTypes/PublishPacket.cs GitHub Actions / Test-ubuntu-latest
Check warning on line 10 in src/TurboMqtt.Core/PacketTypes/PublishPacket.cs GitHub Actions / Test-ubuntu-latest
Check warning on line 10 in src/TurboMqtt.Core/PacketTypes/PublishPacket.cs GitHub Actions / Test-windows-latest
|
||
{ | ||
public override MqttPacketType PacketType => MqttPacketType.Publish; | ||
|
||
public override bool Duplicate { get; } = Duplicate; | ||
|
||
public override QualityOfService QualityOfService { get; } = Qos; | ||
|
||
public override bool RetainRequested { get; } = RetainRequested; | ||
|
||
/// <summary> | ||
/// Optional for <see cref="QualityOfService.AtMostOnce"/> | ||
/// </summary> | ||
public string? TopicName { get; set; } | ||
|
||
public int PacketIdentifier { get; set; } // Only needed for QoS 1 and 2 | ||
|
||
// Payload | ||
public ReadOnlyMemory<byte> Payload { get; set; } = ReadOnlyMemory<byte>.Empty; | ||
|
||
// MQTT 3.1.1 and 5.0 - Optional Properties | ||
|
||
/// <summary> | ||
/// The Content Type property, available in MQTT 5.0. | ||
/// This property is optional and indicates the MIME type of the application message. | ||
/// </summary> | ||
public string? ContentType { get; set; } // MQTT 5.0 only | ||
|
||
/// <summary> | ||
/// Response Topic property, available in MQTT 5.0. | ||
/// It specifies the topic name for a response message. | ||
/// </summary> | ||
public string? ResponseTopic { get; set; } // MQTT 5.0 only | ||
|
||
/// <summary> | ||
/// Correlation Data property, available in MQTT 5.0. | ||
/// This property is used by the sender of the request message to identify which request the response message is for when it receives a response. | ||
/// </summary> | ||
public byte[]? CorrelationData { get; set; } // MQTT 5.0 only | ||
|
||
/// <summary> | ||
/// User Property, available in MQTT 5.0. | ||
/// This is a key-value pair that can be sent multiple times to convey additional information that is not covered by other means. | ||
/// </summary> | ||
public IDictionary<string, string>? UserProperties { get; set; } // MQTT 5.0 only | ||
|
||
/// <summary> | ||
/// Subscription Identifiers, available in MQTT 5.0. | ||
/// This property allows associating the publication with multiple subscriptions. | ||
/// Each identifier corresponds to a different subscription that matches the published message. | ||
/// </summary> | ||
public List<uint>? SubscriptionIdentifiers { get; set; } // MQTT 5.0 only | ||
|
||
public override string ToString() | ||
{ | ||
return $"Publish: [Topic={TopicName}] [PayloadLength={Payload.Length}] [QoSLevel={QualityOfService}] [Dup={Duplicate}] [Retain={RetainRequested}] [PacketIdentifier={PacketIdentifier}]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters