[IOMultiplexerV2] tidied up _IOSocket socket_flag definitions and added possibility...
[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
26 #define _WIN32_WINNT 0x501
27 #include <windows.h>
28 #include <winsock2.h>
29
30 /* This is massively kludgy.  Unfortunately, the only performant I/O
31  * multiplexer with halfway decent semantics under Windows is
32  * WSAAsyncSelect() -- which requires a window that can receive
33  * messages.
34  *
35  * So ioset_win32_init() creates a hidden window and sets it up for
36  * asynchronous socket notifications.
37  */
38
39 #define IDT_TIMER1 1000
40 #define IDT_TIMER2 1001
41 #define IDT_SOCKET 1002
42
43 static HWND ioset_window;
44
45 static struct _IOSocket *engine_win32_get_iosock(int fd) {
46         struct _IOSocket *iosock;
47         for(iosock = iosocket_first; iosock; iosock = iosock->next) {
48                 if(iosock->fd == fd)
49                         return iosock;
50         }
51         return NULL;
52 }
53
54 static LRESULT CALLBACK engine_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
55         struct _IOSocket *iosock;
56         int events;
57
58         if (hWnd == ioset_window) {
59                 switch (uMsg) {
60                 case IDT_TIMER1:
61                         return 0;
62                 case IDT_TIMER2:
63                         //check timers
64                         _trigger_timer();
65                         return 0;
66                 case IDT_SOCKET:
67                         iosock = engine_win32_get_iosock(wParam);
68                         events = WSAGETSELECTEVENT(lParam);
69                         
70                         if((events & FD_CONNECT)) {
71                                 int err;
72                                 if((err = WSAGETSELECTERROR(lParam)))
73                                         iosocket_events_callback(iosock, err, 0);
74                                 else
75                                         iosocket_events_callback(iosock, 0, 1);
76                         } else
77                                 iosocket_events_callback(iosock, (events & (FD_READ | FD_ACCEPT | FD_CLOSE)) != 0, (events & FD_WRITE) != 0);
78                         return 0;
79                 case WM_QUIT:
80                         return 0;
81                 }
82         }
83         return DefWindowProc(hWnd, uMsg, wParam, lParam);
84 }
85
86 static int engine_win32_init() {
87         WNDCLASSEX wcx;
88         HINSTANCE hinst;
89         WSADATA wsadata;
90         
91         // Start Windows Sockets.
92         if (WSAStartup(MAKEWORD(2, 0), &wsadata)) {
93                 iolog_trigger(IOLOG_FATAL, "Unable to start Windows Sockets");
94                 return 0;
95         }
96         
97         // Get Windows HINSTANCE.
98         hinst = GetModuleHandle(NULL);
99
100         // Describe and register a window class.
101         memset(&wcx, 0, sizeof(wcx));
102         wcx.cbSize = sizeof(wcx);
103         wcx.lpfnWndProc = engine_win32_wndproc;
104         wcx.hInstance = hinst;
105         wcx.lpszClassName = "IOMultiplexerMainWindow";
106         if (!RegisterClassEx(&wcx))
107                 return 0;
108
109         ioset_window = CreateWindow("IOMultiplexerMainWindow", "IOMultiplexer", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinst, NULL);
110         if (!ioset_window)
111                 return 0;
112         return 1;
113 }
114
115 static long engine_win32_events(struct _IOSocket *iosock) {
116         if(iosock->socket_flags & IOSOCKETFLAG_LISTENING)
117                 return FD_ACCEPT;
118         if(iosock->socket_flags & IOSOCKETFLAG_CONNECTING)
119                 return FD_CONNECT;
120         
121         return FD_CLOSE | (iosocket_wants_reads(iosock) ? FD_READ : 0) | (iosocket_wants_writes(iosock) ? FD_WRITE : 0);
122 }
123
124 static void engine_win32_update(struct _IOSocket *iosock) {
125         long events;
126         events = engine_win32_events(iosock);
127         WSAAsyncSelect(iosock->fd, ioset_window, IDT_SOCKET, events);
128 }
129
130 static void engine_win32_add(struct _IOSocket *iosock) {
131         engine_win32_update(iosock);
132 }
133
134 static void engine_win32_remove(struct _IOSocket *iosock) {
135         unsigned long ulong = 0;
136         WSAAsyncSelect(iosock->fd, ioset_window, IDT_SOCKET, 0);
137         ioctlsocket(iosock->fd, FIONBIO, &ulong);
138 }
139
140 static void engine_win32_loop(struct timeval *timeout) {
141         MSG msg;
142         BOOL res;
143         int msec, msec2;
144         struct timeval now;
145         
146         //check timers
147         gettimeofday(&now, NULL);
148         if(iotimer_sorted_descriptors && timeval_is_bigger(now, iotimer_sorted_descriptors->timeout))
149                 _trigger_timer();
150         
151         //get timeout (timer or given timeout)
152         if(iotimer_sorted_descriptors) {
153                 msec = (iotimer_sorted_descriptors->timeout.tv_sec - now.tv_sec) * 1000;
154                 msec += (iotimer_sorted_descriptors->timeout.tv_usec - now.tv_usec) / 1000;
155         }
156         if(timeout) {
157                 msec2 = (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
158                 if(!iotimer_sorted_descriptors || msec2 < msec)
159                         msec = msec2;
160         } else if(!iotimer_sorted_descriptors)
161                 msec = -1;
162         
163         //set TIMER
164         SetTimer(ioset_window, IDT_TIMER1, 1000, NULL);
165         if(msec > -1)
166                 SetTimer(ioset_window, IDT_TIMER2, msec, NULL);
167         
168         //GetMessage system call
169         res = GetMessage(&msg, NULL, 0, 0);
170         
171         //kill TIMER
172         KillTimer(ioset_window, IDT_TIMER1);
173         if(msec > -1)
174                 KillTimer(ioset_window, IDT_TIMER2);
175         
176         if (res <=0)
177                 return;
178         else {
179                 TranslateMessage(&msg);
180                 DispatchMessage(&msg);
181         }
182 }
183
184 static void engine_win32_cleanup() {
185         DestroyWindow(ioset_window);
186         ioset_window = NULL;
187         WSACleanup();
188 }
189
190 struct IOEngine engine_win32 = {
191         .name = "win32",
192         .init = engine_win32_init,
193         .add = engine_win32_add,
194         .remove = engine_win32_remove,
195         .update = engine_win32_update,
196         .loop = engine_win32_loop,
197         .cleanup = engine_win32_cleanup,
198 };
199
200 #else
201
202 struct IOEngine engine_win32 = {
203         .name = "win32",
204         .init = NULL,
205         .add = NULL,
206         .remove = NULL,
207         .update = NULL,
208         .loop = NULL,
209         .cleanup = NULL,
210 };
211
212 #endif