while (!do_terminate) {
struct instruction_queue_node *send_queue_node;
send_queue_node = (struct instruction_queue_node *) dequeue(send_outgoing_packets_queue);
if (send_queue_node) {
struct client *client;
int32_t client_is_socket_connected;
client = (struct client *) send_queue_node->peer;
read_lock_pthread_read_write_lock(&(client->lock));
client_is_socket_connected = client->is_socket_connected;
unlock_pthread_read_write_lock(&(client->lock));
if (client_is_socket_connected) {
struct send_outgoing_packet_callback_arguments *send_outgoing_packet_callback_arguments;
send_outgoing_packet_callback_arguments = (struct send_outgoing_packet_callback_arguments *) allocate_memory(sizeof (struct send_outgoing_packet_callback_arguments));
send_outgoing_packet_callback_arguments->total_number_of_bytes_sent_lock = total_number_of_bytes_sent_lock;
send_outgoing_packet_callback_arguments->total_number_of_bytes_sent = total_number_of_bytes_sent;
send_outgoing_packet_callback_arguments->instruction_length = send_queue_node->instruction_length;
send_outgoing_packet_callback_arguments->instruction_buffer = send_queue_node->instruction_buffer;
send_outgoing_packet_callback_arguments->client = client;
send_outgoing_packet_callback_arguments->dump_log_queue = dump_log_queue;
event_base_once(event_base, client->socket_descriptor, EV_WRITE, send_outgoing_packet_callback, (void *) send_outgoing_packet_callback_arguments, NULL);
event_base_loop(event_base, EVLOOP_NONBLOCK);
} else
free_memory(send_queue_node->instruction_buffer);
}
free_memory(send_queue_node);
lock_pthread_mutex(do_terminate_lock);
do_terminate = *(send_outgoing_packets_arguments->do_terminate);
unlock_pthread_mutex(do_terminate_lock);
}
The problem I'm seeing is that the event created with event_base_once occasionally will never fire if the client has just connected (New connection established).
I was under the impression that when event_base_loop is called, if the event created by event_base_once is ready to trigger immediately, then it's callback would be called. If it wasn't ready to trigger then on the next call of event_base_loop the same would be repeated again.