Commit 0815616d authored by 957dd's avatar 957dd

修改了阻塞等待事件,可以让mqtt服务器增多

parent 47699a2f
cmake_minimum_required(VERSION 3.10)
project(car
VERSION 1.2.13
VERSION 1.2.14
LANGUAGES C
)
......
#define PROJECT_VERSION_MAJOR 1
#define PROJECT_VERSION_MINOR 2
#define PROJECT_VERSION_PATCH 13
#define PROJECT_VERSION_PATCH 14
#define GIT_HASH ""
#define BUILD_TIMESTAMP ""
#define BUILD_USER ""
No preview for this file type
......@@ -30,9 +30,6 @@ int mqtt_init() {
my_zlog_error("Failed to create Mosquitto client");
return -1;
}
//mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
//mosquitto_int_option(mosq1, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
//mosquitto_int_option(mosq2, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
return 0;
}
......@@ -87,22 +84,6 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
}
}
// 返回连接结果,0成功,非0失败,单个使用函数
// int mqtt_create(struct mosquitto *mosq) {
// mosquitto_reconnect_delay_set(mosq, 2, 10, true);
// mosquitto_connect_callback_set(mosq, on_connect);
// mosquitto_message_callback_set(mosq, on_message);
// mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
// int rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60);
// if (rc != MOSQ_ERR_SUCCESS) {
// my_zlog_warn("Failed to connect to broker: %s", mosquitto_strerror(rc));
// // 不销毁 mosq,等待调用者处理重连
// return rc;
// }
// return 0; // 成功
// }
int add_mqtt_create(const char *host, int port, int clients_count){
static int uuid_index =0;
......@@ -152,16 +133,6 @@ int add_mqtt_create(const char *host, int port, int clients_count){
return 0;
}
//阻塞型
// int mqtt_cycle(struct mosquitto *mosq) {
// // 这个函数会阻塞直到连接断开或错误返回
// int rc = mosquitto_loop_forever(mosq, -1, 1);
// if (rc != MOSQ_ERR_SUCCESS) {
// my_zlog_warn("mqtt loop error: %s", mosquitto_strerror(rc));
// return rc;
// }
// }
//mqtt清理
void mqtt_clean(){
// 清理
......@@ -177,7 +148,7 @@ void mqtt_clean(){
int mqtt_cycle() {//非阻塞型
while (1) {
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
int rc= mosquitto_loop(g_clients_t[i].mosq, 100, 1); // 每秒检查一次
int rc= mosquitto_loop(g_clients_t[i].mosq, 17, 1); // 每秒检查一次
if(rc != MOSQ_ERR_SUCCESS){
my_zlog_warn("服务器 %s:%d 断开,尝试重连...",
g_clients_t[i].host, g_clients_t[i].port);
......@@ -185,7 +156,6 @@ int mqtt_cycle() {//非阻塞型
}
}
delay_ms(300); // 防止空转
}
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment