1 / 21
文档名称:

mosquitto源码分析.doc

格式:doc   大小:439KB   页数:21页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

mosquitto源码分析.doc

上传人:薄荷牛奶 2022/3/14 文件大小:439 KB

下载得到文件列表

mosquitto源码分析.doc

相关文档

文档介绍

文档介绍:mosquitto源码分析
 本文由逍遥子撰写,转发请标注原址:
://
一、  Mosquitto简介
mosquitto      |----python
         |----python3
     |---- ssl
         |----demoCA
         |----rootCA
         |----signingCA       
所需关注的目录有/ mosquitto-、/ mosquitto-、/ mosquitto-,其中src和lib目录下主要放置mosquitto的实现代码以及局部底层与网络相关的操作,client目录主要为两个客户端程序的实现源码。
Mosquito的源码及其相关文档可从其官方网站获取,其官方网站为: :///
mosquitto客户端和效劳器运行命令
[1] 发布者客户端运行命令例如:
./mosquitto_pub -h -p 1883 -t "111" -m "this is " -u 111 -P 111
[2] 订阅者客户端运行命令例如:
./mosquitto_sub -h -i 111 -p 1883 -t 111 -k 60 -d -c -u hjx -P hjx
[3] mosquitto效劳器端运行命令例如:
./mosquitto
二、  Mosquito的数据结构
1〕  struct mosquito
结构体struct mosquito主要用于保存一个客户端连接的所有信息,例如用户名、密码、用户ID、向该客户端发送的消息等,其定义为:
struct mosquitto {
   int sock;
   char*address;
   char *id;
   char*username;
   char*password;
   uint16_tkeepalive;
   time_tlast_msg_in;
   time_tlast_msg_out;
struct mosquitto_client_msg *msgs;

}
上面列举了该结构体局部重要成员,其中sock表示mosquitto效劳器程序与该客户端连接通信所用的socket描述符;address表示该客户端的IP地址;id是该客户端登陆mosquitto程序时所提供的ID值,该值与其他的客户端不能重复;成员username和password用于记录客户端登陆时所提供的用户名和密码;keepalive是该客户端需在此时间内向mosquitto效劳器程序发送一条ping/pong消息。参数last_msg_in和last_msg_out用于记录上次收发消息的时间;参数struct mosquitto_client_msg*msgs用于暂时存储发往该context的消息。
2〕  struct mosquitto_db
结构体struct mosquitto_db是mosquitto对所有内部数据的统一管理结构,可以认为是其内部的一个内存数据库。它保存了所有的客户端,所有客户端的订阅关系等等,其定义形式为:
struct mosquitto_db{
   dbid_tlast_db_id;
   struct_mosquitto_subhier subs;
   struct mosquitto**contexts;
   struct_clientid_index_hash *clientid_index_hash;
   intcontext_count;
   structmosquitto_msg_store *msg_store;
   intmsg_store_count;
   structmqtt3_config *config;
   intsubscription_count;
……
};
上述结构体声明中,结构体成员struct _mosquitto_subhier subs保存了订阅树的总树根,mosquitto中对所有的topic都在该订阅树中维护,客户端的订阅关系也在该订阅树中维护;结构体成员struct mosquitto **contexts可理解为一个用于存放所有客户端变量〔类型为struct mosquitto〕地址的数组,mosquitto程序中