Search code examples
c++cmqttlibmosquitto

What does it mean that data is prepared in the function of mosquitto_want_write()?


According to the API, the mosquitto_want_write() function returns TRUE when data to be used in the socket is prepared.

  1. But what does it mean to have data ready? Where is the data being prepared? What function call can prepare the data?

  2. Is the data prepared by the call Mosquito_publish()?

Unfortunately, I couldn't find any link related to this. I would appreciate it if you could let me know if there is any data or link related to this.


Solution

  • Look no further than the definition of mosquitto_want_write:

    bool mosquitto_want_write(struct mosquitto *mosq) {
        bool result = false;
        if(mosq->out_packet || mosq->current_out_packet){
            result = true;
        }
        return result;
    }
    

    A call to mosquitto_publish eventually ends up in send__publish, which calls send__real_publish, which calls packet__queue.

    And packet__queue is where mosq->out_packet is filled:

        pthread_mutex_lock(&mosq->out_packet_mutex);
        if(mosq->out_packet){
            mosq->out_packet_last->next = packet;
        }else{
            mosq->out_packet = packet;
        }
        mosq->out_packet_last = packet;
        pthread_mutex_unlock(&mosq->out_packet_mutex);
    

    So, to summarize: mosquitto struct holds a linked list of packets to send and the actions add packets to the queue. Under certain conditions these packets are sent directly, I will leave it up to you to figure out what these are.