Skip to content

Commit 78afc47

Browse files
committed
Fix library reinitialise with packet buffer.
1 parent d903637 commit 78afc47

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

lib/mosquitto.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ int mosquitto_lib_cleanup(void)
101101
return MOSQ_ERR_SUCCESS;
102102
}
103103

104+
static int alloc_packet_buffer(struct mosquitto *mosq)
105+
{
106+
mosq->in_packet.packet_buffer_size = 4096;
107+
mosq->in_packet.packet_buffer = mosquitto_calloc(1, mosq->in_packet.packet_buffer_size);
108+
return !mosq->in_packet.packet_buffer;
109+
}
110+
104111
struct mosquitto *mosquitto_new(const char *id, bool clean_start, void *userdata)
105112
{
106113
struct mosquitto *mosq = NULL;
@@ -113,13 +120,6 @@ struct mosquitto *mosquitto_new(const char *id, bool clean_start, void *userdata
113120

114121
mosq = (struct mosquitto *)mosquitto_calloc(1, sizeof(struct mosquitto));
115122
if(mosq){
116-
mosq->in_packet.packet_buffer_size = 4096;
117-
mosq->in_packet.packet_buffer = mosquitto_calloc(1, mosq->in_packet.packet_buffer_size);
118-
if(!mosq->in_packet.packet_buffer){
119-
mosquitto_FREE(mosq);
120-
errno = ENOMEM;
121-
return NULL;
122-
}
123123
mosq->sock = INVALID_SOCKET;
124124
#ifdef WITH_THREADING
125125
# ifndef WIN32
@@ -169,6 +169,9 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st
169169
mosq->wsd.is_client = true;
170170
mosq->wsd.http_header_size = 4096;
171171
#endif
172+
if(alloc_packet_buffer(mosq)){
173+
return MOSQ_ERR_NOMEM;
174+
}
172175
mosq->transport = mosq_t_tcp;
173176
mosq->protocol = mosq_p_mqtt311;
174177
mosq->sock = INVALID_SOCKET;
@@ -305,7 +308,7 @@ void mosquitto__destroy(struct mosquitto *mosq)
305308
mosquitto_FREE(mosq->host);
306309
mosquitto_FREE(mosq->bind_address);
307310
mosquitto_FREE(mosq->in_packet.packet_buffer);
308-
mosq->in_packet.packet_buffer_size = 0;
311+
mosq->in_packet.packet_buffer_size = 4096;
309312

310313
mosquitto_property_free_all(&mosq->connect_properties);
311314

lib/packet_mosq.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,6 @@ int packet__read(struct mosquitto *mosq)
583583
return MOSQ_ERR_NO_CONN;
584584
}
585585

586-
state = mosquitto__get_state(mosq);
587-
if(state == mosq_cs_connect_pending){
588-
return MOSQ_ERR_SUCCESS;
589-
}
590586
#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_BUILTIN
591587
if(mosq->transport == mosq_t_ws){
592588
local__read = net__read_ws;
@@ -611,6 +607,10 @@ int packet__read(struct mosquitto *mosq)
611607
* Finally, free the memory and reset everything to starting conditions.
612608
*/
613609
do{
610+
state = mosquitto__get_state(mosq);
611+
if(state == mosq_cs_connect_pending){
612+
return MOSQ_ERR_SUCCESS;
613+
}
614614
rc = packet__read_single(mosq, state, local__read);
615615
if(rc) return rc;
616616
}while(mosq->in_packet.packet_buffer_to_process > 0);

0 commit comments

Comments
 (0)