Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] Reconnection to a TLS server is unreliable.

Hi Roger,

I have a bit of code (that fetches my gas & electricity meter data from glowmqtt.energyhive.com). In the last couple of weeks that server has been having connection problems (which the owner tells me they've solved). But, I found that this piece of code in my C program is unreliable.

void glow_disconnect(struct mosquitto *mosq, void *obj, int rc)
{
printf("Glow disconnect from glowmqtt.energyhive.com:8883 at %s\n", time_stamp);
printf("Return code = %d\n", rc);
}

if(mosq_sub)
{
mosquitto_connect_callback_set(mosq_sub, glow_connect);
mosquitto_message_callback_set(mosq_sub, glow_message);
mosquitto_username_pw_set(mosq_sub, username, password);
mosquitto_tls_set(mosq_sub, NULL, "/etc/ssl/certs", NULL, NULL, NULL);
mosquitto_tls_opts_set(mosq_sub, 1, NULL, NULL);
rc = mosquitto_connect(mosq_sub, "glowmqtt.energyhive.com", 8883, 30);
if (rc) printf("mosquitto connect, rc=%d\n", rc);
//sprintf(topic, "SMART/HILD/%s", device);
sprintf(topic, "SMART/+/%s", device);
mosquitto_disconnect_callback_set(mosq_sub, glow_disconnect);
mosquitto_subscribe(mosq_sub, NULL, topic, 0);
mosquitto_loop_forever(mosq_sub, -1, 1);
mosquitto_destroy(mosq_sub);
}
It would frequently drive the disconnect callback with RC=19 (MOSQ_ERR_KEEPALIVE) and fail to reconnect.
So last night I removed the mosquitto_loop_forever() and replaced it with modq_start_loop() and turned all that TLS stuff into a new function in my code.

int try_Connect()
{
int rc;
if (was_connected !=0) {
printf("Was connected rc=%d", was_connected);
mosquitto_disconnect(mosq_sub);
mosquitto_loop_stop(mosq_sub, true);
was_connected = 0;
}
mosquitto_connect_callback_set(mosq_sub, glow_connect);
mosquitto_message_callback_set(mosq_sub, glow_message);
mosquitto_username_pw_set(mosq_sub, username, password);
mosquitto_tls_set(mosq_sub, NULL, "/etc/ssl/certs", NULL, NULL, NULL);
mosquitto_tls_opts_set(mosq_sub, 1, NULL, NULL);
rc = mosquitto_connect(mosq_sub, "glowmqtt.energyhive.com", 8883, 30);
if (rc) printf("mosquitto connect, rc=%d\n", rc);
mosquitto_disconnect_callback_set(mosq_sub, glow_disconnect);
return rc;
}

if(mosq_sub)
{
// sprintf(topic, "SMART/HILD/%s", device);
sprintf(topic, "SMART/+/%s", device);
mosquitto_subscribe(mosq_sub, NULL, topic, 0);
mosquitto_loop_start(mosq_pub);
// mosquitto_loop_forever(mosq_sub, -1, 1);
while(reconnect)
{
rc = mosquitto_loop(mosq_sub, -1, 1);
if (reconnect && rc)
{
printf("Conn error\n");
sleep(1);
rc = try_Connect();
printf("Glowmarkt reconnect rc=%d\n",rc);
// mosquitto_reconnect(mosq_sub);
mosquitto_subscribe(mosq_sub, NULL, topic, 0);
}
}
mosquitto_destroy(mosq_sub);
mosquitto_destroy(mosq_pub);
}

I've run that for twelve hours now and haven't had a failure during reconnection. I'm not sure whether mosq_loop_forever() is handling the TLS stuff correctly
My new version of code is on github @ https://github.com/DougieLawson/glowmarkt

Is this an opportunity for you to improve how mosq_loop_forever() handles a disconnection from a TLS server? Or was my original code just a pile of junk?

Regards. Dougie


Back to the top