[IOMultiplexerV2] fixed compiling issue with newer MinGW builds...
[NextIRCd.git] / src / IOHandler / IOEngine_select.c
1 /* IOEngine_select.c - IOMultiplexer
2  * Copyright (C) 2014  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
20 #include "IOLog.h"
21 #include "IOSockets.h"
22 #include "IOTimer.h"
23
24 #include <errno.h>
25 #include <time.h>
26 #include <string.h>
27 #include <stdio.h>
28 #ifdef WIN32
29 #ifdef _WIN32_WINNT
30 #undef _WIN32_WINNT
31 #endif
32 #define _WIN32_WINNT 0x501
33 #include <winsock2.h>
34 #include <windows.h>
35 #elif defined HAVE_SYS_SELECT_H
36 #include <sys/select.h>
37 #endif
38
39 /* compat */
40 #include "compat/utime.h"
41
42 static int engine_select_init() {
43         return 1;
44 }
45
46 static void engine_select_add(struct _IOSocket *iosock) {
47         /* empty */
48 }
49
50 static void engine_select_remove(struct _IOSocket *iosock) {
51         /* empty */
52 }
53
54 static void engine_select_update(struct _IOSocket *iosock) {
55         /* empty */
56 }
57
58 static void engine_select_loop(struct timeval *timeout) {
59         fd_set read_fds;
60         fd_set write_fds;
61         unsigned int fds_size = 0;
62         struct _IOSocket *iosock, *next_iosock;
63         struct timeval now, tout;
64         int select_result;
65         
66         //clear fds
67         FD_ZERO(&read_fds);
68         FD_ZERO(&write_fds);
69         
70         //check timers
71         gettimeofday(&now, NULL);
72         if(iotimer_sorted_descriptors && timeval_is_bigger(now, iotimer_sorted_descriptors->timeout))
73                 _trigger_timer();
74         
75         //get timeout (timer or given timeout)
76         if(iotimer_sorted_descriptors) {
77                 tout = iotimer_sorted_descriptors->timeout;
78                 tout.tv_sec -= now.tv_sec;
79                 tout.tv_usec -= now.tv_usec;
80                 if(tout.tv_usec < 0) {
81                         tout.tv_sec --;
82                         tout.tv_usec += 1000000;
83                 }
84         }
85         if(timeout) {
86                 if(!iotimer_sorted_descriptors || timeval_is_smaler((*timeout), tout)) {
87                         tout.tv_usec = timeout->tv_usec;
88                         tout.tv_sec = timeout->tv_sec;
89                 }
90                 timeout = &tout;
91         } else if(iotimer_sorted_descriptors)
92                 timeout = &tout;
93         
94         select_result = 0;
95         for(iosock = iosocket_first; iosock; iosock = iosock->next) {
96                 if(!(iosock->socket_flags & IOSOCKETFLAG_ACTIVE)) 
97                         continue;
98                 if(iosock->fd > fds_size)
99                         fds_size = iosock->fd;
100                 select_result++;
101                 if(iosocket_wants_reads(iosock))
102                         FD_SET(iosock->fd, &read_fds);
103                 if(iosocket_wants_writes(iosock))
104                         FD_SET(iosock->fd, &write_fds);
105         }
106
107         if(select_result) //select system call
108                 select_result = select(fds_size + 1, &read_fds, &write_fds, NULL, timeout);
109         else if(timeout) {
110                 usleep_tv(*timeout);
111                 select_result = 0;
112         } else
113                 usleep(10000); // 10ms
114         
115         if (select_result < 0) {
116                 if (errno != EINTR) {
117                         iolog_trigger(IOLOG_FATAL, "select() failed with errno %d %d: %s", select_result, errno, strerror(errno));
118                         return;
119                 }
120         }
121         
122         gettimeofday(&now, NULL);
123         
124         //check all descriptors
125         for(iosock = iosocket_first; iosock; iosock = next_iosock) {
126                 next_iosock = iosock->next;
127                 if(!(iosock->socket_flags & IOSOCKETFLAG_ACTIVE))
128                         return;
129                 if(FD_ISSET(iosock->fd, &read_fds) || FD_ISSET(iosock->fd, &write_fds))
130                         iosocket_events_callback(iosock, FD_ISSET(iosock->fd, &read_fds), FD_ISSET(iosock->fd, &write_fds));
131         }
132         
133         //check timers
134         if(iotimer_sorted_descriptors && timeval_is_bigger(now, iotimer_sorted_descriptors->timeout))
135                 _trigger_timer();
136         
137 }
138
139 static void engine_select_cleanup() {
140         /* empty */
141 }
142
143 struct IOEngine engine_select = {
144         .name = "select",
145         .init = engine_select_init,
146         .add = engine_select_add,
147         .remove = engine_select_remove,
148         .update = engine_select_update,
149         .loop = engine_select_loop,
150         .cleanup = engine_select_cleanup,
151 };