/* * description: send msg to zigbee middleware * sendbuf: the msg , need contain '\0' in the tail * sendbuflen: msg length * */ void ev_send_msg_2_zigbees(EV_ALL* cev, const unsigned char *sendbuf, int sendbuflen) //add send queue { if(cev->zigbee_client.isconnected ==socket_notconnect) { log_printf(LOG_ERROR, "[ZigbeeClientIsClosed]\n"); return; } send_queue_item_st *item; item = malloc(sizeof(send_queue_item_st));//important, must be free , 队列发送完成后需要释放 unsigned char *buf_ptr_item = malloc(sendbuflen); //important, must be free , 队列发送完成后需要释放 //snprintf((char *)buf_ptr_item, sendbuflen, "%s", (char *)sendbuf); // char* buf_ptr_item = (char *)sendbuf; memcpy(buf_ptr_item, sendbuf,sendbuflen ); item->sendbuf = buf_ptr_item; item->buflen = sendbuflen; llqueue_offer(cev->zigbee_client.send_queue, item); //入队列 int q_count = llqueue_count(cev->zigbee_client.send_queue); log_printf(LOG_NOTICE, "[ZigbeeSendQueueCount]=%d\n", q_count); if(llqueue_count(cev->zigbee_client.send_queue)==1) { // ev_io_set(&cev->zg_writew, cev->zigbee_client.my_socket, EV_WRITE); ev_io_start(cev->mainloop, &cev->zg_writew); } }
static void zg_write_cb(EV_P_ ev_io *w, int revents) //send by queue { int rc=-1; EV_ALL *cev = (EV_ALL *)(((char *)w) - offsetof (EV_ALL, zg_writew)); unsigned char *buf_ptr_item; send_queue_item_st *item; item = llqueue_poll(cev->zigbee_client.send_queue); buf_ptr_item = item->sendbuf; int sendlen = item->buflen; rc = ev_write(cev->zigbee_client.my_socket, buf_ptr_item, sendlen); if (rc <0) { log_printf(LOG_ERROR, "zg_write_cb,send msg to zigbee middleware failed\n"); //TODO: ERROR HANDLE } else { //send success log_printf(LOG_NOTICE, "[Send2ZW]: %s, %d\n", buf_ptr_item, sendlen); } free(buf_ptr_item); //must free free(item); //must free int q_count = llqueue_count(cev->zigbee_client.send_queue); log_printf(LOG_NOTICE, "[ZigbeeSendQueueCount]=%d\n", q_count); if(q_count <=0) { log_printf(LOG_NOTICE, "[StopZigbeeSendWatcher]send queue empty!\n"); ev_io_stop(cev->mainloop, &cev->zg_writew); } }
static void mqtt_write_cb(EV_P_ ev_io *w, int revents) { EV_ALL *cev = (EV_ALL *)(((char *)w) - offsetof (EV_ALL, mt_writew)); int rc = FAILURE, sent = 0; unsigned char *buf_ptr_item; send_queue_item_st *item; item = llqueue_poll(cev->client.send_queue); buf_ptr_item = item->sendbuf; int length = item->buflen; while (sent < length) { rc = linux_write_ev(cev->client.ipstack, &buf_ptr_item[sent], length); if (rc < 0) // there was an error writing the data break; sent += rc; } if (sent == length) { //record the fact that we have successfully sent the packet if(cev->client.keepAliveInterval >0) { // cev->mt_pingw.repeat = cev->client.keepAliveInterval-1; //可以重新设超时时间 ev_timer_again(cev->mainloop, &cev->mt_pingw); //重新计时发ping包 log_printf(LOG_NOTICE, "reset mqtt ping timer,interval is %d\n", PING_INTERVAL); } log_printf(LOG_NOTICE, "[Send2Mqtt]: %s, %d\n", buf_ptr_item, length); rc = SUCCESS; } else { log_printf(LOG_ERROR, "mqtt_write_cb failed\n"); rc = FAILURE; } //队列数据取出发送之后,需要释放 free(buf_ptr_item); //must free free(item); //must free int q_count = llqueue_count(cev->client.send_queue); log_printf(LOG_NOTICE, "[MqttSendQueueCount]=%d\n", q_count); if(q_count <=0) { log_printf(LOG_NOTICE, "[StopMqttSendWatcher]send queue empty!\n"); ev_io_stop(cev->mainloop, &cev->mt_writew); } // if(rc != SUCCESS) { log_printf(LOG_ERROR, "mqtt_write_cb failed,[ReConnectMqtt]\n"); MQTTReConnect(&cev->client); } }
void ev_send_msg_2_mqtts(EV_ALL* cev, const unsigned char *sendbuf, int sendbuflen) //new queue send { send_queue_item_st *item; item = malloc(sizeof(send_queue_item_st));//important, must be free , 队列发送完成后需要释放 unsigned char *buf_ptr_item = malloc(sendbuflen); //important, must be free , 队列发送完成后需要释放 memcpy(buf_ptr_item, sendbuf, sendbuflen); item->sendbuf = buf_ptr_item; item->buflen = sendbuflen; llqueue_offer(cev->client.send_queue, item); //入队列 int q_count = llqueue_count(cev->client.send_queue); log_printf(LOG_NOTICE, "[MqttSendQueueCount]=%d\n", q_count); if(llqueue_count(cev->client.send_queue)==1) { ev_io_start(cev->mainloop, &cev->mt_writew); } }
int sender_msgs_available(void* s) { sender_t* me = s; return 0 < llqueue_count(me->inbox); }