YOUSEFUL / DELPHI / COMPONENTS

TMqttPacket

One MQTT 3.1.1 control packet as a record: the input of TAtlasMqttCodec.Encode and the output of Decode.

Record Unit Atlas.MqttCodec · Product Atlas MQTT

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

MemberKindWhat it does
KindfieldThe packet type.
DupfieldPUBLISH: the duplicate flag, set when a message is sent again.
RetainfieldPUBLISH: the retain flag.
QoSfieldPUBLISH: the quality of service.
TopicfieldPUBLISH: the topic name.
PayloadfieldPUBLISH: the application bytes.
PacketIdfieldThe packet identifier, 1 to 65535, of PUBLISH above quality 0, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE and UNSUBACK.
ClientIdfieldCONNECT: the client identifier.
UsernamefieldCONNECT: the user name; sent only when HasUsername is True.
WillTopicfieldCONNECT: the will topic; sent only when HasWill is True.
PasswordfieldCONNECT: the password, as bytes (it may be binary); sent only when HasPassword is True.
WillPayloadfieldCONNECT: the will message.
HasUsernamefieldCONNECT: a user name is present.
HasPasswordfieldCONNECT: a password is present.
HasWillfieldCONNECT: a will is present.
WillRetainfieldCONNECT: the will is to be retained.
CleanSessionfieldCONNECT: start a fresh session.
WillQoSfieldCONNECT: the quality of the will.
KeepAlivefieldCONNECT: the keep-alive period in seconds; 0 turns it off.
ProtocolLevelfieldCONNECT: 4 for MQTT 3.1.1.
SessionPresentfieldCONNACK: the broker kept a session from before.
ReturnCodefieldCONNACK: 0 accepted; 1 to 5 refused.
FiltersfieldSUBSCRIBE and UNSUBSCRIBE: the topic filters; at least one.
FilterQoSfieldSUBSCRIBE: the requested quality for each filter, in the same order.
GrantedfieldSUBACK: per filter, the granted quality 0, 1 or 2, or $80 (TAtlasMqttCodec.SubscribeFailure) for a refusal.
CreatemethodReturns 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).

pascal
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.

pascal
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;

TMqttPacketKind · TAtlasMqttCodec · TAtlasMqttParser

Also in Atlas MQTT

TAtlasMqttClient · TAtlasMqttBroker