Skip to content

Commit

Permalink
Add user level connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ger committed Feb 20, 2018
1 parent 1821f75 commit 8a4404c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/mqtt/mqtt_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef struct _MQTT_ClientCon {
// MqttDataCallback dataCb;
ETSTimer mqttTimer;
uint32_t sendTimeout;
uint32_t connectionTimeout;
tConnState connState;
QUEUE msgQueue;
uint8_t protocolVersion;
Expand Down
10 changes: 10 additions & 0 deletions src/mqtt_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ void ICACHE_FLASH_ATTR mqtt_server_timer(void *arg) {

if (clientcon->sendTimeout > 0)
clientcon->sendTimeout--;

if (clientcon->connectionTimeout > 0) {
clientcon->connectionTimeout--;
} else {
MQTT_WARNING("MQTT: Connection timeout %ds\r\n", 2*clientcon->connect_info.keepalive+10);
MQTT_server_disconnectClientCon(clientcon);
}
}

bool ICACHE_FLASH_ATTR delete_client_by_id(const uint8_t *id) {
Expand Down Expand Up @@ -776,6 +783,8 @@ static void ICACHE_FLASH_ATTR MQTT_ClientCon_recv_cb(void *arg, char *pdata, uns
break;
}

clientcon->connectionTimeout = 2 * clientcon->connect_info.keepalive+10;

// More than one MQTT command in the packet?
len = clientcon->mqtt_state.message_length_read;
if (clientcon->mqtt_state.message_length < len) {
Expand Down Expand Up @@ -859,6 +868,7 @@ static void ICACHE_FLASH_ATTR MQTT_ClientCon_connected_cb(void *arg) {

MQTT_server_initClientCon(mqttClientCon);

mqttClientCon->connectionTimeout = 40;
os_timer_setfn(&mqttClientCon->mqttTimer, (os_timer_func_t *) mqtt_server_timer, mqttClientCon);
os_timer_arm(&mqttClientCon->mqttTimer, 1000, 1);
}
Expand Down

0 comments on commit 8a4404c

Please sign in to comment.