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