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