5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "silcincludes.h"
26 /* Our "select()" for WIN32. This actually is not the select() and does
27 not call Winsock's select() (since it cannot be used for our purposes)
28 but mimics the functions of select().
30 This is taken from the GLib and is the g_poll function in the Glib.
31 It has been *heavily* modified to be select() like and fit for SILC. */
33 int silc_select(int n, fd_set *readfds, fd_set *writefds,
34 fd_set *exceptfds, struct timeval *timeout)
36 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
41 /* Check fd sets (ignoring the exceptfds for now) */
44 for (i = 0; i < n - 1; i++)
45 if (FD_ISSET(i, readfds))
46 handles[nhandles++] = (HANDLE)i;
50 /* If write fd is set then we just return */
51 for (i = 0; i < n - 1; i++)
52 if (FD_ISSET(i, writefds))
59 timeo = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000);
65 ready = WaitForMultipleObjects(nhandles, handles, FALSE, timeo,
68 if (ready == WAIT_FAILED) {
69 SILC_LOG_WARNING(("WaitForMultipleObjects() failed"));
71 } else if (ready == WAIT_TIMEOUT) {
73 } else if (ready == WAIT_OBJECT_0 + nhandles) {
74 /* For Windows messages. The MSDN online says that if the application
75 creates a window then its main loop (and we're assuming that
76 it is our SILC Scheduler) must handle the Windows messages, so do
77 it here as the MSDN suggests. -Pekka */
78 /* For reference: http://msdn.microsoft.com/library/default.asp?
79 url=/library/en-us/winui/hh/winui/messques_77zk.asp */
82 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
83 TranslateMessage(&msg);
84 DispatchMessage(&msg);
87 /* Bad thing is that I don't know what to return, since actually
88 nothing for us happened. So, make another try with the waiting
89 and do not return. This of course may fuck up the timeouts! */
91 } else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles &&
93 for (i = 0; i < n - 1; i++) {
94 if (ready - WAIT_OBJECT_0 != i)
98 /* Always one entry in the fd set. */