YOUSEFUL / DELPHI / COMPONENTS
TMqttPacket
One MQTT 3.1.1 control packet as a record: the input of TAtlasMqttCodec.Encode and the output of Decode.
Description
A single record for all fourteen packet types. Kind says which fields are meaningful; the others are ignored when encoding and left empty when decoding. The components never expose packets. Use it for protocol tests, for tools, or for a transport or stand-in broker of your own. There is no separate message or will record: the client passes messages to OnMessage as separate arguments and takes the will from its Will... properties.
Members
| Member | Kind | What it does |
|---|---|---|
Kind | field | The packet type. |
Dup | field | PUBLISH: the duplicate flag, set when a message is sent again. |
Retain | field | PUBLISH: the retain flag. |
QoS | field | PUBLISH: the quality of service. |
Topic | field | PUBLISH: the topic name. |
Payload | field | PUBLISH: the application bytes. |
PacketId | field | The packet identifier, 1 to 65535, of PUBLISH above quality 0, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE and UNSUBACK. |
ClientId | field | CONNECT: the client identifier. |
Username | field | CONNECT: the user name; sent only when HasUsername is True. |
WillTopic | field | CONNECT: the will topic; sent only when HasWill is True. |
Password | field | CONNECT: the password, as bytes (it may be binary); sent only when HasPassword is True. |
WillPayload | field | CONNECT: the will message. |
HasUsername | field | CONNECT: a user name is present. |
HasPassword | field | CONNECT: a password is present. |
HasWill | field | CONNECT: a will is present. |
WillRetain | field | CONNECT: the will is to be retained. |
CleanSession | field | CONNECT: start a fresh session. |
WillQoS | field | CONNECT: the quality of the will. |
KeepAlive | field | CONNECT: the keep-alive period in seconds; 0 turns it off. |
ProtocolLevel | field | CONNECT: 4 for MQTT 3.1.1. |
SessionPresent | field | CONNACK: the broker kept a session from before. |
ReturnCode | field | CONNACK: 0 accepted; 1 to 5 refused. |
Filters | field | SUBSCRIBE and UNSUBSCRIBE: the topic filters; at least one. |
FilterQoS | field | SUBSCRIBE: the requested quality for each filter, in the same order. |
Granted | field | SUBACK: per filter, the granted quality 0, 1 or 2, or $80 (TAtlasMqttCodec.SubscribeFailure) for a refusal. |
Create | method | Returns an empty packet of the given kind, with ProtocolLevel 4 and CleanSession True; everything else is zero or empty. |
Methods
Create method
class function Create(AKind: TMqttPacketKind): TMqttPacket; static;Returns an empty packet of the given kind, with ProtocolLevel 4 and CleanSession True; everything else is zero or empty. Start every packet with it rather than with Default(TMqttPacket).
Packet := TMqttPacket.Create(mkPingReq);Fields
Kind field
Kind: TMqttPacketKind;The packet type. Decides which of the other fields mean anything.
Dup field
Dup, Retain: Boolean;PUBLISH: the duplicate flag, set when a message is sent again. Must be False at quality 0.
Retain field
Dup, Retain: Boolean;PUBLISH: the retain flag.
QoS field
QoS: TMqttQoS;PUBLISH: the quality of service.
Topic field
Topic: string;PUBLISH: the topic name. It may not be empty or contain wildcards.
Payload field
Payload: TBytes;PUBLISH: the application bytes. May be empty.
PacketId field
PacketId: Word;The packet identifier, 1 to 65535, of PUBLISH above quality 0, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE and UNSUBACK. 0 is invalid for these, and the encoder refuses it.
ClientId field
ClientId, Username, WillTopic: string;CONNECT: the client identifier.
Username field
ClientId, Username, WillTopic: string;CONNECT: the user name; sent only when HasUsername is True.
WillTopic field
ClientId, Username, WillTopic: string;CONNECT: the will topic; sent only when HasWill is True.
Password field
Password, WillPayload: TBytes;CONNECT: the password, as bytes (it may be binary); sent only when HasPassword is True.
WillPayload field
Password, WillPayload: TBytes;CONNECT: the will message.
HasUsername field
HasUsername, HasPassword, HasWill, WillRetain, CleanSession: Boolean;CONNECT: a user name is present.
HasPassword field
HasUsername, HasPassword, HasWill, WillRetain, CleanSession: Boolean;CONNECT: a password is present. MQTT 3.1.1 requires HasUsername too; the encoder refuses a password alone.
HasWill field
HasUsername, HasPassword, HasWill, WillRetain, CleanSession: Boolean;CONNECT: a will is present.
WillRetain field
HasUsername, HasPassword, HasWill, WillRetain, CleanSession: Boolean;CONNECT: the will is to be retained.
CleanSession field
HasUsername, HasPassword, HasWill, WillRetain, CleanSession: Boolean;CONNECT: start a fresh session. Create sets it to True.
WillQoS field
WillQoS: TMqttQoS;CONNECT: the quality of the will.
KeepAlive field
KeepAlive: Word;CONNECT: the keep-alive period in seconds; 0 turns it off.
ProtocolLevel field
ProtocolLevel: Byte;CONNECT: 4 for MQTT 3.1.1. Create sets it to 4.
SessionPresent field
SessionPresent: Boolean;CONNACK: the broker kept a session from before.
ReturnCode field
ReturnCode: Byte;CONNACK: 0 accepted; 1 to 5 refused. TAtlasMqttCodec.ConnAckText turns a code into words.
Filters field
Filters: TArray<string>;SUBSCRIBE and UNSUBSCRIBE: the topic filters; at least one.
FilterQoS field
FilterQoS: TArray<TMqttQoS>;SUBSCRIBE: the requested quality for each filter, in the same order.
Granted field
Granted: TArray<Byte>;SUBACK: per filter, the granted quality 0, 1 or 2, or $80 (TAtlasMqttCodec.SubscribeFailure) for a refusal.
Examples
Encode a PUBLISH
Topic a/b, quality 1, identifier 10, payload hi. The result is the eleven bytes 32 09 00 03 61 2F 62 00 0A 68 69.
uses
System.SysUtils, Atlas.MqttCodec;
function EncodedPublish: TBytes;
var
Packet: TMqttPacket;
begin
Packet := TMqttPacket.Create(mkPublish);
Packet.Topic := 'a/b';
Packet.QoS := qosAtLeastOnce;
Packet.PacketId := 10;
Packet.Payload := TEncoding.UTF8.GetBytes('hi');
Result := TAtlasMqttCodec.Encode(Packet);
end;Related
TMqttPacketKind · TAtlasMqttCodec · TAtlasMqttParser