zoukankan      html  css  js  c++  java
  • 转:mosquitto源码分析(二)

     本文由逍遥子撰写,转发请标注原址:

    http://write.blog.csdn.NET/postedit/21462005

    一、  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程序中,所有的客户端都在此数组中保存;成员int context_count用于保存数组contexts的大小,该值也是当前mosquitto程序中维持的所有客户端的数目;成员结构体struct _clientid_index_hash*clientid_index_hash用于保存一个hash表,该hash表可通过客户端的ID快速找到该客户端在数组contexts中的索引;结构体成员struct mqtt3_config*config用于保存mosquitto的所有配置信息;

    3)struct_mosquitto_subhier

    数据结构struct _mosquitto_subhier是用于保存订阅树的节点(包括叶子节点和中间节点),mosquitto中对订阅树采用孩子-兄弟链表法的方式进行存储,该存储方式主要借助与数据结构struct _mosquitto_subhier来完成,该数据结构的定义为:

    struct _mosquitto_subhier {

       struct_mosquitto_subhier *children;

       struct_mosquitto_subhier *next;

       struct_mosquitto_subleaf *subs;

       char*topic;

       structmosquitto_msg_store *retained;

    };

    成员说明:

    children :该成员指针指向同结构的第一个孩子节点;

    next:该成员指针指向该节点的下一个兄弟节点;

     subs:该成员指向订阅列表;

    topic:该节点对应的topic片段;

    3)  struct _mosquitto_subleaf

    在mosquitto程序中,对某一topic的所有订阅者被组织成一个订阅列表,该订阅列表是一个双向链表,链表的每个节点都保存有一个订阅者,该链表的节点即是:struct _mosquitto_subleaf,定义形式为:

    struct _mosquitto_subleaf {

       struct_mosquitto_subleaf *prev;

       struct_mosquitto_subleaf *next;

       structmosquitto *context;

       int qos;

    };

    其中成员struct mosquitto *context表示一个订阅客户端,prev和next表示其前一个节点和后一个节点。

    6)structmqtt3_config

    结构体struct mqtt3_config用于保存mosquitto的所有配置信息,mosquitto程序在启动时将初始化该结构体并从配置文件中读取配置信息保存于该结构体变量内。

  • 相关阅读:
    洛谷P1084 [NOIP2012提高组Day2T3]疫情控制
    洛谷P1083 [NOIP2012提高组Day2T2]借教室
    洛谷P2736 “破锣摇滚”乐队 Raucous Rockers
    POJ1692 Crossed Matchings
    洛谷P1315 [NOIP2011提高组Day2T3] 观光公交
    阅读了几个别人写的轮播源码
    js遍历函数
    解决IE6的PNG透明
    04-树5 Root of AVL Tree
    平衡树实现
  • 原文地址:https://www.cnblogs.com/killer-xc/p/6775557.html
Copyright © 2011-2022 走看看