Split event.c into per-API files
[tinc] / src / windows / event.c
1 /*
2     event.c -- event support for Windows
3     Copyright (C) 2012-2022 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "../system.h"
21
22 #include <assert.h>
23
24 #include "../event.h"
25 #include "../utils.h"
26 #include "../net.h"
27
28 static bool running = false;
29 static DWORD event_count = 0;
30
31 static const long READ_EVENTS = FD_READ | FD_ACCEPT | FD_CLOSE;
32 static const long WRITE_EVENTS = FD_WRITE | FD_CONNECT;
33
34 void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
35         if(io->cb) {
36                 return;
37         }
38
39         io->fd = fd;
40
41         if(io->fd != -1) {
42                 io->event = WSACreateEvent();
43
44                 if(io->event == WSA_INVALID_EVENT) {
45                         abort();
46                 }
47         }
48
49         event_count++;
50
51         io->cb = cb;
52         io->data = data;
53         io->node.data = io;
54
55         io_set(io, flags);
56
57         if(!splay_insert_node(&io_tree, &io->node)) {
58                 abort();
59         }
60 }
61
62 void io_add_event(io_t *io, io_cb_t cb, void *data, WSAEVENT event) {
63         io->event = event;
64         io_add(io, cb, data, -1, 0);
65 }
66
67 void io_set(io_t *io, int flags) {
68         if(flags == io->flags) {
69                 return;
70         }
71
72         io->flags = flags;
73
74         if(io->fd == -1) {
75                 return;
76         }
77
78         long events = 0;
79
80         if(flags & IO_WRITE) {
81                 events |= WRITE_EVENTS;
82         }
83
84         if(flags & IO_READ) {
85                 events |= READ_EVENTS;
86         }
87
88         if(WSAEventSelect(io->fd, io->event, events) != 0) {
89                 abort();
90         }
91 }
92
93 void io_del(io_t *io) {
94         if(!io->cb) {
95                 return;
96         }
97
98         io_set(io, 0);
99
100         if(io->fd != -1 && WSACloseEvent(io->event) == FALSE) {
101                 abort();
102         }
103
104         event_count--;
105         splay_unlink_node(&io_tree, &io->node);
106         io->cb = NULL;
107 }
108
109 bool event_loop(void) {
110         running = true;
111
112         assert(WSA_WAIT_EVENT_0 == 0);
113
114         while(running) {
115                 struct timeval diff;
116                 struct timeval *tv = timeout_execute(&diff);
117                 DWORD timeout_ms = tv ? (DWORD)(tv->tv_sec * 1000 + tv->tv_usec / 1000 + 1) : WSA_INFINITE;
118
119                 if(!event_count) {
120                         Sleep(timeout_ms);
121                         continue;
122                 }
123
124                 /*
125                    For some reason, Microsoft decided to make the FD_WRITE event edge-triggered instead of level-triggered,
126                    which is the opposite of what select() does. In practice, that means that if a FD_WRITE event triggers,
127                    it will never trigger again until a send() returns EWOULDBLOCK. Since the semantics of this event loop
128                    is that write events are level-triggered (i.e. they continue firing until the socket is full), we need
129                    to emulate these semantics by making sure we fire each IO_WRITE that is still writeable.
130
131                    Note that technically FD_CLOSE has the same problem, but it's okay because user code does not rely on
132                    this event being fired again if ignored.
133                 */
134                 unsigned int curgen = io_tree.generation;
135
136                 for splay_each(io_t, io, &io_tree) {
137                         if(io->flags & IO_WRITE && send(io->fd, NULL, 0, 0) == 0) {
138                                 io->cb(io->data, IO_WRITE);
139
140                                 if(curgen != io_tree.generation) {
141                                         break;
142                                 }
143                         }
144                 }
145
146                 if(event_count > WSA_MAXIMUM_WAIT_EVENTS) {
147                         WSASetLastError(WSA_INVALID_PARAMETER);
148                         return(false);
149                 }
150
151                 WSAEVENT events[WSA_MAXIMUM_WAIT_EVENTS];
152                 io_t *io_map[WSA_MAXIMUM_WAIT_EVENTS];
153                 DWORD event_index = 0;
154
155                 for splay_each(io_t, io, &io_tree) {
156                         events[event_index] = io->event;
157                         io_map[event_index] = io;
158                         event_index++;
159                 }
160
161                 /*
162                  * If the generation number changes due to event addition
163                  * or removal by a callback we restart the loop.
164                  */
165                 curgen = io_tree.generation;
166
167                 for(DWORD event_offset = 0; event_offset < event_count;) {
168                         DWORD result = WSAWaitForMultipleEvents(event_count - event_offset, &events[event_offset], FALSE, timeout_ms, FALSE);
169
170                         if(result == WSA_WAIT_TIMEOUT) {
171                                 break;
172                         }
173
174                         if(result >= event_count - event_offset) {
175                                 return false;
176                         }
177
178                         /* Look up io in the map by index. */
179                         event_index = result + event_offset;
180                         io_t *io = io_map[event_index];
181
182                         if(io->fd == -1) {
183                                 io->cb(io->data, 0);
184
185                                 if(curgen != io_tree.generation) {
186                                         break;
187                                 }
188                         } else {
189                                 WSANETWORKEVENTS network_events;
190
191                                 if(WSAEnumNetworkEvents(io->fd, io->event, &network_events) != 0) {
192                                         return(false);
193                                 }
194
195                                 if(network_events.lNetworkEvents & READ_EVENTS) {
196                                         io->cb(io->data, IO_READ);
197
198                                         if(curgen != io_tree.generation) {
199                                                 break;
200                                         }
201                                 }
202
203                                 /*
204                                     The fd might be available for write too. However, if we already fired the read callback, that
205                                     callback might have deleted the io (e.g. through terminate_connection()), so we can't fire the
206                                     write callback here. Instead, we loop back and let the writable io loop above handle it.
207                                  */
208                         }
209
210                         /* Continue checking the rest of the events. */
211                         event_offset = event_index + 1;
212
213                         /* Just poll the next time through. */
214                         timeout_ms = 0;
215                 }
216         }
217
218         return true;
219 }
220
221 void event_exit(void) {
222         running = false;
223 }