From d60e44ec1b1dce062446333d040454f44b08391e Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 30 Jun 2014 06:58:56 +0100 Subject: [PATCH] Handle reconnects for websockets. --- src/read_handle_server.c | 61 ++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/src/read_handle_server.c b/src/read_handle_server.c index ffc90bdb..eeeb1752 100644 --- a/src/read_handle_server.c +++ b/src/read_handle_server.c @@ -31,6 +31,10 @@ Contributors: # include #endif +#ifdef WITH_WEBSOCKETS +#include +#endif + #ifdef WITH_SYS_TREE extern unsigned int g_connection_count; #endif @@ -383,60 +387,45 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context) /* Find if this client already has an entry. This must be done *after* any security checks. */ HASH_FIND(hh_id, db->contexts_by_id, client_id, strlen(client_id), found_context); if(found_context){ + HASH_DELETE(hh_id, db->contexts_by_id, found_context); + _mosquitto_free(found_context->id); + found_context->id = NULL; + /* Found a matching client */ - if(found_context->sock == -1){ + if(found_context->sock == INVALID_SOCKET){ /* Client is reconnecting after a disconnect */ /* FIXME - does anything else need to be done here? */ #ifdef WITH_SYS_TREE db->disconnected_count--; #endif + HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, found_context, sizeof(void *), found_context); }else{ /* Client is already connected, disconnect old version */ if(db->config->connection_messages == true){ _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Client %s already connected, closing old connection.", client_id); } + if(found_context->sock >= 0){ + HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, found_context, sizeof(void *), found_context); + } + mqtt3_context_disconnect(db, found_context); } + if(context->protocol == mosq_p_mqtt311){ if(clean_session == 0){ connect_ack |= 0x01; } } - found_context->clean_session = clean_session; - mqtt3_context_cleanup(db, found_context, false); - found_context->state = mosq_cs_connected; - if(context->address){ - found_context->address = context->address; - context->address = NULL; - }else{ - found_context->address = NULL; - } - found_context->disconnect_t = 0; - if(context->sock >= 0){ - HASH_DELETE(hh_sock, db->contexts_by_sock, context); - } - found_context->sock = context->sock; - found_context->listener = context->listener; - context->listener = NULL; - found_context->last_msg_in = mosquitto_time(); - found_context->last_msg_out = mosquitto_time(); - found_context->keepalive = context->keepalive; - found_context->pollfd_index = context->pollfd_index; -#ifdef WITH_TLS - found_context->ssl = context->ssl; -#endif - if(context->username){ - found_context->username = _mosquitto_strdup(context->username); - } - context->sock = -1; -#ifdef WITH_TLS - context->ssl = NULL; -#endif - context->state = mosq_cs_disconnecting; - HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, context, sizeof(void *), context); - context = found_context; - HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(context->sock), context); - if(context->msgs){ + + if(found_context->msgs){ + if(context->last_msg){ + context->last_msg->next = found_context->msgs; + context->last_msg = context->last_msg->next; + }else{ + context->msgs = found_context->msgs; + context->last_msg = found_context->msgs; + } mqtt3_db_message_reconnect_reset(context); + found_context->msgs = NULL; } }