Skip to content

Commit

Permalink
重命名回调函数名,避免与其它插件名称冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
blusewang committed Aug 4, 2022
1 parent ce42910 commit ebbc9c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
6 changes: 3 additions & 3 deletions handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "handler.h"
#include "hash.h"

int cb_message(int event, void *event_data, void *userdata) {
int handle_delay_message(int event, void *event_data, void *userdata) {
UNUSED(event);
UNUSED(userdata);
struct mosquitto_evt_message *msg = event_data;
Expand Down Expand Up @@ -71,7 +71,7 @@ int cb_message(int event, void *event_data, void *userdata) {

time_t tts;

int cb_tick(int event, void *event_data, void *userdata) {
int handle_delay_messge_tick(int event, void *event_data, void *userdata) {
UNUSED(event);
UNUSED(event_data);
UNUSED(userdata);
Expand All @@ -82,7 +82,7 @@ int cb_tick(int event, void *event_data, void *userdata) {
char *cts = ctime(&ts);
struct delay_message_array arr = get(cts);
if (arr.length > 0) {
// mosquitto_log_printf(MOSQ_LOG_NOTICE, "cb_tick got topic:%s, len:%d, msg:%s", arr.arr[0].topic,
// mosquitto_log_printf(MOSQ_LOG_NOTICE, "handle_delay_messge_tick got topic:%s, len:%d, msg:%s", arr.arr[0].topic,
// strlen(arr.arr[0].topic),
// arr.arr[0].payload);
for (int i = 0; i < arr.length; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

extern int max_delay;

int cb_tick(int event, void *event_data, void *userdata);
int handle_delay_messge_tick(int event, void *event_data, void *userdata);

int cb_message(int event, void *event_data, void *userdata);
int handle_delay_message(int event, void *event_data, void *userdata);

#endif //MOSQUITTO_DELAY_MESSAGE_DELAY_MESSAGE_H
29 changes: 21 additions & 8 deletions plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struc
mosquitto_log_printf(MOSQ_LOG_DEBUG, "%s -> %s %d", opts->key, opts->value, strcmp(opts->key, "max_delay"));
if (strcmp(opts->key, "max_delay") == 0) {
max_delay = atoi(opts->value);
if (max_delay > 600) {
return MOSQ_ERR_UNKNOWN;
} else if (max_delay < 6) {
if (max_delay > 600 || max_delay < 6) {
return MOSQ_ERR_UNKNOWN;
}
mosquitto_log_printf(MOSQ_LOG_DEBUG, "maxDelay -> %d", max_delay);
Expand All @@ -40,17 +38,32 @@ mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struc
}

mos_pid = identifier;
mosquitto_callback_register(mos_pid, MOSQ_EVT_TICK, cb_tick, NULL, NULL);
mosquitto_callback_register(mos_pid, MOSQ_EVT_MESSAGE, cb_message, NULL, NULL);

int rs = mosquitto_callback_register(mos_pid, MOSQ_EVT_TICK, handle_delay_messge_tick, NULL, NULL);
if (rs != MOSQ_ERR_SUCCESS) {
mosquitto_log_printf(MOSQ_LOG_ERR, "mosquitto_callback_register MOSQ_EVT_TICK err:%d", rs);
return rs;
}
rs = mosquitto_callback_register(mos_pid, MOSQ_EVT_MESSAGE, handle_delay_message, NULL, NULL);
if (rs != MOSQ_ERR_SUCCESS) {
mosquitto_log_printf(MOSQ_LOG_ERR, "mosquitto_callback_register MOSQ_EVT_MESSAGE err:%d", rs);
return rs;
}
return MOSQ_ERR_SUCCESS;
}


int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count) {
if (mos_pid) {
mosquitto_callback_unregister(mos_pid, MOSQ_EVT_TICK, cb_tick, NULL);
mosquitto_callback_unregister(mos_pid, MOSQ_EVT_MESSAGE, cb_message, NULL);
int rs = mosquitto_callback_unregister(mos_pid, MOSQ_EVT_TICK, handle_delay_messge_tick, NULL);
if (rs != MOSQ_ERR_SUCCESS) {
mosquitto_log_printf(MOSQ_LOG_ERR, "mosquitto_callback_unregister MOSQ_EVT_MESSAGE err:%d", rs);
return rs;
}
rs = mosquitto_callback_unregister(mos_pid, MOSQ_EVT_MESSAGE, handle_delay_message, NULL);
if (rs != MOSQ_ERR_SUCCESS) {
mosquitto_log_printf(MOSQ_LOG_ERR, "mosquitto_callback_unregister MOSQ_EVT_MESSAGE err:%d", rs);
return rs;
}
}
return MOSQ_ERR_SUCCESS;
}

0 comments on commit ebbc9c8

Please sign in to comment.