]>
Commit | Line | Data |
---|---|---|
b7ceceb1 DE |
1 | /* ------------------------------------------------------------------------- |
2 | * Project: GSocket (Generic Socket) | |
79630806 | 3 | * Name: gsockmsw.cpp |
b7ceceb1 DE |
4 | * Author: Guillermo Rodriguez Garcia <guille@iies.es> |
5 | * Purpose: GSocket GUI-specific MSW code | |
6 | * CVSID: $Id$ | |
7 | * ------------------------------------------------------------------------- | |
8 | */ | |
9 | ||
86afa786 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
b7ceceb1 | 17 | /* |
dbfb61b3 | 18 | * DONE: for WinCE we need to replace WSAAsyncSelect |
b7ceceb1 DE |
19 | * (Windows message-based notification of network events for a socket) |
20 | * with another mechanism. | |
dbfb61b3 JS |
21 | * As WSAAsyncSelect is not present on WinCE, it now uses |
22 | * WSACreateEvent, WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents. | |
23 | * When enabling eventhandling for a socket a new thread it created that keeps track of the events | |
24 | * and posts a messageto the hidden window to use the standard message loop. | |
b7ceceb1 DE |
25 | */ |
26 | ||
b7ceceb1 DE |
27 | /* including rasasync.h (included from windows.h itself included from |
28 | * wx/setup.h and/or winsock.h results in this warning for | |
29 | * RPCNOTIFICATION_ROUTINE | |
30 | */ | |
31 | #ifdef _MSC_VER | |
1c8681b4 | 32 | # pragma warning(disable:4115) /* named type definition in parentheses */ |
b7ceceb1 DE |
33 | #endif |
34 | ||
35 | /* This needs to be before the wx/defs/h inclusion | |
36 | * for some reason | |
37 | */ | |
38 | ||
39 | #ifdef __WXWINCE__ | |
1c8681b4 DE |
40 | /* windows.h results in tons of warnings at max warning level */ |
41 | # ifdef _MSC_VER | |
42 | # pragma warning(push, 1) | |
43 | # endif | |
44 | # include <windows.h> | |
45 | # ifdef _MSC_VER | |
46 | # pragma warning(pop) | |
47 | # pragma warning(disable:4514) | |
48 | # endif | |
b7ceceb1 DE |
49 | #endif |
50 | ||
51 | #ifndef __GSOCKET_STANDALONE__ | |
52 | # include "wx/platform.h" | |
53 | # include "wx/setup.h" | |
54 | #endif | |
55 | ||
56 | #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) | |
57 | ||
58 | #ifndef __GSOCKET_STANDALONE__ | |
59 | ||
60 | #include "wx/msw/gsockmsw.h" | |
61 | #include "wx/gsocket.h" | |
62 | ||
ebb97a41 | 63 | extern "C" WXDLLIMPEXP_BASE HINSTANCE wxGetInstance(void); |
b7ceceb1 DE |
64 | #define INSTANCE wxGetInstance() |
65 | ||
66 | #else | |
67 | ||
68 | #include "gsockmsw.h" | |
69 | #include "gsocket.h" | |
70 | ||
77ffb593 | 71 | /* If not using wxWidgets, a global var called hInst must |
b7ceceb1 DE |
72 | * be available and it must contain the app's instance |
73 | * handle. | |
74 | */ | |
dbfb61b3 | 75 | extern HINSTANCE hInst; |
b7ceceb1 DE |
76 | #define INSTANCE hInst |
77 | ||
78 | #endif /* __GSOCKET_STANDALONE__ */ | |
79 | ||
80 | #ifndef __WXWINCE__ | |
81 | #include <assert.h> | |
82 | #else | |
83 | #define assert(x) | |
84 | #include <winsock.h> | |
85 | #include "wx/msw/wince/net.h" | |
86 | #endif | |
87 | ||
88 | #include <string.h> | |
89 | #include <stdio.h> | |
90 | #include <stdlib.h> | |
91 | #include <stddef.h> | |
92 | #include <ctype.h> | |
93 | ||
94 | #include <winsock.h> | |
95 | ||
96 | #ifdef _MSC_VER | |
97 | # pragma warning(default:4115) /* named type definition in parentheses */ | |
98 | #endif | |
99 | ||
100 | #define CLASSNAME TEXT("_GSocket_Internal_Window_Class") | |
101 | ||
102 | /* implemented in utils.cpp */ | |
32524c8f | 103 | extern "C" WXDLLIMPEXP_BASE HWND |
b7ceceb1 DE |
104 | wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc); |
105 | ||
106 | /* Maximum number of different GSocket objects at a given time. | |
107 | * This value can be modified at will, but it CANNOT be greater | |
108 | * than (0x7FFF - WM_USER + 1) | |
109 | */ | |
110 | #define MAXSOCKETS 1024 | |
111 | ||
112 | #if (MAXSOCKETS > (0x7FFF - WM_USER + 1)) | |
113 | #error "MAXSOCKETS is too big!" | |
114 | #endif | |
115 | ||
dbfb61b3 | 116 | #ifndef __WXWINCE__ |
1c8681b4 | 117 | typedef int (PASCAL *WSAAsyncSelectFunc)(SOCKET,HWND,u_int,long); |
dbfb61b3 JS |
118 | #else |
119 | /* Typedef the needed function prototypes and the WSANETWORKEVENTS structure | |
120 | */ | |
121 | typedef struct _WSANETWORKEVENTS { | |
122 | long lNetworkEvents; | |
123 | int iErrorCode[10]; | |
124 | } WSANETWORKEVENTS, FAR * LPWSANETWORKEVENTS; | |
125 | typedef HANDLE (PASCAL *WSACreateEventFunc)(void); | |
126 | typedef int (PASCAL *WSAEventSelectFunc)(SOCKET,HANDLE,long); | |
127 | typedef int (PASCAL *WSAWaitForMultipleEventsFunc)(long,HANDLE,BOOL,long,BOOL); | |
128 | typedef int (PASCAL *WSAEnumNetworkEventsFunc)(SOCKET,HANDLE,LPWSANETWORKEVENTS); | |
129 | #endif //__WXWINCE__ | |
b7ceceb1 | 130 | |
c90cc42e DE |
131 | LRESULT CALLBACK _GSocket_Internal_WinProc(HWND, UINT, WPARAM, LPARAM); |
132 | ||
b7ceceb1 DE |
133 | /* Global variables */ |
134 | ||
dbfb61b3 | 135 | extern HINSTANCE INSTANCE; |
b7ceceb1 DE |
136 | static HWND hWin; |
137 | static CRITICAL_SECTION critical; | |
138 | static GSocket* socketList[MAXSOCKETS]; | |
139 | static int firstAvailable; | |
dbfb61b3 JS |
140 | |
141 | #ifndef __WXWINCE__ | |
1c8681b4 | 142 | static WSAAsyncSelectFunc gs_WSAAsyncSelect = NULL; |
dbfb61b3 JS |
143 | #else |
144 | /* Setup WinCE specific stuff | |
145 | */ | |
146 | static socket_running; | |
147 | static unsigned int currSocket; | |
148 | HANDLE hThread[MAXSOCKETS]; | |
149 | static WSACreateEventFunc gs_WSACreateEvent = NULL; | |
150 | static WSAEventSelectFunc gs_WSAEventSelect = NULL; | |
151 | static WSAWaitForMultipleEventsFunc gs_WSAWaitForMultipleEvents = NULL; | |
152 | static WSAEnumNetworkEventsFunc gs_WSAEnumNetworkEvents = NULL; | |
153 | /* This structure will be used to pass data on to the thread that handles socket events. | |
154 | */ | |
155 | typedef struct thread_data{ | |
1c6dd11c WS |
156 | HWND hEvtWin; |
157 | unsigned long msgnumber; | |
158 | unsigned long fd; | |
159 | unsigned long lEvent; | |
dbfb61b3 JS |
160 | }thread_data; |
161 | #endif | |
162 | ||
1c8681b4 | 163 | static HMODULE gs_wsock32dll = 0; |
b7ceceb1 | 164 | |
dbfb61b3 JS |
165 | |
166 | #ifdef __WXWINCE__ | |
167 | /* This thread handles socket events on WinCE using WSAEventSelect() as WSAAsyncSelect is not supported. | |
168 | * When an event occures for the socket, it is checked what kind of event happend and the correct message gets posted | |
169 | * so that the hidden window can handle it as it would in other MSW builds. | |
170 | */ | |
171 | DWORD WINAPI SocketThread(LPVOID data) | |
172 | { | |
1c6dd11c WS |
173 | WSANETWORKEVENTS NetworkEvents; |
174 | thread_data* d = (thread_data *)data; | |
175 | ||
176 | HANDLE NetworkEvent = gs_WSACreateEvent(); | |
177 | gs_WSAEventSelect(d->fd, NetworkEvent, d->lEvent); | |
178 | ||
179 | while(socket_running) | |
180 | { | |
181 | if ((gs_WSAWaitForMultipleEvents(1, &NetworkEvent, FALSE,INFINITE, FALSE)) == WAIT_FAILED) | |
182 | { | |
183 | printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError()); | |
184 | return 0; | |
185 | } | |
186 | if (gs_WSAEnumNetworkEvents(d->fd ,NetworkEvent, &NetworkEvents) == SOCKET_ERROR) | |
187 | { | |
188 | printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError()); | |
189 | return 0; | |
190 | } | |
191 | ||
192 | long flags = NetworkEvents.lNetworkEvents; | |
193 | if (flags & FD_READ) | |
194 | ::PostMessage(d->hEvtWin, d->msgnumber,d->fd, FD_READ); | |
195 | if (flags & FD_WRITE) | |
196 | ::PostMessage(d->hEvtWin, d->msgnumber,d->fd, FD_WRITE); | |
197 | if (flags & FD_OOB) | |
198 | ::PostMessage(d->hEvtWin, d->msgnumber,d->fd, FD_OOB); | |
199 | if (flags & FD_ACCEPT) | |
200 | ::PostMessage(d->hEvtWin, d->msgnumber,d->fd, FD_ACCEPT); | |
201 | if (flags & FD_CONNECT) | |
202 | ::PostMessage(d->hEvtWin, d->msgnumber,d->fd, FD_CONNECT); | |
203 | if (flags & FD_CLOSE) | |
204 | ::PostMessage(d->hEvtWin, d->msgnumber,d->fd, FD_CLOSE); | |
205 | ||
206 | } | |
207 | gs_WSAEventSelect(d->fd, NetworkEvent, 0); | |
208 | ExitThread(0); | |
209 | return 0; | |
dbfb61b3 JS |
210 | } |
211 | #endif | |
212 | ||
213 | ||
c90cc42e | 214 | bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop() |
1c6dd11c WS |
215 | { |
216 | return true; | |
217 | } | |
c90cc42e | 218 | |
b7ceceb1 DE |
219 | /* Global initializers */ |
220 | ||
c90cc42e | 221 | bool GSocketGUIFunctionsTableConcrete::OnInit() |
b7ceceb1 | 222 | { |
1c6dd11c WS |
223 | static LPCTSTR pclassname = NULL; |
224 | int i; | |
b7ceceb1 | 225 | |
1c6dd11c WS |
226 | /* Create internal window for event notifications */ |
227 | hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, _GSocket_Internal_WinProc); | |
228 | if (!hWin) | |
229 | return false; | |
b7ceceb1 | 230 | |
1c6dd11c WS |
231 | /* Initialize socket list */ |
232 | InitializeCriticalSection(&critical); | |
b7ceceb1 | 233 | |
1c6dd11c WS |
234 | for (i = 0; i < MAXSOCKETS; i++) |
235 | { | |
236 | socketList[i] = NULL; | |
237 | } | |
238 | firstAvailable = 0; | |
b7ceceb1 | 239 | |
1c6dd11c WS |
240 | /* Load WSAAsyncSelect from wsock32.dll (we don't link against it |
241 | statically to avoid dependency on wsock32.dll for apps that don't use | |
242 | sockets): */ | |
dbfb61b3 | 243 | #ifndef __WXWINCE__ |
1c6dd11c WS |
244 | gs_wsock32dll = LoadLibrary(wxT("wsock32.dll")); |
245 | if (!gs_wsock32dll) | |
246 | return false; | |
247 | gs_WSAAsyncSelect =(WSAAsyncSelectFunc)GetProcAddress(gs_wsock32dll, | |
248 | "WSAAsyncSelect"); | |
249 | if (!gs_WSAAsyncSelect) | |
250 | return false; | |
dbfb61b3 JS |
251 | #else |
252 | /* On WinCE we load ws2.dll which will provide the needed functions. | |
253 | */ | |
1c6dd11c WS |
254 | gs_wsock32dll = LoadLibrary(wxT("ws2.dll")); |
255 | if (!gs_wsock32dll) | |
256 | return false; | |
257 | gs_WSAEventSelect =(WSAEventSelectFunc)GetProcAddress(gs_wsock32dll, | |
258 | wxT("WSAEventSelect")); | |
259 | if (!gs_WSAEventSelect) | |
260 | return false; | |
261 | ||
262 | gs_WSACreateEvent =(WSACreateEventFunc)GetProcAddress(gs_wsock32dll, | |
263 | wxT("WSACreateEvent")); | |
264 | if (!gs_WSACreateEvent) | |
265 | return false; | |
266 | ||
267 | gs_WSAWaitForMultipleEvents =(WSAWaitForMultipleEventsFunc)GetProcAddress(gs_wsock32dll, | |
268 | wxT("WSAWaitForMultipleEvents")); | |
269 | if (!gs_WSAWaitForMultipleEvents) | |
270 | return false; | |
271 | ||
272 | gs_WSAEnumNetworkEvents =(WSAEnumNetworkEventsFunc)GetProcAddress(gs_wsock32dll, | |
273 | wxT("WSAEnumNetworkEvents")); | |
274 | if (!gs_WSAEnumNetworkEvents) | |
275 | return false; | |
276 | ||
277 | currSocket = 0; | |
dbfb61b3 | 278 | #endif |
1c6dd11c WS |
279 | |
280 | return true; | |
b7ceceb1 DE |
281 | } |
282 | ||
c90cc42e | 283 | void GSocketGUIFunctionsTableConcrete::OnExit() |
b7ceceb1 | 284 | { |
dbfb61b3 | 285 | #ifdef __WXWINCE__ |
1c6dd11c WS |
286 | /* Delete the threads here */ |
287 | for(unsigned int i=0; i < currSocket; i++) | |
288 | CloseHandle(hThread[i]); | |
dbfb61b3 | 289 | #endif |
b7ceceb1 DE |
290 | /* Destroy internal window */ |
291 | DestroyWindow(hWin); | |
292 | UnregisterClass(CLASSNAME, INSTANCE); | |
293 | ||
1c8681b4 DE |
294 | /* Unlock wsock32.dll */ |
295 | if (gs_wsock32dll) | |
296 | { | |
297 | FreeLibrary(gs_wsock32dll); | |
298 | gs_wsock32dll = 0; | |
299 | } | |
300 | ||
b7ceceb1 DE |
301 | /* Delete critical section */ |
302 | DeleteCriticalSection(&critical); | |
303 | } | |
304 | ||
305 | /* Per-socket GUI initialization / cleanup */ | |
306 | ||
c90cc42e | 307 | bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket) |
b7ceceb1 DE |
308 | { |
309 | int i; | |
310 | ||
311 | /* Allocate a new message number for this socket */ | |
312 | EnterCriticalSection(&critical); | |
313 | ||
314 | i = firstAvailable; | |
315 | while (socketList[i] != NULL) | |
316 | { | |
317 | i = (i + 1) % MAXSOCKETS; | |
318 | ||
319 | if (i == firstAvailable) /* abort! */ | |
320 | { | |
321 | LeaveCriticalSection(&critical); | |
c90cc42e | 322 | return false; |
b7ceceb1 DE |
323 | } |
324 | } | |
325 | socketList[i] = socket; | |
326 | firstAvailable = (i + 1) % MAXSOCKETS; | |
327 | socket->m_msgnumber = (i + WM_USER); | |
328 | ||
329 | LeaveCriticalSection(&critical); | |
330 | ||
c90cc42e | 331 | return true; |
b7ceceb1 DE |
332 | } |
333 | ||
c90cc42e | 334 | void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket) |
b7ceceb1 DE |
335 | { |
336 | /* Remove the socket from the list */ | |
337 | EnterCriticalSection(&critical); | |
338 | socketList[(socket->m_msgnumber - WM_USER)] = NULL; | |
339 | LeaveCriticalSection(&critical); | |
340 | } | |
341 | ||
342 | /* Windows proc for asynchronous event handling */ | |
343 | ||
344 | LRESULT CALLBACK _GSocket_Internal_WinProc(HWND hWnd, | |
345 | UINT uMsg, | |
346 | WPARAM wParam, | |
347 | LPARAM lParam) | |
348 | { | |
349 | GSocket *socket; | |
350 | GSocketEvent event; | |
351 | GSocketCallback cback; | |
352 | char *data; | |
353 | ||
354 | if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1)) | |
355 | { | |
356 | EnterCriticalSection(&critical); | |
357 | socket = socketList[(uMsg - WM_USER)]; | |
358 | event = (GSocketEvent) -1; | |
359 | cback = NULL; | |
360 | data = NULL; | |
361 | ||
362 | /* Check that the socket still exists (it has not been | |
363 | * destroyed) and for safety, check that the m_fd field | |
364 | * is what we expect it to be. | |
365 | */ | |
366 | if ((socket != NULL) && (socket->m_fd == wParam)) | |
367 | { | |
368 | switch WSAGETSELECTEVENT(lParam) | |
369 | { | |
370 | case FD_READ: event = GSOCK_INPUT; break; | |
371 | case FD_WRITE: event = GSOCK_OUTPUT; break; | |
372 | case FD_ACCEPT: event = GSOCK_CONNECTION; break; | |
373 | case FD_CONNECT: | |
374 | { | |
375 | if (WSAGETSELECTERROR(lParam) != 0) | |
376 | event = GSOCK_LOST; | |
377 | else | |
378 | event = GSOCK_CONNECTION; | |
379 | break; | |
380 | } | |
381 | case FD_CLOSE: event = GSOCK_LOST; break; | |
382 | } | |
383 | ||
384 | if (event != -1) | |
385 | { | |
386 | cback = socket->m_cbacks[event]; | |
387 | data = socket->m_data[event]; | |
388 | ||
389 | if (event == GSOCK_LOST) | |
390 | socket->m_detected = GSOCK_LOST_FLAG; | |
391 | else | |
392 | socket->m_detected |= (1 << event); | |
393 | } | |
394 | } | |
395 | ||
396 | /* OK, we can now leave the critical section because we have | |
397 | * already obtained the callback address (we make no further | |
398 | * accesses to socket->whatever). However, the app should | |
399 | * be prepared to handle events from a socket that has just | |
400 | * been closed! | |
401 | */ | |
402 | LeaveCriticalSection(&critical); | |
403 | ||
404 | if (cback != NULL) | |
405 | (cback)(socket, event, data); | |
406 | ||
407 | return (LRESULT) 0; | |
408 | } | |
409 | else | |
410 | return DefWindowProc(hWnd, uMsg, wParam, lParam); | |
411 | } | |
412 | ||
413 | /* _GSocket_Enable_Events: | |
414 | * Enable all event notifications; we need to be notified of all | |
415 | * events for internal processing, but we will only notify users | |
416 | * when an appropiate callback function has been installed. | |
417 | */ | |
c90cc42e | 418 | void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket) |
b7ceceb1 DE |
419 | { |
420 | assert (socket != NULL); | |
421 | ||
422 | if (socket->m_fd != INVALID_SOCKET) | |
423 | { | |
424 | /* We could probably just subscribe to all events regardless | |
425 | * of the socket type, but MS recommends to do it this way. | |
426 | */ | |
427 | long lEvent = socket->m_server? | |
428 | FD_ACCEPT : (FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE); | |
dbfb61b3 | 429 | #ifndef __WXWINCE__ |
1c8681b4 | 430 | gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent); |
dbfb61b3 JS |
431 | #else |
432 | /* | |
433 | * WinCE creates a thread for socket event handling. | |
1c6dd11c | 434 | * All needed parameters get passed through the thread_data structure. |
dbfb61b3 | 435 | */ |
1c6dd11c WS |
436 | socket_running = true; |
437 | thread_data* d = new thread_data; | |
438 | d->lEvent = lEvent; | |
439 | d->hEvtWin = hWin; | |
440 | d->msgnumber = socket->m_msgnumber; | |
441 | d->fd = socket->m_fd; | |
442 | hThread[currSocket++] = CreateThread(NULL, 0, &SocketThread,(LPVOID)d, 0, NULL); | |
dbfb61b3 | 443 | #endif |
b7ceceb1 DE |
444 | } |
445 | } | |
446 | ||
447 | /* _GSocket_Disable_Events: | |
448 | * Disable event notifications (when shutdowning the socket) | |
449 | */ | |
c90cc42e | 450 | void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket) |
b7ceceb1 DE |
451 | { |
452 | assert (socket != NULL); | |
453 | ||
454 | if (socket->m_fd != INVALID_SOCKET) | |
455 | { | |
dbfb61b3 | 456 | #ifndef __WXWINCE__ |
1c8681b4 | 457 | gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0); |
dbfb61b3 | 458 | #else |
1c6dd11c WS |
459 | //Destroy the thread |
460 | //TODO: This needs to be changed, maybe using another global event that could be triggered. | |
461 | socket_running = false; | |
dbfb61b3 | 462 | #endif |
b7ceceb1 DE |
463 | } |
464 | } | |
465 | ||
466 | #else /* !wxUSE_SOCKETS */ | |
467 | ||
468 | /* | |
469 | * Translation unit shouldn't be empty, so include this typedef to make the | |
470 | * compiler (VC++ 6.0, for example) happy | |
471 | */ | |
472 | typedef void (*wxDummy)(); | |
473 | ||
474 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |