MQTT基于QUIC接入示例

本文介绍一个基于QUIC协议实现MQTT接入(MQTT over QUIC)的设备端Demo工程,可以在弱网等情况下提高性能和接入体验。

使用前必读

本文操作步骤以普通用户权限为例。如果您在操作过程中涉及到管理员权限才能执行的操作,可尝试使用sudo命令执行。

重要

目前QUIC协议仅适用于华东2(上海)地域。

背景信息

QUIC(Quick UDP Internet Connections)是一种实验性传输层网络协议,提供与TLS/SSL相当的安全性,处理更多的连接、安全性和低延迟QUIC基于UDP拥有弱网性能,在丢包和网络延迟情况下仍可提供可用服务。

QUIC在应用程序层面可以实现不同的拥塞控制算法,不需要操作系统和内核支持,对比传统的TCP协议,提高了改造灵活性,并且可帮助解决在TCP协议遇到瓶颈的业务

工程下载

单击下载mqtt_over_quic

环境要求

  • 当前版本仅支持Linux操作系统。

  • 参考如下命令安装gitcmakelibevent

    sudo apt-get install git cmake libevent-dev

目录结构

├── cmake
├── CMakeLists.txt
├── CMakeOptions.txt
├── CODE_OF_CONDUCT.md
├── compile.sh
├── CONTRIBUTING.md
├── demo
├── docs
├── include
├── LICENSE
├── README.md
├── run_demo.sh
├── scripts
├── SECURITY.md
├── src
│   ├── common
│   ├── congestion_control
│   ├── http3
│   ├── mqtt
│   ├── tls
│   └── transport
├── xqc_build.sh
└── xqc_configure.h.in
  • src目录。

    • xquiccommoncongestion_controlhttp3tlstransport

    • mqtt:MQTT核心仓库,以及基于xquic通道的适配层。

  • demo目录。

    mqtt_basic_demo.c:Demo工程文件。

  • 执行脚本。

    • compile.sh:编译脚本。

    • run_demo.sh:运行Demo的脚本 。

Demo工程文件使用说明

  1. 在Demo工程文件mqtt_basic_demo.c中,将设备信息(ProductKeyDeviceNameDeviceSecret)修改为您的设备信息。

    #define PRODUCT_KEY      "产品ProductKey"
    #define DEVICE_NAME      "设备名称DeviceName"
    #define DEVICE_SECRET    "设备密钥DeviceSecret"
  2. Demo工程文件中的接入点和端口如下所示。

    char  *mqtt_host = "quic.itls.cn-shanghai.aliyuncs.com";
    ...
    uint16_t port = 5660
  3. 您可使用MQTT的API实现物联网业务。相关API文件,请参见./src/mqtt/core/aiot_mqtt_api.h

    1. 设备通过aiot_mqtt_connect建立连接。

    2. 设备通过aiot_mqtt_sub订阅Topic。

    3. 设备通过aiot_mqtt_pub发布消息。

使用方式

  1. 使用如下命令进行编译。

    sh ./compile.sh
  2. 使用如下命令运行Demo。

    sh ./run_demo.sh

测试和运行

编译日志

[ 92%] Linking C shared library libxquic.so
[ 93%] Linking C static library libxquic-static.a
[ 93%] Built target xquic-static
[ 93%] Built target xquic
[ 93%] Building C object CMakeFiles/demo_client.dir/demo/xqc_hq_ctx.c.o
[ 94%] Building C object CMakeFiles/demo_server.dir/demo/demo_server.c.o
[ 93%] Building C object CMakeFiles/mqtt_basic_demo.dir/demo/mqtt_basic_demo.c.o
[ 93%] Building C object CMakeFiles/demo_server.dir/demo/xqc_hq_ctx.c.o
[ 94%] Building C object CMakeFiles/demo_client.dir/demo/xqc_hq_conn.c.o
[ 94%] Building C object CMakeFiles/demo_client.dir/demo/xqc_hq_request.c.o
[ 95%] Building C object CMakeFiles/demo_client.dir/demo/demo_client.c.o
[ 96%] Building C object CMakeFiles/demo_server.dir/demo/xqc_hq_request.c.o
[ 97%] Building C object CMakeFiles/demo_server.dir/demo/xqc_hq_conn.c.o
[ 98%] Linking C executable mqtt_basic_demo
[ 99%] Linking C executable demo_server
[100%] Linking C executable demo_client
[100%] Built target mqtt_basic_demo
[100%] Built target demo_server
[100%] Built target demo_client

设备运行日志

[1677124290.795][LK-0313] MQTT user calls aiot_mqtt_connect api, connect
[1677124290.795][LK-032A] mqtt host: ***************
[1677124290.795][LK-0317] user name: devinf****asic_demo&gb***mX7yX
[1677124290.795][LK-0318] password: *****************************
doing network init
congestion control flags: 0
engine created done
try to start mqtt process thread
sessoin data read error or use_1rtt
xqc_conn_is_ready_to_send_early_data:0
xqc_stream_create ready to go
save_tp_cb use server domain as the key. h3[0]
====>DCID:fca4525cf73dc************8e5359dc5a02c73
====>SCID:a3e7*******6251299
done network establish
recvfrom size:1788
====>ping_id:1
====>no ping_id
recvfrom size:934
recvfrom size:34
[1677124291.611][LK-0313] MQTT connect success in 816 ms
AIOT_MQTTEVT_CONNECT
[1677124291.611][LK-0309] sub: /sys/gb8****7yX/devinfo_basic_demo/thing/event/property/post_reply
[1677124291.611][LK-0309] pub: /sys/gb8****7yX/devinfo_basic_demo/thing/event/property/post

[LK-030A] > 7B 22 69 64 22 3A 22 31  22 2C 22 76 65 72 73 69 | {"id":"1","versi
[LK-030A] > 6F 6E 22 3A 22 31 2E 30  22 2C 22 70 61 72 61 6D | on":"1.0","param
[LK-030A] > 73 22 3A 7B 22 4C 69 67  68 74 53 77 69 74 63 68 | s":{"LightSwitch
[LK-030A] > 22 3A 30 7D 7D                                   | ":0}}

recving rate: 27.273 Kbps
recvfrom size:31
recvfrom size:32
recvfrom size:32
recvfrom size:35
recvfrom size:149
recvfrom size:208
heartbeat response
[1677124292.612][LK-0309] pub: /sys/gb8****7yX/devinfo_basic_demo/thing/event/property/post

[LK-030A] > 7B 22 69 64 22 3A 22 31  22 2C 22 76 65 72 73 69 | {"id":"1","versi
[LK-030A] > 6F 6E 22 3A 22 31 2E 30  22 2C 22 70 61 72 61 6D | on":"1.0","param
[LK-030A] > 73 22 3A 7B 22 4C 69 67  68 74 53 77 69 74 63 68 | s":{"LightSwitch
[LK-030A] > 22 3A 30 7D 7D                                   | ":0}}

recving rate: 3.804 Kbps
recvfrom size:32
recvfrom size:150
recvfrom size:208
suback, res: -0x0000, packet id: 1, max qos: 1
[1677124293.413][LK-0309] pub: /sys/gb8*****7yX/devinfo_basic_demo/thing/event/property/post

[LK-030A] < 7B 22 69 64 22 3A 22 31  22 2C 22 76 65 72 73 69 | {"id":"1","versi
[LK-030A] < 6F 6E 22 3A 22 31 2E 30  22 2C 22 70 61 72 61 6D | on":"1.0","param
[LK-030A] < 73 22 3A 7B 22 4C 69 67  68 74 53 77 69 74 63 68 | s":{"LightSwitch
[LK-030A] < 22 3A 30 7D 7D                                   | ":0}}