Skip to content

Commit

Permalink
Fix library reinitialise with packet buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Nov 20, 2024
1 parent d903637 commit 78afc47
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
19 changes: 11 additions & 8 deletions lib/mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ int mosquitto_lib_cleanup(void)
return MOSQ_ERR_SUCCESS;
}

static int alloc_packet_buffer(struct mosquitto *mosq)
{
mosq->in_packet.packet_buffer_size = 4096;
mosq->in_packet.packet_buffer = mosquitto_calloc(1, mosq->in_packet.packet_buffer_size);
return !mosq->in_packet.packet_buffer;
}

struct mosquitto *mosquitto_new(const char *id, bool clean_start, void *userdata)
{
struct mosquitto *mosq = NULL;
Expand All @@ -113,13 +120,6 @@ struct mosquitto *mosquitto_new(const char *id, bool clean_start, void *userdata

mosq = (struct mosquitto *)mosquitto_calloc(1, sizeof(struct mosquitto));
if(mosq){
mosq->in_packet.packet_buffer_size = 4096;
mosq->in_packet.packet_buffer = mosquitto_calloc(1, mosq->in_packet.packet_buffer_size);
if(!mosq->in_packet.packet_buffer){
mosquitto_FREE(mosq);
errno = ENOMEM;
return NULL;
}
mosq->sock = INVALID_SOCKET;
#ifdef WITH_THREADING
# ifndef WIN32
Expand Down Expand Up @@ -169,6 +169,9 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st
mosq->wsd.is_client = true;
mosq->wsd.http_header_size = 4096;
#endif
if(alloc_packet_buffer(mosq)){
return MOSQ_ERR_NOMEM;
}
mosq->transport = mosq_t_tcp;
mosq->protocol = mosq_p_mqtt311;
mosq->sock = INVALID_SOCKET;
Expand Down Expand Up @@ -305,7 +308,7 @@ void mosquitto__destroy(struct mosquitto *mosq)
mosquitto_FREE(mosq->host);
mosquitto_FREE(mosq->bind_address);
mosquitto_FREE(mosq->in_packet.packet_buffer);
mosq->in_packet.packet_buffer_size = 0;
mosq->in_packet.packet_buffer_size = 4096;

mosquitto_property_free_all(&mosq->connect_properties);

Expand Down
8 changes: 4 additions & 4 deletions lib/packet_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,6 @@ int packet__read(struct mosquitto *mosq)
return MOSQ_ERR_NO_CONN;
}

state = mosquitto__get_state(mosq);
if(state == mosq_cs_connect_pending){
return MOSQ_ERR_SUCCESS;
}
#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_BUILTIN
if(mosq->transport == mosq_t_ws){
local__read = net__read_ws;
Expand All @@ -611,6 +607,10 @@ int packet__read(struct mosquitto *mosq)
* Finally, free the memory and reset everything to starting conditions.
*/
do{
state = mosquitto__get_state(mosq);
if(state == mosq_cs_connect_pending){
return MOSQ_ERR_SUCCESS;
}
rc = packet__read_single(mosq, state, local__read);
if(rc) return rc;
}while(mosq->in_packet.packet_buffer_to_process > 0);
Expand Down

0 comments on commit 78afc47

Please sign in to comment.