Search code examples
mosquittolibmosquitto

Mosquitto.h does not define "struct mosquitto" contents


As far as I understood, when client connects, I must check for session being persistent, and if it is not, perform subscribe to the topics of interest. I am using Mosquitto version 1.5.

I did not find a way to check for this property using API calls, but found out there's clean_session boolean field in the struct mosquitto. But when I try to access it this way:

void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
    log("Connected, session persistency",(int)(mosq->clean_session));

I get error dereferencing pointer to incomplete type. It appears to be normal as soon as mosquitto.h does NOT have definition of the fields within struct mosquitto. The file called mosquitto_internal.h does have it, but it seems it is not designed to be included into the application project.

What is wrong here?

Update (after @hardillb comment): (1) I did not find documentation saying that clean_session in the mosquitto struct is the same I pass to mosquitto_new; (2) the documentation here says

The CONNACK message contains two data entries:

  • The session present flag
  • A connect return code

And it is logical that "session present flag" would be somewhere in the connect callback data or in mosquitto struct itself. Therefore I guessed that clean_session in the struct is the value returned by the connect function. Next, same source says:

The session present flag tells the client whether the broker already has a persistent session available from previous interactions with the client. When a client connects with Clean Session set to true, the session present flag is always false because there is no session available. If a client connects with Clean Session set to false, there are two possibilities: If session information is available for the clientId. and the broker has stored session information, the session present flag is true.

Where is this session_present flag - so that application knows current connection is continuation of previous and does not re-subscribe to same topics?


Solution

  • Looking at the source code it implies you should be using the mosquitto_connect_with_flags_callback_set() rather than mosquitto_connect_callback_set()

    And passing a callback function pointer that takes an extra int field at the end of the args.

    The int field will hold the CONNACK flags (which will basically be 0 or 1)

    It does look like that callback is missing from the man page.