X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FIOEngine_select.c;h=8303308f3876f2dcb68a85f76ed36f63987c5f58;hb=HEAD;hp=af919c2c92fde8935ac2d84ea73f79a4ca697c87;hpb=0e2ac9360b71a7d054d7e945be575c0d21fa880c;p=TransparentIRC.git diff --git a/src/IOEngine_select.c b/src/IOEngine_select.c index af919c2..8303308 100644 --- a/src/IOEngine_select.c +++ b/src/IOEngine_select.c @@ -56,56 +56,62 @@ static void engine_select_loop(struct timeval *timeout) { FD_SET(iofd->fd, &read_fds); if(iohandler_wants_writes(iofd)) FD_SET(iofd->fd, &write_fds); - } else if(iofd->type == IOTYPE_TIMER) { - tdiff.tv_sec = iofd->timeout.tv_sec - now.tv_sec; - tdiff.tv_usec = iofd->timeout.tv_usec - now.tv_usec; - - if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) { - //exec timer - iofd->state = IO_CLOSED; - iohandler_events(iofd, 1, 0); - iohandler_close(iofd); - continue; - } else if(tdiff.tv_usec < 0) { - tdiff.tv_sec--; - tdiff.tv_usec += 1000000; //1 sec - } - if(timeval_is_smaler(&tdiff, timeout)) { - timeout->tv_sec = tdiff.tv_sec; - timeout->tv_usec = tdiff.tv_usec; - } } } + while(timer_priority) { + tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec; + tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec; + if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) { + iohandler_events(timer_priority, 0, 0); + iohandler_close(timer_priority); //also sets timer_priority to the next timed element + continue; + } else if(tdiff.tv_usec < 0) { + tdiff.tv_sec--; + tdiff.tv_usec += 1000000; //1 sec + } + if(timeval_is_smaler((&tdiff), timeout)) { + timeout->tv_sec = tdiff.tv_sec; + timeout->tv_usec = tdiff.tv_usec; + } + break; + } + //select system call select_result = select(fds_size + 1, &read_fds, &write_fds, NULL, timeout); if (select_result < 0) { if (errno != EINTR) { - //hard fail + iohandler_log(IOLOG_FATAL, "select() failed with errno %d: %s", errno, strerror(errno)); return; } } + gettimeofday(&now, NULL); + //check all descriptors for(iofd = first_descriptor; iofd; iofd = tmp_iofd) { tmp_iofd = iofd->next; if(iofd->type == IOTYPE_SERVER || iofd->type == IOTYPE_CLIENT || iofd->type == IOTYPE_STDIN) { if(FD_ISSET(iofd->fd, &read_fds) || FD_ISSET(iofd->fd, &write_fds)) { iohandler_events(iofd, FD_ISSET(iofd->fd, &read_fds), FD_ISSET(iofd->fd, &write_fds)); - } - } else if(iofd->type == IOTYPE_TIMER) { - tdiff.tv_sec = iofd->timeout.tv_sec - now.tv_sec; - tdiff.tv_usec = iofd->timeout.tv_usec - now.tv_usec; - - if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) { - //exec timer - iofd->state = IO_CLOSED; - iohandler_events(iofd, 1, 0); continue; } } } + + //check timers + while(timer_priority) { + tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec; + tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec; + if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) { + iohandler_events(timer_priority, 0, 0); + iohandler_close(timer_priority); //also sets timer_priority to the next timed element + continue; + } + break; + } + } static void engine_select_cleanup() {