updates.
[silc.git] / lib / silcutil / win32 / silcwin32schedule.c
1 /*
2
3   silcwin32schedule.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 /* Our "select()" for WIN32. This actually is not the select() and does
25    not call Winsock's select() (since it cannot be used for our purposes)
26    but mimics the functions of select(). 
27
28    This is taken from the GLib and is the g_poll function in the Glib.
29    It has been *heavily* modified to be select() like and fit for SILC. */
30
31 int silc_select(int n, fd_set *readfds, fd_set *writefds,
32                 fd_set *exceptfds, struct timeval *timeout)
33 {
34   HANDLE handles[MAXIMUM_WAIT_OBJECTS];
35   DWORD ready;
36   int nhandles = 0;
37   int timeo, i;
38
39   /* Check fd sets (ignoring the exceptfds for now) */
40
41   if (readfds) {
42     for (i = 0; i < n - 1; i++)
43       if (FD_ISSET(i, readfds))
44         handles[nhandles++] = (HANDLE)i;
45   }
46
47   if (writefds) {
48     /* If write fd is set then we just return */
49     for (i = 0; i < n - 1; i++)
50       if (FD_ISSET(i, writefds))
51         return 1;
52   }
53
54   if (!timeout)
55     timeo = INFINITE;
56   else
57     timeo = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000);
58
59  retry:
60   if (nhandles == 0)
61     return -1;
62   else
63     ready = MsgWaitForMultipleObjects(nhandles, handles, FALSE, timeo,
64                                       QS_ALLINPUT);
65
66   if (ready == WAIT_FAILED) {
67     SILC_LOG_WARNING(("WaitForMultipleObjects() failed"));
68     return -1;
69   } else if (ready == WAIT_TIMEOUT) {
70     return 0;
71   } else if (ready == WAIT_OBJECT_0 + nhandles) {
72     /* For Windows messages. The MSDN online says that if the application
73        creates a window then its main loop (and we're assuming that
74        it is our SILC Scheduler) must handle the Windows messages, so do
75        it here as the MSDN suggests. -Pekka */
76     /* For reference: http://msdn.microsoft.com/library/default.asp?
77        url=/library/en-us/winui/hh/winui/messques_77zk.asp */
78     MSG msg;
79
80     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
81       TranslateMessage(&msg); 
82       DispatchMessage(&msg); 
83     }
84
85     /* Bad thing is that I don't know what to return, since actually
86        nothing for us happened. So, make another try with the waiting
87        and do not return. This of course may fuck up the timeouts! */
88     goto retry;
89   } else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles &&
90              readfds) {
91     for (i = 0; i < n - 1; i++) {
92       if (ready - WAIT_OBJECT_0 != i)
93         FD_CLR(i, readfds);
94     }
95
96     /* Always one entry in the fd set. */
97     return 1;
98   }
99
100   return -1;
101 }