[477] Send will messages for connected clients when broker stops.

Thanks to mikeS7.

Bug: https://github.com/eclipse/mosquitto/issues/477
pull/459/head
Roger A. Light 8 years ago
parent 2d90a1f45b
commit 5246a76f87

@ -14,6 +14,8 @@ Broker:
- Set persistence file to only be readable by owner, except on Windows. Closes - Set persistence file to only be readable by owner, except on Windows. Closes
#468. #468.
- Fix CONNECT check for reserved=0, as per MQTT v3.1.1 check MQTT-3.1.2-3. - 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: Clients:
- Don't use / in auto-generated client ids. - Don't use / in auto-generated client ids.

@ -143,6 +143,8 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b
context->address = NULL; context->address = NULL;
} }
mqtt3_context_send_will(db, context);
if(context->id){ if(context->id){
assert(db); /* db can only be NULL here if the client hasn't sent a assert(db); /* db can only be NULL here if the client hasn't sent a
CONNECT and hence wouldn't have an id. */ CONNECT and hence wouldn't have an id. */
@ -163,12 +165,6 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b
context->out_packet = context->out_packet->next; context->out_packet = context->out_packet->next;
_mosquitto_free(packet); _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){ if(do_free || context->clean_session){
msg = context->msgs; msg = context->msgs;
while(msg){ while(msg){
@ -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(ctxt->state != mosq_cs_disconnecting && ctxt->will){
if(mosquitto_acl_check(db, ctxt, ctxt->will->topic, MOSQ_ACL_WRITE) == MOSQ_ERR_SUCCESS){ if(mosquitto_acl_check(db, ctxt, ctxt->will->topic, MOSQ_ACL_WRITE) == MOSQ_ERR_SUCCESS){
@ -199,6 +196,13 @@ void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt)
_mosquitto_free(ctxt->will); _mosquitto_free(ctxt->will);
ctxt->will = NULL; 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); ctxt->disconnect_t = time(NULL);
_mosquitto_socket_close(db, ctxt); _mosquitto_socket_close(db, ctxt);
} }

@ -387,12 +387,6 @@ int main(int argc, char *argv[])
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "mosquitto version %s terminating", VERSION); _mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "mosquitto version %s terminating", VERSION);
mqtt3_log_close(&config); mqtt3_log_close(&config);
#ifdef WITH_PERSISTENCE
if(config.persistence){
mqtt3_db_backup(&int_db, true);
}
#endif
#ifdef WITH_WEBSOCKETS #ifdef WITH_WEBSOCKETS
for(i=0; i<int_db.config->listener_count; i++){ for(i=0; i<int_db.config->listener_count; i++){
if(int_db.config->listeners[i].ws_context){ if(int_db.config->listeners[i].ws_context){
@ -428,6 +422,12 @@ int main(int argc, char *argv[])
#endif #endif
mosquitto__free_disused_contexts(&int_db); mosquitto__free_disused_contexts(&int_db);
#ifdef WITH_PERSISTENCE
if(config.persistence){
mqtt3_db_backup(&int_db, true);
}
#endif
mqtt3_db_close(&int_db); mqtt3_db_close(&int_db);
if(listensock){ if(listensock){

@ -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 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__add_context_to_disused(struct mosquitto_db *db, struct mosquitto *context);
void mosquitto__free_disused_contexts(struct mosquitto_db *db); void mosquitto__free_disused_contexts(struct mosquitto_db *db);
void mqtt3_context_send_will(struct mosquitto_db *db, struct mosquitto *context);
/* ============================================================ /* ============================================================
* Logging functions * Logging functions

Loading…
Cancel
Save