1 /* Win32 ioset backend for srvx
2 * Copyright 2006 srvx Development Team
4 * This file is part of srvx.
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include "ioset-impl.h"
25 /* This is massively kludgy. Unfortunately, the only performant I/O
26 * multiplexer with halfway decent semantics under Windows is
27 * WSAAsyncSelect() -- which requires a window that can receive
30 * So ioset_win32_init() creates a hidden window and sets it up for
31 * asynchronous socket notifications.
34 #define IDT_TIMER1 1000
35 #define IDT_SOCKET 1001
37 static HWND ioset_window;
38 static struct io_fd **fds;
39 static unsigned int fds_used;
40 static unsigned int fds_size;
46 int upper = fds_used - 1;
48 while (lower <= upper)
50 int mid = (upper + lower) / 2;
51 if (fd < fds[mid]->fd)
53 else if (fd > fds[mid]->fd)
62 io_fd_from_socket(int fd)
66 return ((ii < fds_used) && (fds[ii]->fd == fd)) ? fds[ii] : NULL;
69 static LRESULT CALLBACK
70 ioset_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
76 if (hWnd == ioset_window) switch (uMsg)
81 fd = io_fd_from_socket(wParam);
82 events = WSAGETSELECTEVENT(lParam);
83 err = WSAGETSELECTERROR(lParam);
84 ioset_events(fd, (events & (FD_READ | FD_ACCEPT | FD_CLOSE)) != 0, (events & (FD_WRITE | FD_CONNECT)) != 0);
87 quit_services = wParam;
90 return DefWindowProc(hWnd, uMsg, wParam, lParam);
94 ioset_win32_init(void)
101 // Start Windows Sockets.
102 res = WSAStartup(MAKEWORD(2, 0), &wsadata);
105 log_module(MAIN_LOG, LOG_FATAL, "Unable to start Windows Sockets (%d)", res);
108 // Get Windows HINSTANCE.
109 hinst = GetModuleHandle(NULL);
111 // Describe and register a window class.
112 memset(&wcx, 0, sizeof(wcx));
113 wcx.cbSize = sizeof(wcx);
114 wcx.lpfnWndProc = ioset_win32_wndproc;
115 wcx.hInstance = hinst;
116 wcx.lpszClassName = "srvxMainWindow";
117 if (!RegisterClassEx(&wcx))
119 log_module(MAIN_LOG, LOG_FATAL, "Unable to register window class (%lu)", GetLastError());
122 ioset_window = CreateWindow("srvxMainWindow", PACKAGE_STRING, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinst, NULL);
125 log_module(MAIN_LOG, LOG_FATAL, "Unable to create window (%lu)", GetLastError());
132 ioset_win32_events(const struct io_fd *fd)
143 return FD_READ | FD_CLOSE | (fd_wants_writes(fd) ? FD_WRITE : 0);
148 ioset_win32_update(struct io_fd *fd)
153 events = ioset_win32_events(fd);
154 rc = WSAAsyncSelect(fd->fd, ioset_window, IDT_SOCKET, events);
157 log_module(MAIN_LOG, LOG_ERROR, "Unable to add events %#lx for fd %#x (%d)", events, fd->fd, WSAGetLastError());
162 ioset_win32_add(struct io_fd *fd)
167 // Make sure fds[] can hold a new entry.
168 if (fds_used + 1 >= fds_size)
170 struct io_fd **new_fds;
171 unsigned int new_size;
173 new_size = (fds_size < 8) ? 8 : fds_size * 2;
174 new_fds = calloc(new_size * 2, sizeof(new_fds[0]));
177 log_module(MAIN_LOG, LOG_FATAL, "Unable to allocate %u-entry socket array.", new_size);
179 for (ii = 0; ii < fds_used; ++ii)
180 new_fds[ii] = fds[ii];
186 // Insert fd into the appropriate spot in fds[].
187 pos = io_fd_pos(fd->fd);
188 for (ii = pos; ii < fds_used; ++ii)
189 fds[ii + 1] = fds[ii];
193 // Ask the OS for future notifications.
194 ioset_win32_update(fd);
198 ioset_win32_remove(struct io_fd *fd, int os_closed)
203 // Unregister from the OS.
208 rc = WSAAsyncSelect(fd->fd, ioset_window, IDT_SOCKET, 0);
211 log_module(MAIN_LOG, LOG_ERROR, "Unable to remove events for fd %#x (%d)", fd->fd, WSAGetLastError());
215 ioctlsocket(fd->fd, FIONBIO, &ulong);
218 // Remove from the fds[] array.
219 pos = io_fd_pos(fd->fd);
220 for (--fds_used; pos < fds_used; ++pos)
221 fds[pos] = fds[pos + 1];
225 ioset_win32_cleanup(void)
227 DestroyWindow(ioset_window);
233 ioset_win32_loop(struct timeval *timeout)
236 BOOL not_really_bool;
239 // Make sure we are woken up after the appropriate time.
240 msec = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
241 SetTimer(ioset_window, IDT_TIMER1, msec, NULL);
242 // Do a blocking read of the message queue.
243 not_really_bool = GetMessage(&msg, NULL, 0, 0);
244 KillTimer(ioset_window, IDT_TIMER1);
245 if (not_really_bool < 0)
249 else if (not_really_bool == 0)
256 TranslateMessage(&msg);
257 DispatchMessage(&msg);
262 struct io_engine io_engine_win32 = {
264 .init = ioset_win32_init,
265 .add = ioset_win32_add,
266 .remove = ioset_win32_remove,
267 .update = ioset_win32_update,
268 .loop = ioset_win32_loop,
269 .cleanup = ioset_win32_cleanup,