Merge pull request #678 from gc87/buf-malloc

refactor(mqtt): static alloc -> dynamic alloc
This commit is contained in:
fengzeroz
2022-07-29 16:09:05 +08:00
committed by GitHub
2 changed files with 28 additions and 9 deletions
+8 -3
View File
@@ -28,6 +28,7 @@
#define INTERVAL 100000U
#define TIMEOUT 3000U
#define MAX_TOPIC_LEN 256
#define TOPIC_READ_REQ "neuron/%s/read/req"
#define TOPIC_WRITE_REQ "neuron/%s/write/req"
@@ -64,13 +65,17 @@ struct mqtt_routine {
static char *topics_format(char *format, char *name)
{
char temp[256] = { '\0' };
int rc = snprintf(temp, 256, format, name);
char *topic = calloc(1, MAX_TOPIC_LEN);
if (NULL == topic) {
return NULL;
}
int rc = snprintf(topic, MAX_TOPIC_LEN, format, name);
if (-1 != rc) {
char *topic = strdup(temp);
return topic;
}
free(topic);
return NULL;
}
+20 -6
View File
@@ -53,8 +53,8 @@ struct reconnect_state {
char * key_file;
char * keypass;
mqtt_c_client_t *client;
uint8_t send_buf[SEND_BUF_SIZE];
uint8_t recv_buf[RECV_BUF_SIZE];
uint8_t * send_buf;
uint8_t * recv_buf;
size_t send_buf_sz;
size_t recv_buf_sz;
X509 * cert;
@@ -410,6 +410,14 @@ static void reconnect_state_uninit(struct reconnect_state *state)
if (NULL != state->keypass) {
free(state->keypass);
}
if (NULL != state->send_buf) {
free(state->send_buf);
}
if (NULL != state->recv_buf) {
free(state->recv_buf);
}
}
static void subscribe_on_reconnect(mqtt_c_client_t *client)
@@ -584,6 +592,14 @@ static mqtt_c_client_t *client_create(const neu_mqtt_option_t *option,
return NULL;
}
struct reconnect_state *state = &client->state;
state->send_buf = calloc(1, SEND_BUF_SIZE);
state->recv_buf = calloc(1, RECV_BUF_SIZE);
if (NULL == state->send_buf || NULL == state->recv_buf) {
zlog_error(option->log, "mqtt buff alloc error");
return NULL;
}
client->option = option;
client->user_data = context;
client->state_update_func = client->option->state_update_func;
@@ -591,8 +607,6 @@ static mqtt_c_client_t *client_create(const neu_mqtt_option_t *option,
UT_icd tuple_icd = { sizeof(struct subscribe_tuple *), NULL, NULL, NULL };
utarray_new(client->array, &tuple_icd);
struct reconnect_state *state = &client->state;
if (NULL != client->option->clientid) {
state->client_id = strdup(client->option->clientid);
}
@@ -644,8 +658,8 @@ static mqtt_c_client_t *client_create(const neu_mqtt_option_t *option,
}
state->client = client;
state->send_buf_sz = sizeof(state->send_buf);
state->recv_buf_sz = sizeof(state->recv_buf);
state->send_buf_sz = SEND_BUF_SIZE;
state->recv_buf_sz = RECV_BUF_SIZE;
state->cert = NULL;
state->key = NULL;
state->ssl_ctx = NULL;