1 Star 0 Fork 45

chxj / mqttclient

forked from Gitee 极速下载 / mqttclient 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

license

English Documents

mqttclient

一个高性能、高稳定性的跨平台MQTT客户端

一个高性能、高稳定性的跨平台MQTT客户端,基于socket API之上开发,可以在嵌入式设备(FreeRTOS/LiteOS/RT-Thread/TencentOS tiny)、Linux、Windows、Mac上使用,拥有非常简洁的API接口,以极少的资源实现QOS2的服务质量,并且无缝衔接了mbedtls加密库。

优势:

  • 具有极高的稳定性:无论是掉线重连丢包重发,都是严格遵循MQTT协议标准执行,除此之外对大数据量的测试无论是收是发,都是非常稳定,高频测试也是非常稳定。

  • 轻量级:整个代码工程极其简单,不使用mbedtls情况下,占用资源极少,作者曾使用esp8266模组与云端通信,整个工程代码消耗的RAM不足15k。

  • 支持mbedtls加密传输,让网络传输更加安全,而且接口层完全不需要用户理会,无论是否加密,mqttclient对用户提供的API接口是固定的,这就很好的兼容了一套代应用层的码可以加密传输也可以不加密传输。

  • 支持多客户端,同时兼容多个客户端运行工作,一个设备连接多个的服务器。

  • 支持同步异步处理,应用程序无需阻塞等待浪费CPU资源。

  • 支持拦截器配置,在某些平台中,客户端会默认自动订阅系统主题,而且每次来自服务器的主题都是变动的,此时需要使用拦截器进行拦截,分离主题与数据信息,并递交给用户,极大提高灵活性。

  • 拥有在线代码生成工具,极其简单配置就可以生成对应的代码,地址:https://jiejietop.gitee.io/mqtt/index.html

  • 拥有极简的API接口,总的来说,mqttclient的配置都有默认值,基本无需配置都能使用的,也可以随意配置,对配置都有健壮性检测,这样子设计的API接口也是非常简单。

  • 多功能参数可配置可裁剪,重连时间间隔、心跳周期、最大订阅数量、命令超时、读写缓冲区大小、拦截器处理等等参数功能可裁剪可配置,满足开发者再复杂、简单等各种开发环境下的使用需求。

  • 支持自动重新订阅主题,在自动重连后保证主题不会丢失。

  • 支持主题通配符“#”、“+”

  • 订阅的主题与消息处理完全分离,让编程逻辑更加简单易用,用户无需理会错综复杂的逻辑关系。

  • mqttclient内部已实现保活处理机制,无需用户过多关心理会,用户只需专心处理应用功能即可。

  • 拥有非常好的设计,以极少的资源设计了记录机制,当报文在丢失的时候进行重发操作,确保qos1、qos2服务质量等级的报文保证其服务质量。

  • 有非常好的代码风格与思想:整个代码采用分层式设计,代码实现采用异步处理的思想,降低耦合,提高性能。

  • 基于标准BSD socket之上开发,只要是兼容BSD socket的系统均可使用。

  • 无缝衔接salof:它是一个同步异步日志输出框架,在空闲时候输出对应的日志信息,也可以将信息写入flash中保存,方便调试。

  • 使用著名的 paho mqtt 库封包

  • 无其他依赖

在线代码生成工具

本项目拥有代码生成工具,只需要在线配置即可生成代码,极其简单易用,代码生成工具地址:https://jiejietop.gitee.io/mqtt/index.html

在线代码生成工具

占用资源大小

总计占 10857字节 的ROM,而RAM的开销则几乎只依赖动态内存,在不使用TLS加密传输的情况下,维持QOS0服务质量等级的通信动态内存大约仅需要3694字节,这包括1024读缓冲区 + 1024写缓冲区 + 1024内部线程栈大小,相对于其他MQTT客户端来说,mqttclient需要的RAM资源开销极少。

Code RO Data RW Data ZI Data Object Name
7118 791 0 0 mqttclient.o
546 0 0 0 mqttconnectclient.o
212 0 0 0 mqttdeserializepublish.o
476 0 4 0 mqttpacket.o
236 0 0 0 mqttserializepublish.o
310 0 0 0 mqttsubscribeclient.o
38 0 0 0 mqttunsubscribeclient.o
56 0 0 0 nettype_tcp.o
62 0 0 0 network.o
24 0 0 0 platform_memory.o
40 0 0 0 platform_mutex.o
344 0 0 0 platform_net_socket.o
94 0 0 0 platform_thread.o
70 0 0 0 platform_timer.o
246 0 4 0 random.o
62 0 0 0 mqtt_list.o
- - - - -
10066 791 8 0 total

整体框架

拥有非常明确的分层框架。

整体框架

  • 在框架的最上方是API函数接口,实现了客户端的申请、释放、设置参数、连接服务器、断开连接、订阅主题、取消订阅主题、发布消息等函数接口。

  • 使用了著名的paho mqtt库作为MQTT报文封包库。

  • 采用异步处理的机制去管理所有的ack,它在发送报文的时候不用等待服务器的应答,只是记录下来,在收到服务器的ack后,取消这个记录,效率极高;而在发送mqtt报文(QoS1/QoS2)的时候没有收到服务器的应答时,将重发该报文。

  • 内部实现了一个mqtt yield线程,统一处理所有的内容,比如超时的处理、ack报文的处理、收到来自服务器的publish报文,此时会去调用回调函数告知用户收到的数据,发布释放、发布完成报文的处理、心跳报文(保持活性),当与服务器断开连接时需要尝试重连、重新订阅主题,重新发送报文或者应答等。

  • 报文的处理,如读写报文、解码mqtt报文、设置报文(dup标记)、销毁报文等操作。

  • network是网络组件,它可以自动选择数据通道,如果是加密方式则通过tls加密进行数据传输,而tls可以选择mbedtls作为加密后端;也可以是tcp直连方式,最终都是通过tcp传输的。

  • platform是平台抽象层,封装了不同系统的东西,比如socke或者AT,线程、时间、互斥锁、内存管理,这些是与系统打交道的,也是跨平台必要的封装。

  • 最右边的则是通用的内容,list的处理、日志库、错误处理、软件随机数发生器等。

支持的平台

目前已实现了Linux、TencentOS tiny、FreeRTOS、RT-Thread平台(已做成软件包,名字为kawaii-mqtt),除此之外TencentOS tiny的AT框架亦可以使用,并且稳定性极好!

平台 代码位置
Linux https://github.com/jiejieTop/mqttclient
TencentOS tiny https://github.com/Tencent/TencentOS-tiny/tree/master/board/Fire_STM32F429
TencentOS tiny AT 框架 https://github.com/jiejieTop/gokit3-board-mqttclient
RT-Thread https://github.com/jiejieTop/kawaii-mqtt
FreeRTOS https://github.com/jiejieTop/freertos-mqttclient

版本

发布版本 描述
[v1.0.0] 初次发布,完成基本框架及其稳定性验证
[v1.0.1] 修复主动与服务器断开连接时的逻辑处理
[v1.0.2] 添加新特性——拦截器,修复一些小bug
[v1.0.3] 避免造成全局污染修改了log、list相关函数的命名
[v1.0.4] 重新调整了network结构与mbedtls数据通道
[v1.1.0] 一个较大版本的更新,重构部分代码,优化MQTT处理的逻辑,提升整体的稳定性,支持多客户端,支持设置遗嘱,优化API接口,增加多个云平台的测试代码与说明文档,增加在线代码生成工具、在线裁剪配置工具

问题

欢迎以 GitHub Issues 的形式提交问题和bug报告

版权和许可

mqttclient 遵循 Apache License v2.0 开源协议。鼓励代码共享和尊重原作者的著作权,可以自由的使用、修改源代码,也可以将修改后的代码作为开源或闭源软件发布。

linux平台下测试使用

安装cmake:

    sudo apt-get install cmake g++

测试程序

测试平台 位置
emqx(我私人部署的服务器) ./test/emqx/test.c
百度天工 ./test/baidu/test.c
onenet ./test/onenet/test.c
阿里云物联 ./test/ali/test.c

编译 & 运行

    ./build.sh

运行build.sh脚本后会在 ./build/bin/目录下生成可执行文件emqxbaiduonenet等多个平台的可执行程序,直接运行即可。

    ./build/bin/emqx 

编译成动态库libmqttclient.so

    ./make-libmqttclient.sh

运行make-libmqttclient.sh脚本后会在 ./libmqttclient/lib目录下生成一个动态库文件libmqttclient.so,并安装到系统的/usr/lib 目录下,相关头文件已经拷贝到./libmqttclient/include目录下,将其拷贝到你的工程中即可,编译应用程序的时候只需要链接动态库即可-lmqttclient -lpthread,动态库的配置文件是根据./test/mqtt_config.h配置的。

如果你是用交叉编译器的话,则应该根据你使用的编译器导出对应的环境变量(此处使用的交叉编译器是arm-linux-gnueabihf-gcc),同时你也必须将动态库文件libmqttclient.so拷贝到你的嵌入式系统的/usr/lib 目录下:

    export CROSS_COMPILE=arm-linux-gnueabihf- 

如若需要卸载libmqttclient.so,执行以下命令即可:

    ./make-libmqttclient.sh remove

学习更多

请看文档:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

一个高性能、高稳定性的跨平台 MQTT 客户端,基于 socket API 开发,可以在嵌入式设备(FreeRTOS/LiteOS/RT-Thread/TencentOS tiny 展开 收起
C/C++
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C/C++
1
https://gitee.com/chxj8080/mqttclient.git
git@gitee.com:chxj8080/mqttclient.git
chxj8080
mqttclient
mqttclient
master

搜索帮助

14c37bed 8189591 565d56ea 8189591