int InitializeMultiplexer(void) { EpollHandle = epoll_create(4); if (EpollHandle == -1) return -1; vec_init(&events); // Default of 5 events, it will expand if we get more sockets vec_reserve(&events, 5); if (errno == ENOMEM) return -1; return 0; }
static void ext_iup_vbox(script_t* script, vector_t* args) { vector_t* array = &script_get_arg(args, 0)->array; vector_t handle_array; vec_init(&handle_array, sizeof(Ihandle*)); vec_reserve(&handle_array, array->length + 1); for(int i = 0; i < array->length; ++i) { script_value_t* val = vec_get_value(array, i, script_value_t*); vec_push_back(&handle_array, &val->nat.value); } Ihandle* null_handle = NULL; vec_push_back(&handle_array, &null_handle); Ihandle* vbox = IupVboxv((Ihandle**)handle_array.data); vec_destroy(&handle_array); script_push_native(script, vbox, NULL, iup_handle_free); script_return_top(script); }
void ProcessSockets(void) { if (socketpool.length >= events.capacity) { bprintf("Reserving more space for events\n"); vec_reserve(&events, socketpool.length * 2); } bprintf("Entering epoll_wait\n"); int total = epoll_wait(EpollHandle, &vec_first(&events), events.capacity, config->readtimeout * 1000); if (total == -1) { if (errno != EINTR) fprintf(stderr, "Error processing sockets: %s\n", strerror(errno)); return; } for (int i = 0; i < total; ++i) { epoll_t *ev = &(events.data[i]); socket_t s; if (FindSocket(ev->data.fd, &s) == -1) { bfprintf(stderr, "Unknown FD in multiplexer: %d\n", ev->data.fd); // We don't know what socket this is. Someone added something // stupid somewhere so shut this shit down now. // We have to create a temporary socket_t object to remove it // from the multiplexer, then we can close it. socket_t tmp = { ev->data.fd, 0, 0, 0, 0 }; RemoveFromMultiplexer(tmp); close(ev->data.fd); continue; } // Call our event. CallEvent(EV_SOCKETACTIVITY, &s); if (ev->events & (EPOLLHUP | EPOLLERR)) { bprintf("Epoll error reading socket %d, destroying.\n", s.fd); DestroySocket(s, 1); continue; } // Process socket write events if (ev->events & EPOLLOUT && SendPackets(s) == -1) { bprintf("Destorying socket due to send failure!\n"); DestroySocket(s, 1); } // process socket read events. if (ev->events & EPOLLIN && ReceivePackets(s) == -1) { bprintf("Destorying socket due to receive failure!\n"); DestroySocket(s, 1); } } }