1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: src/msw/gsockmsw.cpp
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
7 * Purpose: GSocket GUI-specific MSW code
9 * -------------------------------------------------------------------------
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
20 * DONE: for WinCE we need to replace WSAAsyncSelect
21 * (Windows message-based notification of network events for a socket)
22 * with another mechanism.
23 * As WSAAsyncSelect is not present on WinCE, it now uses
24 * WSACreateEvent, WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents.
25 * When enabling eventhandling for a socket a new thread it created that keeps track of the events
26 * and posts a messageto the hidden window to use the standard message loop.
29 /* including rasasync.h (included from windows.h itself included from
30 * wx/setup.h and/or winsock.h results in this warning for
31 * RPCNOTIFICATION_ROUTINE
34 # pragma warning(disable:4115) /* named type definition in parentheses */
37 /* This needs to be before the wx/defs/h inclusion
42 /* windows.h results in tons of warnings at max warning level */
44 # pragma warning(push, 1)
49 # pragma warning(disable:4514)
53 #ifndef __GSOCKET_STANDALONE__
54 # include "wx/platform.h"
57 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
59 #ifndef __GSOCKET_STANDALONE__
61 #include "wx/msw/gsockmsw.h"
62 #include "wx/gsocket.h"
64 extern "C" WXDLLIMPEXP_BASE HINSTANCE
wxGetInstance(void);
65 #define INSTANCE wxGetInstance()
67 #else /* __GSOCKET_STANDALONE__ */
72 /* If not using wxWidgets, a global var called hInst must
73 * be available and it must contain the app's instance
76 extern HINSTANCE hInst
;
77 #define INSTANCE hInst
79 #endif /* !__GSOCKET_STANDALONE__/__GSOCKET_STANDALONE__ */
86 #include "wx/msw/wince/net.h"
87 #include "wx/hashmap.h"
88 WX_DECLARE_HASH_MAP(int,bool,wxIntegerHash
,wxIntegerEqual
,SocketHash
);
100 # pragma warning(default:4115) /* named type definition in parentheses */
103 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
105 /* implemented in utils.cpp */
106 extern "C" WXDLLIMPEXP_BASE HWND
107 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
109 /* Maximum number of different GSocket objects at a given time.
110 * This value can be modified at will, but it CANNOT be greater
111 * than (0x7FFF - WM_USER + 1)
113 #define MAXSOCKETS 1024
115 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
116 #error "MAXSOCKETS is too big!"
120 typedef int (PASCAL
*WSAAsyncSelectFunc
)(SOCKET
,HWND
,u_int
,long);
122 /* Typedef the needed function prototypes and the WSANETWORKEVENTS structure
124 typedef struct _WSANETWORKEVENTS
{
127 } WSANETWORKEVENTS
, FAR
* LPWSANETWORKEVENTS
;
128 typedef HANDLE (PASCAL
*WSACreateEventFunc
)(void);
129 typedef int (PASCAL
*WSAEventSelectFunc
)(SOCKET
,HANDLE
,long);
130 typedef int (PASCAL
*WSAWaitForMultipleEventsFunc
)(long,HANDLE
,BOOL
,long,BOOL
);
131 typedef int (PASCAL
*WSAEnumNetworkEventsFunc
)(SOCKET
,HANDLE
,LPWSANETWORKEVENTS
);
134 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND
, UINT
, WPARAM
, LPARAM
);
136 /* Global variables */
139 static CRITICAL_SECTION critical
;
140 static GSocket
* socketList
[MAXSOCKETS
];
141 static int firstAvailable
;
144 static WSAAsyncSelectFunc gs_WSAAsyncSelect
= NULL
;
146 static SocketHash socketHash
;
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.
155 typedef struct thread_data
{
157 unsigned long msgnumber
;
159 unsigned long lEvent
;
163 static HMODULE gs_wsock32dll
= 0;
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.
171 DWORD WINAPI
SocketThread(LPVOID data
)
173 WSANETWORKEVENTS NetworkEvents
;
174 thread_data
* d
= (thread_data
*)data
;
176 HANDLE NetworkEvent
= gs_WSACreateEvent();
177 gs_WSAEventSelect(d
->fd
, NetworkEvent
, d
->lEvent
);
179 while(socketHash
[d
->fd
] == true)
181 if ((gs_WSAWaitForMultipleEvents(1, &NetworkEvent
, FALSE
,INFINITE
, FALSE
)) == WAIT_FAILED
)
183 printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
186 if (gs_WSAEnumNetworkEvents(d
->fd
,NetworkEvent
, &NetworkEvents
) == SOCKET_ERROR
)
188 printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
192 long flags
= NetworkEvents
.lNetworkEvents
;
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
);
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
);
207 gs_WSAEventSelect(d
->fd
, NetworkEvent
, 0);
214 bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
219 /* Global initializers */
221 bool GSocketGUIFunctionsTableConcrete::OnInit()
223 static LPCTSTR pclassname
= NULL
;
226 /* Create internal window for event notifications */
227 hWin
= wxCreateHiddenWindow(&pclassname
, CLASSNAME
, _GSocket_Internal_WinProc
);
231 /* Initialize socket list */
232 InitializeCriticalSection(&critical
);
234 for (i
= 0; i
< MAXSOCKETS
; i
++)
236 socketList
[i
] = NULL
;
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
244 gs_wsock32dll
= LoadLibrary(wxT("wsock32.dll"));
247 gs_WSAAsyncSelect
=(WSAAsyncSelectFunc
)GetProcAddress(gs_wsock32dll
,
249 if (!gs_WSAAsyncSelect
)
252 /* On WinCE we load ws2.dll which will provide the needed functions.
254 gs_wsock32dll
= LoadLibrary(wxT("ws2.dll"));
257 gs_WSAEventSelect
=(WSAEventSelectFunc
)GetProcAddress(gs_wsock32dll
,
258 wxT("WSAEventSelect"));
259 if (!gs_WSAEventSelect
)
262 gs_WSACreateEvent
=(WSACreateEventFunc
)GetProcAddress(gs_wsock32dll
,
263 wxT("WSACreateEvent"));
264 if (!gs_WSACreateEvent
)
267 gs_WSAWaitForMultipleEvents
=(WSAWaitForMultipleEventsFunc
)GetProcAddress(gs_wsock32dll
,
268 wxT("WSAWaitForMultipleEvents"));
269 if (!gs_WSAWaitForMultipleEvents
)
272 gs_WSAEnumNetworkEvents
=(WSAEnumNetworkEventsFunc
)GetProcAddress(gs_wsock32dll
,
273 wxT("WSAEnumNetworkEvents"));
274 if (!gs_WSAEnumNetworkEvents
)
283 void GSocketGUIFunctionsTableConcrete::OnExit()
286 /* Delete the threads here */
287 for(unsigned int i
=0; i
< currSocket
; i
++)
288 CloseHandle(hThread
[i
]);
290 /* Destroy internal window */
292 UnregisterClass(CLASSNAME
, INSTANCE
);
294 /* Unlock wsock32.dll */
297 FreeLibrary(gs_wsock32dll
);
301 /* Delete critical section */
302 DeleteCriticalSection(&critical
);
305 /* Per-socket GUI initialization / cleanup */
307 bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket
*socket
)
311 /* Allocate a new message number for this socket */
312 EnterCriticalSection(&critical
);
315 while (socketList
[i
] != NULL
)
317 i
= (i
+ 1) % MAXSOCKETS
;
319 if (i
== firstAvailable
) /* abort! */
321 LeaveCriticalSection(&critical
);
325 socketList
[i
] = socket
;
326 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
327 socket
->m_msgnumber
= (i
+ WM_USER
);
329 LeaveCriticalSection(&critical
);
334 void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket
*socket
)
336 /* Remove the socket from the list */
337 EnterCriticalSection(&critical
);
338 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
339 LeaveCriticalSection(&critical
);
342 /* Windows proc for asynchronous event handling */
344 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
351 GSocketCallback cback
;
354 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
356 EnterCriticalSection(&critical
);
357 socket
= socketList
[(uMsg
- WM_USER
)];
358 event
= (GSocketEvent
) -1;
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.
366 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
368 switch WSAGETSELECTEVENT(lParam
)
370 case FD_READ
: event
= GSOCK_INPUT
; break;
371 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
372 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
375 if (WSAGETSELECTERROR(lParam
) != 0)
378 event
= GSOCK_CONNECTION
;
381 case FD_CLOSE
: event
= GSOCK_LOST
; break;
386 cback
= socket
->m_cbacks
[event
];
387 data
= socket
->m_data
[event
];
389 if (event
== GSOCK_LOST
)
390 socket
->m_detected
= GSOCK_LOST_FLAG
;
392 socket
->m_detected
|= (1 << event
);
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
402 LeaveCriticalSection(&critical
);
405 (cback
)(socket
, event
, data
);
410 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
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.
418 void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket
*socket
)
420 assert (socket
!= NULL
);
422 if (socket
->m_fd
!= INVALID_SOCKET
)
424 /* We could probably just subscribe to all events regardless
425 * of the socket type, but MS recommends to do it this way.
427 long lEvent
= socket
->m_server
?
428 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
430 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
433 * WinCE creates a thread for socket event handling.
434 * All needed parameters get passed through the thread_data structure.
437 thread_data
* d
= new thread_data
;
440 d
->msgnumber
= socket
->m_msgnumber
;
441 d
->fd
= socket
->m_fd
;
442 socketHash
[socket
->m_fd
] = true;
443 hThread
[currSocket
++] = CreateThread(NULL
, 0, &SocketThread
,(LPVOID
)d
, 0, NULL
);
448 /* _GSocket_Disable_Events:
449 * Disable event notifications (when shutdowning the socket)
451 void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket
*socket
)
453 assert (socket
!= NULL
);
455 if (socket
->m_fd
!= INVALID_SOCKET
)
458 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
461 socketHash
[socket
->m_fd
] = false;
466 #else /* !wxUSE_SOCKETS */
469 * Translation unit shouldn't be empty, so include this typedef to make the
470 * compiler (VC++ 6.0, for example) happy
472 typedef void (*wxDummy
)();
474 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */