2 event.c -- event support for Windows
3 Copyright (C) 2012-2022 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
20 #include "../system.h"
28 static bool running = false;
29 static DWORD event_count = 0;
31 static const long READ_EVENTS = FD_READ | FD_ACCEPT | FD_CLOSE;
32 static const long WRITE_EVENTS = FD_WRITE | FD_CONNECT;
34 void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
42 io->event = WSACreateEvent();
44 if(io->event == WSA_INVALID_EVENT) {
57 if(!splay_insert_node(&io_tree, &io->node)) {
62 void io_add_event(io_t *io, io_cb_t cb, void *data, WSAEVENT event) {
64 io_add(io, cb, data, -1, 0);
67 void io_set(io_t *io, int flags) {
68 if(flags == io->flags) {
80 if(flags & IO_WRITE) {
81 events |= WRITE_EVENTS;
85 events |= READ_EVENTS;
88 if(WSAEventSelect(io->fd, io->event, events) != 0) {
93 void io_del(io_t *io) {
100 if(io->fd != -1 && WSACloseEvent(io->event) == FALSE) {
105 splay_unlink_node(&io_tree, &io->node);
109 bool event_loop(void) {
112 assert(WSA_WAIT_EVENT_0 == 0);
116 struct timeval *tv = timeout_execute(&diff);
117 DWORD timeout_ms = tv ? (DWORD)(tv->tv_sec * 1000 + tv->tv_usec / 1000 + 1) : WSA_INFINITE;
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.
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.
134 unsigned int curgen = io_tree.generation;
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);
140 if(curgen != io_tree.generation) {
146 if(event_count > WSA_MAXIMUM_WAIT_EVENTS) {
147 WSASetLastError(WSA_INVALID_PARAMETER);
151 WSAEVENT events[WSA_MAXIMUM_WAIT_EVENTS];
152 io_t *io_map[WSA_MAXIMUM_WAIT_EVENTS];
153 DWORD event_index = 0;
155 for splay_each(io_t, io, &io_tree) {
156 events[event_index] = io->event;
157 io_map[event_index] = io;
162 * If the generation number changes due to event addition
163 * or removal by a callback we restart the loop.
165 curgen = io_tree.generation;
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);
170 if(result == WSA_WAIT_TIMEOUT) {
174 if(result >= event_count - event_offset) {
178 /* Look up io in the map by index. */
179 event_index = result + event_offset;
180 io_t *io = io_map[event_index];
185 if(curgen != io_tree.generation) {
189 WSANETWORKEVENTS network_events;
191 if(WSAEnumNetworkEvents(io->fd, io->event, &network_events) != 0) {
195 if(network_events.lNetworkEvents & READ_EVENTS) {
196 io->cb(io->data, IO_READ);
198 if(curgen != io_tree.generation) {
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.
210 /* Continue checking the rest of the events. */
211 event_offset = event_index + 1;
213 /* Just poll the next time through. */
221 void event_exit(void) {