MQTT解析

Java

package com.qqbot;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class MqttEntityEncoder {

    public static byte[] encode(MqttEntity entity) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        // 1. mdcSn[20]
        writeFixedLengthString(dos, entity.getMdcSn(), 20);

        // 2. length = 54 + msgLen
        int length = 54 + (entity.getMsgBody() == null ? 0 : entity.getMsgBody().length);
        dos.writeInt(Integer.reverseBytes(length)); // 小端写入

        // 3. timeStamp (uint64_t)
        dos.writeLong(Long.reverseBytes(entity.getTimeStamp()));

        // 4. transId[36]
        writeFixedLengthString(dos, entity.getTransId(), 36);

        // 5. msgType (uint16_t)
        dos.writeShort(Short.reverseBytes(entity.getMsgType()));

        // 6. msgLen (uint32_t)
        int msgLen = entity.getMsgBody() == null ? 0 : entity.getMsgBody().length;
        dos.writeInt(Integer.reverseBytes(msgLen));

        // 7. protVer (uint32_t)
        dos.writeInt(Integer.reverseBytes(entity.getProtVer()));

        // 8. msgBody[msgLen]
        if (entity.getMsgBody() != null) {
            dos.write(entity.getMsgBody());
        }

        dos.flush();
        return baos.toByteArray();
    }

    private static void writeFixedLengthString(DataOutputStream dos, String value, int fixedLength) throws IOException {
        byte[] bytes = value != null ? value.getBytes(StandardCharsets.UTF_8) : new byte[0];
        if (bytes.length > fixedLength) {
            dos.write(bytes, 0, fixedLength); // 截断
        } else {
            dos.write(bytes);                 // 写入实际内容
            for (int i = 0; i < fixedLength - bytes.length; i++) {
                dos.writeByte(0);             // 补0
            }
        }
    }

    public static void main(String[] args) throws IOException {
        MqttEntity entity = new MqttEntity();
        entity.setMdcSn("ABC123EFGGGHDDD");
        entity.setTimeStamp(System.currentTimeMillis());
        entity.setTransId("123e4567-e89b-12d3-a456");
        entity.setMsgType((short) 1);
        entity.setProtVer(1);
        entity.setMsgBody("hello mqtt test ddd".getBytes(StandardCharsets.UTF_8));

        byte[] payload = MqttEntityEncoder.encode(entity);
// payload 可直接通过 MQTT 或 TCP 发送
        System.out.println(payload);

    }
}

C++

#include <iostream>
#include <cstdint>
#include <cstring>

#pragma pack(push, 1)
struct MqttStruct {
    char mdcSn[20];
    uint32_t length;
    uint64_t timeStamp;
    char transId[36];
    uint16_t msgType;
    uint32_t msgLen;
    uint32_t protVer;
    // msgBody follows
};
#pragma pack(pop)

int main() {
    uint8_t data[] = {
        65, 66, 67, 49, 50, 51, 69, 70, 71, 71, 71, 72, 68, 68, 68, 0, 0, 0, 0, 0, // mdcSn
        73, 0, 0, 0,                                                             // length = 73
        33, 227, 216, 122, 152, 1, 0, 0,                                         // timeStamp (小端)
        49, 50, 51, 101, 52, 53, 54, 55, 45, 101, 56, 57, 98, 45, 49, 50, 
        100, 51, 45, 97, 52, 53, 54, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                                   // transId 补齐到 36
        1, 0,                                                                    // msgType
        19, 0, 0, 0,                                                             // msgLen = 19
        1, 0, 0, 0,                                                              // protVer
        104, 101, 108, 108, 111, 32, 109, 113, 116, 116, 32, 116, 101, 115, 116, 32, 100, 100, 100
    };

    MqttStruct header{};
    std::memcpy(&header, data, sizeof(MqttStruct));

    std::cout << "mdcSn: " << std::string(header.mdcSn, 20) << std::endl;
    std::cout << "length: " << header.length << std::endl;
    std::cout << "timestamp: " << header.timeStamp << std::endl;
    std::cout << "transId: " << std::string(header.transId, 36) << std::endl;
    std::cout << "msgType: " << header.msgType << std::endl;
    std::cout << "msgLen: " << header.msgLen << std::endl;
    std::cout << "protVer: " << header.protVer << std::endl;

    const char* msgBody = reinterpret_cast<const char*>(data + sizeof(MqttStruct));
    std::cout << "msgBody: " << std::string(msgBody, header.msgLen) << std::endl;

    return 0;
}

MQTT(Message Queuing Telemetry Transport)是一种轻量级的通讯协议,常用于物联网设备之间的通信。下面是一个简单的MQTT解析包的代码示例。 ```python import struct def parse_mqtt_packet(packet): # 解析固定报头的第一个字节,获取报文类型和标志位 packet_type = packet[0] >> 4 flags = packet[0] & 0x0F print("Packet Type:", packet_type) print("Flags:", flags) # 解析剩余长度字段 remaining_length = 0 multiplier = 1 index = 1 while True: digit = packet[index] remaining_length += (digit & 127) * multiplier multiplier *= 128 index += 1 if (digit & 128) == 0: break print("Remaining Length:", remaining_length) # 解析可变报头和负载数据 if packet_type == 1: # 连接请求报文 protocol_name_length = struct.unpack("!H", packet[index:index+2])[0] index += 2 protocol_name = packet[index:index+protocol_name_length].decode("utf-8") index += protocol_name_length print("Protocol Name:", protocol_name) protocol_version = packet[index] index += 1 print("Protocol Version:", protocol_version) flags = packet[index] index += 1 print("Flags:", flags) keep_alive = struct.unpack("!H", packet[index:index+2])[0] index += 2 print("Keep Alive:", keep_alive) client_id_length = struct.unpack("!H", packet[index:index+2])[0] index += 2 client_id = packet[index:index+client_id_length].decode("utf-8") index += client_id_length print("Client ID:", client_id) elif packet_type == 3: # 发布报文 topic_length = struct.unpack("!H", packet[index:index+2])[0] index += 2 topic = packet[index:index+topic_length].decode("utf-8") index += topic_length print("Topic:", topic) message = packet[index:] print("Message:", message) # 解析完整个报文后,可以根据需要进行其他操作,如校验报文合法性、执行相应的逻辑等。 # 测试示例 packet = b"\x10\xCC\x00\x04\x4D\x51\x54\x54\x04\x02\x00\x0A\x4D\x79\x43\x6C\x69\x65\x6E\x74\x49\x44" parse_mqtt_packet(packet) ``` 以上代码是一个简单的MQTT解析包示例,用于解析MQTT连接请求和发布报文。函数`parse_mqtt_packet`接收一个字节数组作为参数,通过解析字节数组中的字段,可以获取报文类型、标志位、剩余长度、可变报头和负载数据等信息。可以根据业务需要,进一步扩展代码以支持其他MQTT报文类型的解析
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值