Skip to content

Commit

Permalink
Fix mosquitto_pub -l not handling network failures.
Browse files Browse the repository at this point in the history
Closes #1152. Thanks to Dustin Sallings.
  • Loading branch information
ralight committed Sep 17, 2019
1 parent a4243ee commit 8153eb9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Expand Up @@ -44,6 +44,7 @@ Clients:
Closes #1274.

Clients:
- Fix `mosquitto_pub -l` not handling network failures. Closes #1152.
- Fix double free on exit in mosquitto_pub. Closes #1280.

Documentation:
Expand Down
19 changes: 14 additions & 5 deletions client/pub_client.c
Expand Up @@ -42,7 +42,6 @@ static int last_mid = -1;
static int last_mid_sent = -1;
static char *line_buf = NULL;
static int line_buf_len = 1024;
static bool connected = true;
static bool disconnect_sent = false;
static int publish_count = 0;
static bool ready_for_repeat = false;
Expand Down Expand Up @@ -104,7 +103,7 @@ void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc, const mos
UNUSED(rc);
UNUSED(properties);

connected = false;
status = STATUS_DISCONNECTED;
}

int my_publish(struct mosquitto *mosq, int *mid, const char *topic, int payloadlen, void *payload, int qos, bool retain)
Expand Down Expand Up @@ -222,6 +221,7 @@ int pub_shared_loop(struct mosquitto *mosq)
int buf_len_actual;
int mode;
int loop_delay = 1000;
bool stdin_finished = false;

if(cfg.repeat_count > 1 && (cfg.repeat_delay.tv_sec == 0 || cfg.repeat_delay.tv_usec != 0)){
loop_delay = cfg.repeat_delay.tv_usec / 2000;
Expand All @@ -231,14 +231,15 @@ int pub_shared_loop(struct mosquitto *mosq)

if(mode == MSGMODE_STDIN_LINE){
mosquitto_loop_start(mosq);
stdin_finished = false;
}

do{
if(mode == MSGMODE_STDIN_LINE){
if(status == STATUS_CONNACK_RECVD){
pos = 0;
read_len = line_buf_len;
while(connected && fgets(&line_buf[pos], read_len, stdin)){
while(status == STATUS_CONNACK_RECVD && fgets(&line_buf[pos], read_len, stdin)){
buf_len_actual = strlen(line_buf);
if(line_buf[buf_len_actual-1] == '\n'){
line_buf[buf_len_actual-1] = '\0';
Expand Down Expand Up @@ -270,6 +271,10 @@ int pub_shared_loop(struct mosquitto *mosq)
last_mid = mid_sent;
status = STATUS_WAITING;
}
stdin_finished = true;
}else if(status == STATUS_DISCONNECTED){
/* Not end of stdin, so we've lost our connection and must
* reconnect */
}
}else if(status == STATUS_WAITING){
if(last_mid_sent == last_mid && disconnect_sent == false){
Expand Down Expand Up @@ -307,12 +312,16 @@ int pub_shared_loop(struct mosquitto *mosq)
}
}
}
}while(rc == MOSQ_ERR_SUCCESS && connected);
}while(rc == MOSQ_ERR_SUCCESS && stdin_finished == false);

if(mode == MSGMODE_STDIN_LINE){
mosquitto_loop_stop(mosq, false);
}
return rc;
if(status == STATUS_DISCONNECTED){
return MOSQ_ERR_SUCCESS;
}else{
return rc;
}
}


Expand Down
1 change: 1 addition & 0 deletions client/pub_shared.h
Expand Up @@ -20,6 +20,7 @@ and the Eclipse Distribution License is available at
#define STATUS_CONNACK_RECVD 1
#define STATUS_WAITING 2
#define STATUS_DISCONNECTING 3
#define STATUS_DISCONNECTED 4

extern int mid_sent;
extern int status;
Expand Down

0 comments on commit 8153eb9

Please sign in to comment.