[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[mosquitto-dev] What's the difference between "mqtt3_db_message_write" and "loop_handle_reads_writes"?
|
Hello guys,
Recently I have read the mosquito source code to understand how the mqtt broker works. But I still have some doubts even I read the code again and again.
In mosquito source code, we traversed all the context in mosquitto_main_loop
as follows:
HASH_ITER(hh_sock, db->contexts_by_sock, context, ctxt_tmp)
{
...
/*if the context is valid, we handle the msgs queued in context*/
if(mqtt3_db_message_write(db, context) == MOSQ_ERR_SUCCESS)
{
....
}
...
/*poll all the fd in pollfds*/
fdcount = poll(pollfds, pollfd_index, 100);
if(fdcount == -1){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error in poll: %s.", strerror(errno));
}else{
/*we handle write and read events of pollfds*/
loop_handle_reads_writes(db, pollfds);
...
}
...
}
In mqtt3_db_message_write , we deal with all the msgs with states below: mosq_ms_publish_qos0
, mosq_ms_publish_qos1
, mosq_ms_publish_qos2
, mosq_ms_send_pubrec
, mosq_ms_resend_pubrel
, mosq_ms_resend_pubcomp
. Then we call _mosquitto_packet_queue
to put the packet into out_packet
list. Finaly we call _mosquitto_packet_write
to send all the packets out. For example
/*the call stack when we handle with msgs in 'mosq_ms_publish_qos0' states */
case mosq_ms_publish_qos0 -> _mosquitto_send_publish -> _mosquitto_send_real_publish ->
_mosquitto_packet_queue -> _mosquitto_packet_write -> _mosquitto_net_write
In loop_handle_reads_writes, we still call the function _mosquitto_packet_write
, but we have sent all the msgs in mqtt3_db_message_write
as I described above . So I want to know the difference between mqtt3_db_message_write
and the write action in loop_handle_reads_writes
.
Can you help me ? Thanks a lot.
Regards,
Li Xiasong