1 Star 1 Fork 0

jf/mosquitto-delay-message

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hash.c 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
jf 提交于 2022-08-04 10:17 +08:00 . 使用mosquitto提供的内存管理函数
//
// Created by bluse on 2022/7/31.
//
#include <mosquitto.h>
#include <mosquitto_broker.h>
#include "hash.h"
#include "uthash.h"
void set(const char *key, struct delay_message msg) {
if (key == NULL) {
return;
}
struct dataItem *r;
HASH_FIND_STR(pData, key, r);
if (r == NULL) {
r = mosquitto_malloc(sizeof(struct dataItem));
r->key = key;
r->arr = mosquitto_malloc(sizeof(struct delay_message));
r->arr[0] = msg;
r->count = 1;
HASH_ADD_STR(pData, key, r);
} else {
r->count += 1;
struct delay_message *arr = mosquitto_calloc(r->count, sizeof(struct delay_message));
for (int i = 0; i < r->count; ++i) {
if (i < r->count - 1) {
arr[i] = r->arr[i];
} else {
arr[i] = msg;
}
}
mosquitto_free(r->arr);
r->arr = arr;
}
}
struct delay_message_array get(const char *key) {
if (key == NULL) {
return (struct delay_message_array) {NULL, 0};
}
struct dataItem *i;
HASH_FIND_STR(pData, key, i);
if (i == NULL) {
return (struct delay_message_array) {NULL, 0};
}
struct delay_message_array arr = {i->arr, i->count};
return arr;
}
void del(const char *key) {
if (key == NULL) {
return;
}
struct dataItem *r;
HASH_FIND_STR(pData, key, r);
if (r != NULL) {
HASH_DEL(pData, r);
// 完整清理
for (int i = 0; i < r->count; ++i) {
mosquitto_free(r->arr[i].topic);
mosquitto_free(r->arr[i].payload);
}
mosquitto_free(r->arr);
r->arr = NULL;
mosquitto_free(r);
r = NULL;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/bluse/mosquitto-delay-message.git
git@gitee.com:bluse/mosquitto-delay-message.git
bluse
mosquitto-delay-message
mosquitto-delay-message
main

搜索帮助