Skip to content

Commit

Permalink
[477] Send will messages for connected clients when broker stops.
Browse files Browse the repository at this point in the history
Thanks to mikeS7.

Bug: eclipse-mosquitto#477
  • Loading branch information
ralight committed Jun 27, 2017
1 parent 2d90a1f commit 5246a76
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Broker:
- Set persistence file to only be readable by owner, except on Windows. Closes
#468.
- Fix CONNECT check for reserved=0, as per MQTT v3.1.1 check MQTT-3.1.2-3.
- When the broker stop, wills for any connected clients are now "sent". Closes
#477.

Clients:
- Don't use / in auto-generated client ids.
Expand Down
18 changes: 11 additions & 7 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b
context->address = NULL;
}

mqtt3_context_send_will(db, context);

if(context->id){
assert(db); /* db can only be NULL here if the client hasn't sent a
CONNECT and hence wouldn't have an id. */
Expand All @@ -163,12 +165,6 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b
context->out_packet = context->out_packet->next;
_mosquitto_free(packet);
}
if(context->will){
if(context->will->topic) _mosquitto_free(context->will->topic);
if(context->will->payload) _mosquitto_free(context->will->payload);
_mosquitto_free(context->will);
context->will = NULL;
}
if(do_free || context->clean_session){
msg = context->msgs;
while(msg){
Expand All @@ -185,7 +181,8 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b
}
}

void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt)

void mqtt3_context_send_will(struct mosquitto_db *db, struct mosquitto *ctxt)
{
if(ctxt->state != mosq_cs_disconnecting && ctxt->will){
if(mosquitto_acl_check(db, ctxt, ctxt->will->topic, MOSQ_ACL_WRITE) == MOSQ_ERR_SUCCESS){
Expand All @@ -199,6 +196,13 @@ void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt)
_mosquitto_free(ctxt->will);
ctxt->will = NULL;
}
}


void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt)
{
mqtt3_context_send_will(db, ctxt);

ctxt->disconnect_t = time(NULL);
_mosquitto_socket_close(db, ctxt);
}
Expand Down
12 changes: 6 additions & 6 deletions src/mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,6 @@ int main(int argc, char *argv[])
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "mosquitto version %s terminating", VERSION);
mqtt3_log_close(&config);

#ifdef WITH_PERSISTENCE
if(config.persistence){
mqtt3_db_backup(&int_db, true);
}
#endif

#ifdef WITH_WEBSOCKETS
for(i=0; i<int_db.config->listener_count; i++){
if(int_db.config->listeners[i].ws_context){
Expand Down Expand Up @@ -428,6 +422,12 @@ int main(int argc, char *argv[])
#endif
mosquitto__free_disused_contexts(&int_db);

#ifdef WITH_PERSISTENCE
if(config.persistence){
mqtt3_db_backup(&int_db, true);
}
#endif

mqtt3_db_close(&int_db);

if(listensock){
Expand Down
1 change: 1 addition & 0 deletions src/mosquitto_broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b
void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *context);
void mosquitto__add_context_to_disused(struct mosquitto_db *db, struct mosquitto *context);
void mosquitto__free_disused_contexts(struct mosquitto_db *db);
void mqtt3_context_send_will(struct mosquitto_db *db, struct mosquitto *context);

/* ============================================================
* Logging functions
Expand Down

0 comments on commit 5246a76

Please sign in to comment.