mosquitto 原始碼分析 (一)核心資料結構
阿新 • • 發佈:2019-01-16
mosquitto 原始碼中核心資料結構分析
一、struct mosquitto (在mosquitto_internel.h中定義)
struct mosquitto 這個結構體用來儲存一個客戶端連線的所有資訊
這個結構體包含了很多成員,我們選取一些重要的成員,在程式碼中標註:
struct mosquitto {
#ifndef WIN32
int sock; /*伺服器程式與該客戶端連線通訊所用的socket描述符*/
# ifndef WITH_BROKER
int sockpairR, sockpairW;
# endif
#else
SOCKET sock;
# ifndef WITH_BROKER
SOCKET sockpairR, sockpairW;
# endif
#endif
enum _mosquitto_protocol protocol;
char *address;/*該客戶端的IP地址 */
char *id;/*該客戶端登陸mosquitto程式時所提供的ID值,該值與其他的客戶端不能重複*/
char *username;/*登入使用者名稱*/
char *password;/*密碼*/
uint16_t keepalive;/*該客戶端需在此時間內向mosquitto伺服器程式傳送一條ping/pong訊息*/
uint16_t last_mid;
enum mosquitto_client_state state;
time_t last_msg_in;/*last_msg_in和last_msg_out用於記錄上次收發訊息的時間*/
time_t last_msg_out;
time_t ping_t;
struct _mosquitto_packet in_packet;
struct _mosquitto_packet *current_out_packet;
struct _mosquitto_packet *out_packet;
struct mosquitto_message *will;
...
bool want_write;
bool want_connect;
#if defined(WITH_THREADING) && !defined(WITH_BROKER)
pthread_mutex_t callback_mutex;
pthread_mutex_t log_callback_mutex;
pthread_mutex_t msgtime_mutex;
pthread_mutex_t out_packet_mutex;
pthread_mutex_t current_out_packet_mutex;
pthread_mutex_t state_mutex;
pthread_mutex_t in_message_mutex;
pthread_mutex_t out_message_mutex;
pthread_t thread_id;
#endif
bool clean_session;
#ifdef WITH_BROKER
bool is_dropping;
bool is_bridge;
struct _mqtt3_bridge *bridge;
struct mosquitto_client_msg *msgs;/*用於暫時儲存發往該context的訊息。 */
struct mosquitto_client_msg *last_msg;
int msg_count;
int msg_count12;
struct _mosquitto_acl_user *acl_list;
struct _mqtt3_listener *listener;
time_t disconnect_t;
struct _mosquitto_packet *out_packet_last;
struct _mosquitto_subhier **subs;
int sub_count;
int pollfd_index;
...
};
二、struct mosquitto_db (定義在mosquitto_broker.h)
struct mosquitto_db 結構體定義了對所有內部資料的統一管理,儲存了所有客戶端,訂閱關係。可以認為是一個資料倉庫。
struct mosquitto_db{
dbid_t last_db_id;
struct _mosquitto_subhier subs;/*訂閱樹的總樹根*/
struct _mosquitto_unpwd *unpwd;
struct _mosquitto_acl_user *acl_list;
struct _mosquitto_acl *acl_patterns;
struct _mosquitto_unpwd *psk_id;
struct mosquitto *contexts_by_id; /*所有的客戶端都在此陣列中儲存*/
struct mosquitto *contexts_by_sock;
struct mosquitto *contexts_for_free;
#ifdef WITH_BRIDGE
struct mosquitto **bridges;
#endif
struct _clientid_index_hash *clientid_index_hash;
struct mosquitto_msg_store *msg_store;
struct mosquitto_msg_store_load *msg_store_load;
#ifdef WITH_BRIDGE
int bridge_count;
#endif
int msg_store_count;
struct mqtt3_config *config; /*儲存配置資訊*/
int persistence_changes;
struct _mosquitto_auth_plugin auth_plugin;
#ifdef WITH_SYS_TREE
int subscription_count;
int retained_count;
#endif
struct mosquitto *ll_for_free;
};
三、struct _mosquitto_subhier(定義在mosquitto_broker.h)
用來儲存訂閱樹的所有節點,mosquitto中對訂閱樹採用孩子-兄弟連結串列法的方式進行儲存
struct _mosquitto_subhier {
struct _mosquitto_subhier *children;/*第一個孩子節點*/
struct _mosquitto_subhier *next;/*指向該節點的下一個兄弟節點*/
struct _mosquitto_subleaf *subs;/*指向訂閱列表*/
char *topic;/*訂閱主題*/
struct mosquitto_msg_store *retained;
};
四、struct _mosquitto_subleaf (定義在mosquitto_broker.h)
對某一topic的所有訂閱者被組織成一個訂閱列表,該訂閱列表是一個雙向連結串列,連結串列的每個節點都儲存有一個訂閱者
struct _mosquitto_subleaf {
struct _mosquitto_subleaf *prev;/*前指標*/
struct _mosquitto_subleaf *next;/*後指標*/
struct mosquitto *context;/*表示一個訂閱客戶端*/
int qos;/* Qos Level */
};
五、struct mqtt3_config (定義在mosquitto_broker.h)
儲存mosquitto的所有配置資訊,mosquitto程式在啟動時將初始化該結構體並從配置檔案中讀取配置資訊保存於該結構體變數內。
以上暫且先列出一些核心資料結構,其他資料結構用到時在分析。下篇結合使用方法來分析main程式。