MQTT protocol -MQTT protocol analysis (MQTT packet structure)
协议就是通信双方的一个约定,
即,表示第1位传输的什么、第2位传输的什么……。
在MQTT协议中,一个MQTT数据包由:
固定头(Fixed header)、 可变头(Variable header)、 消息体(payload)三部分构成
MQTT packet structure
* 固定头(Fixed header),存在于所有MQTT数据包中,表示数据包类型及数据包的分组类标识
* 可变头(Variable header),存在于部分MQTT数据包中,数据包类型决定了可变头是否存在及其具体内容
* 消息体(Payload),存在于部分MQTT数据包中,表示客户端收到的具体内容
1 MQTT Glands
MQTT glands are present in all packets, the following structure:
Bit | 7 6 5 4 | 3 2 1 0 |
---|---|---|
byte 1 | MQTT packet types | Specifically identify different types of data packets MQTT |
byte 2 | ... remaining length |
1.1 MQTT packet types
Location : byte 1, bits 7-4.
Phase in a 4-bit unsigned values, the following types:
name | value | Flow direction | description |
---|---|---|---|
Reserved | 0 | unavailable | Reserved bits |
CONNECT | 1 | Client-server | Client requests the server to connect to |
CONNACK | 2 | Server to the client | Connection confirmation |
PUBLISH | 3 | Two-way | make an announcement |
PUBACK | 4 | Two-way | Published confirmation |
PUBREC | 5 | Two-way | Posted received (Part 1 guarantee arrival) |
PUBREL | Six pairs of bribery | Release release (Part 2 guarantee arrival) | |
PUBCOMP | 7 | Two-way | Release complete (Part 3 assurance arrival) |
SUBSCRIBE | 8 | Client-server | Subscribe to client requests |
SUBACK | 9 | Server to the client | Subscription Confirmation |
UNSUBSCRIBE | 10 | Client-server | Request to unsubscribe |
UNSUBACK | 11 | Server to the client | Unsubscribe confirmation |
PINGREQ | 12 | Client-server | PING requests |
PINGRESP | 13 | Server to the client | PING reply |
DISCONNECT | 14 | Client-server | Disconnect |
Reserved | 15 | unavailable | Reserved bits |
1.2 flag
Location : byte 1, bits 3-0.
Without the use of message type flag, the flag is used as a reserved bit. If the invalid flag is received, the receiver must off a network connection:
data pack | Flag | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
---|---|---|---|---|---|
CONNECT | Reserved bits | 0 | 0 | 0 | 0 |
CONNACK | Reserved bits | 0 0 | 0 | 0 | |
PUBLISH | MQTT 3.1.1 Use | DUP1 | QoS2 | QoS2 | RETAIN3 |
PUBACK | Reserved bits | 0 | 0 | 0 | 0 |
PUBREC | Reserved bits | 0 | 0 | 0 | 0 |
PUBREL | Reserved bits | 0 | 0 | 0 | 0 |
PUBCOMP | Reserved bits | 0 | 0 | 0 | 0 |
SUBSCRIBE | Reserved bits | 0 | 0 | 0 | 0 |
SUBACK | Reserved bits | 0 | 0 | 0 | 0 |
UNSUBSCRIBE | Reserved bits | 0 | 0 | 0 | 0 |
UNSUBACK | Reserved bits | 0 | 0 | 0 | 0 |
PINGREQ | Reserved bits | 0 | 0 | 0 | 0 |
PINGRESP | Reserved bits | 0 | 0 | 0 | 0 |
DISCONNECT | Reserved bits | 0 | 0 | 0 | 0 |
- DUP: publish a copy of the message. Is used to ensure reliable transmission of the message, if set to 1, is increased in the following MessageId longer, and requires acknowledgment reply to the message to ensure that the transfer is complete, but not for detection of repeatedly transmitted message.
- QoS: Quality of Service news release, namely: to ensure that the number of message delivery
- 00: at most once, namely: <= 1
- 01: at least once, namely:> = 1
- 10: once, namely: 1 =
- 11: Reserved
- RETAIN: 发布保留标识,表示服务器要保留这次推送的信息,如果有新的订阅者出现,就把这消息推送给它,如果设有那么推送至当前订阅者后释放。
1.3 剩余长度(Remaining Length)
位置:byte 1。
固定头的第二字节用来保存变长头部和消息体的总大小的,但不是直接保存的。这一字节是可以扩展,其保存机制,前7位用于保存长度,后一部用做标识。当最后一位为 1时,表示长度不足,需要使用二个字节继续保存。 例如:计算出后面的大小为0
2 MQTT可变头
MQTT数据包中包含一个可变头,它驻位于固定的头和负载之间。可变头的内容因数据包类型而不同,较常的应用是做为包的标识:
Bit | 7 6 5 4 3 2 1 0 |
---|---|
byte 1 | 包标签符(MSB) |
byte 2 | … 包标签符(LSB) |
很多类型数据包中都包括一个2字节的数据包标识字段,这些类型的包有:PUBLISH (QoS > 0)、PUBACK、PUBREC、PUBREL、PUBCOMP、SUBSCRIBE、SUBACK、UNSUBSCRIBE、UNSUBACK
3 Payload消息体
-
Payload消息体位MQTT数据包的第三部分,CONNECT、SUBSCRIBE、SUBACK、UNSUBSCRIBE四种类型的消息 有消息体:
- CONNECT,消息体内容主要是:客户端的ClientID、订阅的Topic、Message以及用户名和密码。
- SUBSCRIBE,消息体内容是一系列的要订阅的主题以及QoS。
- SUBACK,消息体内容是服务器对于SUBSCRIBE所申请的主题及QoS进行确认和回复。
- UNSUBSCRIBE,消息体内容是要订阅的主题。