int _mosquitto_packet_queue(struct mosquitto *mosq, struct _mosquitto_packet *packet) { assert(mosq); assert(packet); packet->pos = 0; packet->to_process = packet->packet_length; packet->next = NULL; 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); #ifdef WITH_BROKER return _mosquitto_packet_write(mosq); #else if(mosq->in_callback == false && mosq->threaded == false){ return _mosquitto_packet_write(mosq); }else{ return MOSQ_ERR_SUCCESS; } #endif }
int _mosquitto_packet_queue(struct mosquitto *mosq, struct _mosquitto_packet *packet) { struct _mosquitto_packet *tail; assert(mosq); assert(packet); packet->pos = 0; packet->to_process = packet->packet_length; packet->next = NULL; if(mosq->out_packet){ tail = mosq->out_packet; while(tail->next){ tail = tail->next; } tail->next = packet; }else{ mosq->out_packet = packet; } #ifdef WITH_BROKER return _mosquitto_packet_write(mosq); #else if(mosq->in_callback == false){ return _mosquitto_packet_write(mosq); }else{ return MOSQ_ERR_SUCCESS; } #endif }
int _mosquitto_packet_queue(struct mosquitto *mosq, struct _mosquitto_packet *packet) { #ifndef WITH_BROKER char sockpair_data = 0; #endif assert(mosq); assert(packet); packet->pos = 0; packet->to_process = packet->packet_length; packet->next = NULL; 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); #ifdef WITH_BROKER # ifdef WITH_WEBSOCKETS if(mosq->wsi){ libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi); return 0; }else{ return _mosquitto_packet_write(mosq); } # else return _mosquitto_packet_write(mosq); # endif #else /* Write a single byte to sockpairW (connected to sockpairR) to break out * of select() if in threaded mode. */ if(mosq->sockpairW != INVALID_SOCKET){ #ifndef WIN32 if(write(mosq->sockpairW, &sockpair_data, 1)){ } #else send(mosq->sockpairW, &sockpair_data, 1, 0); #endif } if(mosq->in_callback == false && mosq->threaded == false){ return _mosquitto_packet_write(mosq); }else{ return MOSQ_ERR_SUCCESS; } #endif }
int mosquitto_loop_write(struct mosquitto *mosq, int max_packets) { int rc; int i; if(max_packets < 1) return MOSQ_ERR_INVAL; pthread_mutex_lock(&mosq->out_message_mutex); max_packets = mosq->out_queue_len; pthread_mutex_unlock(&mosq->out_message_mutex); pthread_mutex_lock(&mosq->in_message_mutex); max_packets += mosq->in_queue_len; pthread_mutex_unlock(&mosq->in_message_mutex); if(max_packets < 1) max_packets = 1; /* Queue len here tells us how many messages are awaiting processing and * have QoS > 0. We should try to deal with that many in this loop in order * to keep up. */ printf("==================================\n"); printf("----------------------------------\n"); printf("#Queue message : %d\n", max_packets); printf("----------------------------------\n"); printf("==================================\n"); for(i=0; i<max_packets; i++){ rc = _mosquitto_packet_write(mosq); if(rc || errno == EAGAIN || errno == COMPAT_EWOULDBLOCK){ return _mosquitto_loop_rc_handle(mosq, rc); } } return rc; }
static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds) { int i; for(i=0; i<db->context_count; i++){ if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){ assert(pollfds[db->contexts[i]->pollfd_index].fd == db->contexts[i]->sock); #ifdef WITH_TLS if(pollfds[db->contexts[i]->pollfd_index].revents & POLLOUT || db->contexts[i]->want_write || (db->contexts[i]->ssl && db->contexts[i]->state == mosq_cs_new)){ #else if(pollfds[db->contexts[i]->pollfd_index].revents & POLLOUT){ #endif if(_mosquitto_packet_write(db->contexts[i])){ if(db->config->connection_messages == true){ if(db->contexts[i]->state != mosq_cs_disconnecting){ _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Socket write error on client %s, disconnecting.", db->contexts[i]->id); }else{ _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", db->contexts[i]->id); } } /* Write error or other that means we should disconnect */ mqtt3_context_disconnect(db, db->contexts[i]); } } } if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){ assert(pollfds[db->contexts[i]->pollfd_index].fd == db->contexts[i]->sock); #ifdef WITH_TLS if(pollfds[db->contexts[i]->pollfd_index].revents & POLLIN || db->contexts[i]->want_read || (db->contexts[i]->ssl && db->contexts[i]->state == mosq_cs_new)){ #else if(pollfds[db->contexts[i]->pollfd_index].revents & POLLIN){ #endif if(_mosquitto_packet_read(db, db->contexts[i])){ if(db->config->connection_messages == true){ if(db->contexts[i]->state != mosq_cs_disconnecting){ _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Socket read error on client %s, disconnecting.", db->contexts[i]->id); }else{ _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", db->contexts[i]->id); } } /* Read error or other that means we should disconnect */ mqtt3_context_disconnect(db, db->contexts[i]); } } } if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){ if(pollfds[db->contexts[i]->pollfd_index].revents & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)){ do_disconnect(db, i); } } } }
int mosquitto_loop_write(struct mosquitto *mosq, int max_packets) { int rc; int i; if(max_packets < 1) return MOSQ_ERR_INVAL; max_packets = mosq->queue_len; if(max_packets < 1) max_packets = 1; /* Queue len here tells us how many messages are awaiting processing and * have QoS > 0. We should try to deal with that many in this loop in order * to keep up. */ for(i=0; i<max_packets; i++) { rc = _mosquitto_packet_write(mosq); if(rc || errno == EAGAIN || errno == COMPAT_EWOULDBLOCK) { return _mosquitto_loop_rc_handle(mosq, rc); } } return rc; }
static void loop_handle_reads_writes(struct mosquitto_db *db, struct epoll_event *events, int efd) { int i, s; for (i = 0; i < db->context_count; i++) { if (db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){ /* if (events[db->contexts[i]->pollfd_index].data.fd != db->contexts[i]->sock) { */ /* printf ("%d---%d\n", events[db->contexts[i]->pollfd_index].data.fd, db->contexts[i]->sock); */ /* printf ("%d\n", i); */ /* int n; */ /* for (n = 0; n < sizeof (db->contexts); ++n) */ /* { */ /* printf ("%d----%d\n", db->contexts[n]->pollfd_index, events[db->contexts[i]->pollfd_index].data.fd); */ /* } */ /* } */ /* assert(events[db->contexts[i]->pollfd_index].data.fd == db->contexts[i]->sock); */ #ifdef WITH_TLS if (events[db->contexts[i]->pollfd_index].events & EPOLLOUT || db->contexts[i]->want_write || (db->contexts[i]->ssl && db->contexts[i]->state == mosq_cs_new)) { #else if(events[db->contexts[i]->epollfd_index].events & EPOLLOUT){ #endif if(_mosquitto_packet_write(db->contexts[i])){ if(db->config->connection_messages == true){ if(db->contexts[i]->state != mosq_cs_disconnecting){ _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Socket write error on client %s, disconnecting.", db->contexts[i]->id); }else{ _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", db->contexts[i]->id); } } /* Write error or other that means we should disconnect */ mqtt3_context_disconnect(db, db->contexts[i]); } } } if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){ /* assert(events[db->contexts[i]->pollfd_index].data.fd == db->contexts[i]->sock); */ #ifdef WITH_TLS if(events[db->contexts[i]->pollfd_index].events & EPOLLIN || db->contexts[i]->want_read || (db->contexts[i]->ssl && db->contexts[i]->state == mosq_cs_new)){ #else if(events[db->contexts[i]->pollfd_index].events & EPOLLIN){ #endif if(_mosquitto_packet_read(db, db->contexts[i])){ if(db->config->connection_messages == true){ if(db->contexts[i]->state != mosq_cs_disconnecting){ _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Socket read error on client %s, disconnecting.", db->contexts[i]->id); }else{ _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", db->contexts[i]->id); } } /* Read error or other that means we should disconnect */ mqtt3_context_disconnect(db, db->contexts[i]); } } } if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){ if(events[db->contexts[i]->pollfd_index].events & (EPOLLHUP | POLLRDHUP | EPOLLERR)){ s = epoll_ctl (efd, EPOLL_CTL_DEL, events[db->contexts[i]->pollfd_index].data.fd, &events[db->contexts[i]->pollfd_index]); do_disconnect(db, i); } } } }
int mosquitto_loop_write(struct mosquitto *mosq) { return _mosquitto_packet_write(mosq); }