Skip to main content

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

If I remember correctly receiving a disconnect() callback isn't guaranteed but a connect() is.

Steve

On 4/8/2022 12:47 PM, Dougie Lawson wrote:
Hi Steve,

This one's an odd program, it connects to two brokers one as a subscriber (glowmqtt) one as a publisher (running on my local Raspberry). I grab a lump of JSON from the inbound message, strip the six bits of data I want out of the 20+ SMETS2 items that come in and republish that (as simpler JSON).

I'll look at doing the subscribe in the subscriber's connect callback.

Thanks for your reply.

Regards, Dougie

On Fri, 8 Apr 2022 at 17:05, Steve Prior <sprior@xxxxxxxxxxxx> wrote:
This one I think I can actually help with.  Like you I previously thought I should catch the disconnect callback and reconnect and found that to be unreliable.
I then discovered that by default mosquitto will handle the reconnect part for you so there is no need to do that.  I also found out that once I did reconnect that all my subscriptions were gone and needed to be recreated.  The better approach is to catch the connect callback and set up your subscriptions there and let the default behavior handle the reconnects.  I have been stable ever since.

Steve Prior

On 4/8/2022 10:33 AM, Dougie Lawson wrote:
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


_______________________________________________
mosquitto-dev mailing list
mosquitto-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/mosquitto-dev




--



Back to the top