push
[NextIRCd.git] / src / IOHandler / IOEngine_win32.c
1 /* IOEngine_win32.c - IOMultiplexer
2  * Copyright (C) 2014  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
20 #include "IOLog.h"
21 #include "IOSockets.h"
22 #include "IOTimer.h"
23
24 #ifdef WIN32
25 #ifdef _WIN32_WINNT
26 #undef _WIN32_WINNT
27 #endif
28 #define _WIN32_WINNT 0x501
29 #include <winsock2.h>
30 #include <windows.h>
31
32 /* This is massively kludgy.  Unfortunately, the only performant I/O
33  * multiplexer with halfway decent semantics under Windows is
34  * WSAAsyncSelect() -- which requires a window that can receive
35  * messages.
36  *
37  * So ioset_win32_init() creates a hidden window and sets it up for
38  * asynchronous socket notifications.
39  */
40
41 #define IDT_TIMER1 1000
42 #define IDT_TIMER2 1001
43 #define IDT_SOCKET 1002
44
45 static HWND ioset_window;
46
47 static struct _IOSocket *engine_win32_get_iosock(int fd) {
48         struct _IOSocket *iosock;
49         for(iosock = iosocket_first; iosock; iosock = iosock->next) {
50                 if(iosock->fd == fd)
51                         return iosock;
52         }
53         return NULL;
54 }
55
56 static LRESULT CALLBACK engine_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
57         struct _IOSocket *iosock;
58         int events;
59
60         if (hWnd == ioset_window) {
61                 switch (uMsg) {
62                 case IDT_TIMER1:
63                         return 0;
64                 case IDT_TIMER2:
65                         //check timers
66                         _trigger_timer();
67                         return 0;
68                 case IDT_SOCKET:
69                         iosock = engine_win32_get_iosock(wParam);
70                         if(!iosock)
71                                 return 0;
72                         events = WSAGETSELECTEVENT(lParam);
73                         
74                         if((events & FD_CONNECT)) {
75                                 int err;
76                                 if((err = WSAGETSELECTERROR(lParam)))
77                                         iosocket_events_callback(iosock, err, 0);
78                                 else
79                                         iosocket_events_callback(iosock, 0, 1);
80                         } else
81                                 iosocket_events_callback(iosock, (events & (FD_READ | FD_ACCEPT | FD_CLOSE)) != 0, (events & FD_WRITE) != 0);
82                         return 0;
83                 case WM_QUIT:
84                         return 0;
85                 }
86         }
87         return DefWindowProc(hWnd, uMsg, wParam, lParam);
88 }
89
90 static int engine_win32_init() {
91         WNDCLASSEX wcx;
92         HINSTANCE hinst;
93         WSADATA wsadata;
94         
95         // Start Windows Sockets.
96         if (WSAStartup(MAKEWORD(2, 0), &wsadata)) {
97                 iolog_trigger(IOLOG_FATAL, "Unable to start Windows Sockets");
98                 return 0;
99         }
100         
101         // Get Windows HINSTANCE.
102         hinst = GetModuleHandle(NULL);
103
104         // Describe and register a window class.
105         memset(&wcx, 0, sizeof(wcx));
106         wcx.cbSize = sizeof(wcx);
107         wcx.lpfnWndProc = engine_win32_wndproc;
108         wcx.hInstance = hinst;
109         wcx.lpszClassName = "IOMultiplexerMainWindow";
110         if (!RegisterClassEx(&wcx))
111                 return 0;
112
113         ioset_window = CreateWindow("IOMultiplexerMainWindow", "IOMultiplexer", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinst, NULL);
114         if (!ioset_window)
115                 return 0;
116         return 1;
117 }
118
119 static long engine_win32_events(struct _IOSocket *iosock) {
120         if(iosock->socket_flags & IOSOCKETFLAG_LISTENING)
121                 return FD_ACCEPT;
122         if(iosock->socket_flags & IOSOCKETFLAG_CONNECTING)
123                 return FD_CONNECT;
124         
125         return FD_CLOSE | (iosocket_wants_reads(iosock) ? FD_READ : 0) | (iosocket_wants_writes(iosock) ? FD_WRITE : 0);
126 }
127
128 static void engine_win32_update(struct _IOSocket *iosock) {
129         long events;
130         events = engine_win32_events(iosock);
131         WSAAsyncSelect(iosock->fd, ioset_window, IDT_SOCKET, events);
132 }
133
134 static void engine_win32_add(struct _IOSocket *iosock) {
135         engine_win32_update(iosock);
136 }
137
138 static void engine_win32_remove(struct _IOSocket *iosock) {
139         unsigned long ulong = 0;
140         WSAAsyncSelect(iosock->fd, ioset_window, IDT_SOCKET, 0);
141         ioctlsocket(iosock->fd, FIONBIO, &ulong);
142 }
143
144 static void engine_win32_loop(struct timeval *timeout) {
145         MSG msg;
146         BOOL res;
147         int msec, msec2;
148         struct timeval now;
149         
150         //check timers
151         gettimeofday(&now, NULL);
152         if(iotimer_sorted_descriptors && timeval_is_bigger(now, iotimer_sorted_descriptors->timeout))
153                 _trigger_timer();
154         
155         //get timeout (timer or given timeout)
156         if(iotimer_sorted_descriptors) {
157                 msec = (iotimer_sorted_descriptors->timeout.tv_sec - now.tv_sec) * 1000;
158                 msec += (iotimer_sorted_descriptors->timeout.tv_usec - now.tv_usec) / 1000;
159         }
160         if(timeout) {
161                 msec2 = (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
162                 if(!iotimer_sorted_descriptors || msec2 < msec)
163                         msec = msec2;
164         } else if(!iotimer_sorted_descriptors)
165                 msec = -1;
166         
167         //set TIMER
168         SetTimer(ioset_window, IDT_TIMER1, 1000, NULL);
169         if(msec > -1)
170                 SetTimer(ioset_window, IDT_TIMER2, msec, NULL);
171         
172         //GetMessage system call
173         res = GetMessage(&msg, NULL, 0, 0);
174         
175         //kill TIMER
176         KillTimer(ioset_window, IDT_TIMER1);
177         if(msec > -1)
178                 KillTimer(ioset_window, IDT_TIMER2);
179         
180         if (res <=0)
181                 return;
182         else {
183                 TranslateMessage(&msg);
184                 DispatchMessage(&msg);
185         }
186 }
187
188 static void engine_win32_cleanup() {
189         DestroyWindow(ioset_window);
190         ioset_window = NULL;
191         WSACleanup();
192 }
193
194 struct IOEngine engine_win32 = {
195         .name = "win32",
196         .init = engine_win32_init,
197         .add = engine_win32_add,
198         .remove = engine_win32_remove,
199         .update = engine_win32_update,
200         .loop = engine_win32_loop,
201         .cleanup = engine_win32_cleanup,
202 };
203
204 #else
205
206 struct IOEngine engine_win32 = {
207         .name = "win32",
208         .init = NULL,
209         .add = NULL,
210         .remove = NULL,
211         .update = NULL,
212         .loop = NULL,
213         .cleanup = NULL,
214 };
215
216 #endif