1 /* IOEngine_select.c - IOMultiplexer
2 * Copyright (C) 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/>.
21 #define _WIN32_WINNT 0x501
29 static int engine_select_timer_delay_fix;
31 static int engine_select_init() {
32 engine_select_timer_delay_fix = 0;
36 static void engine_select_add(struct IOLowlevelDescriptor *iold) {
38 struct IODescriptor *iofd;
39 if((iofd = IOLOWLEVEL_GET_IOFD(iold))) {
40 if(iofd->type == IOTYPE_STDIN)
41 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT);
47 static void engine_select_remove(struct IODescriptor *iofd) {
51 static void engine_select_update(struct IODescriptor *iofd) {
55 static void engine_select_loop(struct timeval *timeout) {
58 unsigned int fds_size = 0;
59 struct IOLowlevelDescriptor *iold, *tmp_iold;
60 struct IODescriptor *iofd;
61 struct timeval now, tdiff;
65 gettimeofday(&now, NULL);
72 for(iold = first_descriptor; iold; iold = tmp_iold) {
73 tmp_iold = iold->next;
74 iofd = IOLOWLEVEL_GET_IOFD(iold);
75 if(iofd && iofd->type == IOTYPE_STDIN) {
77 //WIN32 doesn't support stdin within select
78 //just try to read the single events from the console
80 INPUT_RECORD inRecords[128];
83 GetNumberOfConsoleInputEvents(GetStdHandle(STD_INPUT_HANDLE), &dwRead);
85 ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &inRecords[0], 128, &dwRead);
86 for (i = 0; i < dwRead; ++i) {
87 if (inRecords[i].EventType == KEY_EVENT) {
88 const char c = inRecords[i].Event.KeyEvent.uChar.AsciiChar;
89 if (inRecords[i].Event.KeyEvent.bKeyDown && c != 0) {
90 iofd->readbuf.buffer[iofd->readbuf.bufpos + read_bytes] = c;
96 iohandler_events(iofd, read_bytes, 0);
97 if(read_bytes >= 128) {
103 timeout->tv_usec = 100000;
106 if(iold->fd > fds_size)
108 FD_SET(iold->fd, &read_fds);
112 else if(iold->fd >= 0) {
113 if(iofd && iofd->state == IO_CLOSED)
115 if(iold->fd > fds_size)
118 if(iold->flags & IOFLAGS_WANT_READ)
119 FD_SET(iold->fd, &read_fds);
120 if(iold->flags & IOFLAGS_WANT_WRITE)
121 FD_SET(iofd->fd, &write_fds);
125 while(timer_priority) {
126 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
127 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
128 if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
129 iofd = IOLOWLEVEL_GET_IOFD(iold);
130 if(iofd && iofd->constant_timeout) {
132 iohandler_set_timeout(iofd, &tdiff);
134 iohandler_events(timer_priority, 0, 0);
136 timer_priority->data.callback(timer_priority, 0, 0);
138 iohandler_events(timer_priority, 0, 0);
139 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
142 } else if(tdiff.tv_usec < 0) {
144 tdiff.tv_usec += 1000000; //1 sec
146 if(timeval_is_smaler((&tdiff), timeout)) {
147 timeout->tv_sec = tdiff.tv_sec;
148 timeout->tv_usec = tdiff.tv_usec + engine_select_timer_delay_fix;
149 if(timeout->tv_usec < 0) {
151 timeout->tv_usec += 1000000;
159 select_result = select(fds_size + 1, &read_fds, &write_fds, NULL, timeout);
162 Sleep((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000) + 1);
164 struct timespec usleep_time;
165 usleep_time.tv_sec = timeout->tv_sec;
166 usleep_time.tv_nsec = timeout->tv_usec * 1000;
167 nanosleep(&usleep_time, NULL);
171 if (select_result < 0) {
172 if (errno != EINTR) {
173 iohandler_log(IOLOG_FATAL, "select() failed with errno %d %d: %s", select_result, errno, strerror(errno));
178 gettimeofday(&now, NULL);
180 //check all descriptors
181 for(iold = first_descriptor; iold; iold = tmp_iold) {
182 tmp_iold = iold->next;
183 iofd = IOLOWLEVEL_GET_IOFD(iold);
184 if((iofd && iofd->type == IOTYPE_STDIN) || iold->fd >= 0) {
185 if(FD_ISSET(iold->fd, &read_fds) || FD_ISSET(iold->fd, &write_fds)) {
187 iohandler_events(iofd, FD_ISSET(iofd->fd, &read_fds), FD_ISSET(iofd->fd, &write_fds));
189 iold->data.callback(iold, FD_ISSET(iofd->fd, &read_fds), FD_ISSET(iofd->fd, &write_fds));
196 while(timer_priority) {
197 tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
198 tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
199 timer_fix = (tdiff.tv_sec * 1000000) + tdiff.tv_usec;
200 if(timer_fix <= 100) {
201 if(timer_fix + 100 < engine_select_timer_delay_fix || timer_fix - 100 > engine_select_timer_delay_fix) {
202 engine_select_timer_delay_fix = ((engine_select_timer_delay_fix * 19) + timer_fix) / 20;
203 iohandler_log(IOLOG_DEBUG, "timer delay fix set to: %d us", engine_select_timer_delay_fix);
205 if(timer_priority->constant_timeout) {
207 iohandler_set_timeout(timer_priority, &tdiff);
208 iohandler_events(timer_priority, 0, 0);
210 iohandler_events(timer_priority, 0, 0);
211 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
220 static void engine_select_cleanup() {
224 struct IOEngine engine_select = {
226 .init = engine_select_init,
227 .add = engine_select_add,
228 .remove = engine_select_remove,
229 .update = engine_select_update,
230 .loop = engine_select_loop,
231 .cleanup = engine_select_cleanup,