1 /* IOEngine_select.c - TransparentIRC 0.1
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "IOHandler.h"
20 static int engine_select_init() {
25 static void engine_select_add(struct IODescriptor *iofd) {
29 static void engine_select_remove(struct IODescriptor *iofd) {
33 static void engine_select_update(struct IODescriptor *iofd) {
37 static void engine_select_loop(struct timeval *timeout) {
40 unsigned int fds_size = 0;
41 struct IODescriptor *iofd, *tmp_iofd;
42 struct timeval now, tdiff;
45 gettimeofday(&now, NULL);
51 for(iofd = first_descriptor; iofd; iofd = tmp_iofd) {
52 tmp_iofd = iofd->next;
53 if(iofd->type == IOTYPE_SERVER || iofd->type == IOTYPE_CLIENT || iofd->type == IOTYPE_STDIN) {
54 if(iofd->fd > fds_size)
56 FD_SET(iofd->fd, &read_fds);
57 if(iohandler_wants_writes(iofd))
58 FD_SET(iofd->fd, &write_fds);
62 while(timer_priority) {
63 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
64 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
65 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
66 iohandler_events(timer_priority, 0, 0);
67 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
69 } else if(tdiff.tv_usec < 0) {
71 tdiff.tv_usec += 1000000; //1 sec
73 if(timeval_is_smaler((&tdiff), timeout)) {
74 timeout->tv_sec = tdiff.tv_sec;
75 timeout->tv_usec = tdiff.tv_usec;
81 select_result = select(fds_size + 1, &read_fds, &write_fds, NULL, timeout);
83 if (select_result < 0) {
85 iohandler_log(IOLOG_FATAL, "select() failed with errno %d: %s", errno, strerror(errno));
90 gettimeofday(&now, NULL);
92 //check all descriptors
93 for(iofd = first_descriptor; iofd; iofd = tmp_iofd) {
94 tmp_iofd = iofd->next;
95 if(iofd->type == IOTYPE_SERVER || iofd->type == IOTYPE_CLIENT || iofd->type == IOTYPE_STDIN) {
96 if(FD_ISSET(iofd->fd, &read_fds) || FD_ISSET(iofd->fd, &write_fds)) {
97 iohandler_events(iofd, FD_ISSET(iofd->fd, &read_fds), FD_ISSET(iofd->fd, &write_fds));
104 while(timer_priority) {
105 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
106 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
107 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
108 iohandler_events(timer_priority, 0, 0);
109 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
117 static void engine_select_cleanup() {
121 struct IOEngine engine_select = {
123 .init = engine_select_init,
124 .add = engine_select_add,
125 .remove = engine_select_remove,
126 .update = engine_select_update,
127 .loop = engine_select_loop,
128 .cleanup = engine_select_cleanup,