]> git.saurik.com Git - wxWidgets.git/blob - src/msw/gsockmsw.c
use HasFlag() and InXXX() instead of explicit flag testing (patch 899507)
[wxWidgets.git] / src / msw / gsockmsw.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: gsockmsw.c
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket GUI-specific MSW code
6 * CVSID: $Id$
7 * -------------------------------------------------------------------------
8 */
9
10 /*
11 * TODO: for WinCE we need to replace WSAAsyncSelect
12 * (Windows message-based notification of network events for a socket)
13 * with another mechanism.
14 * We may need to have a separate thread that polls for socket events
15 * using select() and sends a message to the main thread.
16 */
17
18 /*
19 * PLEASE don't put C++ comments here - this is a C source file.
20 */
21
22 /* including rasasync.h (included from windows.h itself included from
23 * wx/setup.h and/or winsock.h results in this warning for
24 * RPCNOTIFICATION_ROUTINE
25 */
26 #ifdef _MSC_VER
27 # pragma warning(disable:4115) /* named type definition in parentheses */
28 #endif
29
30 /* This needs to be before the wx/defs/h inclusion
31 * for some reason
32 */
33
34 #ifdef __WXWINCE__
35 #include <windows.h>
36 #endif
37
38 #ifndef __GSOCKET_STANDALONE__
39 # include "wx/platform.h"
40 # include "wx/setup.h"
41 #endif
42
43 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
44
45 #ifndef __GSOCKET_STANDALONE__
46
47 #include "wx/msw/gsockmsw.h"
48 #include "wx/gsocket.h"
49
50 HINSTANCE wxGetInstance(void);
51 #define INSTANCE wxGetInstance()
52
53 #else
54
55 #include "gsockmsw.h"
56 #include "gsocket.h"
57
58 /* If not using wxWindows, a global var called hInst must
59 * be available and it must contain the app's instance
60 * handle.
61 */
62 #define INSTANCE hInst
63
64 #endif /* __GSOCKET_STANDALONE__ */
65
66 #ifndef __WXWINCE__
67 #include <assert.h>
68 #else
69 #define assert(x)
70 #include <winsock.h>
71 #include "wx/msw/wince/net.h"
72 #endif
73
74 #include <string.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <stddef.h>
78 #include <ctype.h>
79
80 #include <winsock.h>
81
82 #ifdef _MSC_VER
83 # pragma warning(default:4115) /* named type definition in parentheses */
84 #endif
85
86 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
87
88 /* implemented in utils.cpp */
89 extern WXDLLIMPEXP_BASE HWND
90 wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
91
92 /* Maximum number of different GSocket objects at a given time.
93 * This value can be modified at will, but it CANNOT be greater
94 * than (0x7FFF - WM_USER + 1)
95 */
96 #define MAXSOCKETS 1024
97
98 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
99 #error "MAXSOCKETS is too big!"
100 #endif
101
102 typedef int (PASCAL *WSAAsyncSelectFunc)(SOCKET,HWND,u_int,long);
103
104 /* Global variables */
105
106 extern HINSTANCE INSTANCE;
107 static HWND hWin;
108 static CRITICAL_SECTION critical;
109 static GSocket* socketList[MAXSOCKETS];
110 static int firstAvailable;
111 static WSAAsyncSelectFunc gs_WSAAsyncSelect = NULL;
112 static HMODULE gs_wsock32dll = 0;
113
114 /* Global initializers */
115
116 int _GSocket_GUI_Init(void)
117 {
118 static LPCTSTR pclassname = NULL;
119 int i;
120
121 /* Create internal window for event notifications */
122 hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, _GSocket_Internal_WinProc);
123 if (!hWin)
124 return FALSE;
125
126 /* Initialize socket list */
127 InitializeCriticalSection(&critical);
128
129 for (i = 0; i < MAXSOCKETS; i++)
130 {
131 socketList[i] = NULL;
132 }
133 firstAvailable = 0;
134
135 /* Load WSAAsyncSelect from wsock32.dll (we don't link against it
136 statically to avoid dependency on wsock32.dll for apps that don't use
137 sockets): */
138 gs_wsock32dll = LoadLibraryA("wsock32.dll");
139 if (!gs_wsock32dll)
140 return FALSE;
141 gs_WSAAsyncSelect =(WSAAsyncSelectFunc)GetProcAddress(gs_wsock32dll,
142 "WSAAsyncSelect");
143 if (!gs_WSAAsyncSelect)
144 return FALSE;
145
146 return TRUE;
147 }
148
149 void _GSocket_GUI_Cleanup(void)
150 {
151 /* Destroy internal window */
152 DestroyWindow(hWin);
153 UnregisterClass(CLASSNAME, INSTANCE);
154
155 /* Unlock wsock32.dll */
156 if (gs_wsock32dll)
157 {
158 FreeLibrary(gs_wsock32dll);
159 gs_wsock32dll = 0;
160 }
161
162 /* Delete critical section */
163 DeleteCriticalSection(&critical);
164 }
165
166 /* Per-socket GUI initialization / cleanup */
167
168 int _GSocket_GUI_Init_Socket(GSocket *socket)
169 {
170 int i;
171
172 /* Allocate a new message number for this socket */
173 EnterCriticalSection(&critical);
174
175 i = firstAvailable;
176 while (socketList[i] != NULL)
177 {
178 i = (i + 1) % MAXSOCKETS;
179
180 if (i == firstAvailable) /* abort! */
181 {
182 LeaveCriticalSection(&critical);
183 return FALSE;
184 }
185 }
186 socketList[i] = socket;
187 firstAvailable = (i + 1) % MAXSOCKETS;
188 socket->m_msgnumber = (i + WM_USER);
189
190 LeaveCriticalSection(&critical);
191
192 return TRUE;
193 }
194
195 void _GSocket_GUI_Destroy_Socket(GSocket *socket)
196 {
197 /* Remove the socket from the list */
198 EnterCriticalSection(&critical);
199 socketList[(socket->m_msgnumber - WM_USER)] = NULL;
200 LeaveCriticalSection(&critical);
201 }
202
203 /* Windows proc for asynchronous event handling */
204
205 LRESULT CALLBACK _GSocket_Internal_WinProc(HWND hWnd,
206 UINT uMsg,
207 WPARAM wParam,
208 LPARAM lParam)
209 {
210 GSocket *socket;
211 GSocketEvent event;
212 GSocketCallback cback;
213 char *data;
214
215 if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1))
216 {
217 EnterCriticalSection(&critical);
218 socket = socketList[(uMsg - WM_USER)];
219 event = (GSocketEvent) -1;
220 cback = NULL;
221 data = NULL;
222
223 /* Check that the socket still exists (it has not been
224 * destroyed) and for safety, check that the m_fd field
225 * is what we expect it to be.
226 */
227 if ((socket != NULL) && (socket->m_fd == wParam))
228 {
229 switch WSAGETSELECTEVENT(lParam)
230 {
231 case FD_READ: event = GSOCK_INPUT; break;
232 case FD_WRITE: event = GSOCK_OUTPUT; break;
233 case FD_ACCEPT: event = GSOCK_CONNECTION; break;
234 case FD_CONNECT:
235 {
236 if (WSAGETSELECTERROR(lParam) != 0)
237 event = GSOCK_LOST;
238 else
239 event = GSOCK_CONNECTION;
240 break;
241 }
242 case FD_CLOSE: event = GSOCK_LOST; break;
243 }
244
245 if (event != -1)
246 {
247 cback = socket->m_cbacks[event];
248 data = socket->m_data[event];
249
250 if (event == GSOCK_LOST)
251 socket->m_detected = GSOCK_LOST_FLAG;
252 else
253 socket->m_detected |= (1 << event);
254 }
255 }
256
257 /* OK, we can now leave the critical section because we have
258 * already obtained the callback address (we make no further
259 * accesses to socket->whatever). However, the app should
260 * be prepared to handle events from a socket that has just
261 * been closed!
262 */
263 LeaveCriticalSection(&critical);
264
265 if (cback != NULL)
266 (cback)(socket, event, data);
267
268 return (LRESULT) 0;
269 }
270 else
271 return DefWindowProc(hWnd, uMsg, wParam, lParam);
272 }
273
274 /* _GSocket_Enable_Events:
275 * Enable all event notifications; we need to be notified of all
276 * events for internal processing, but we will only notify users
277 * when an appropiate callback function has been installed.
278 */
279 void _GSocket_Enable_Events(GSocket *socket)
280 {
281 assert (socket != NULL);
282
283 if (socket->m_fd != INVALID_SOCKET)
284 {
285 /* We could probably just subscribe to all events regardless
286 * of the socket type, but MS recommends to do it this way.
287 */
288 long lEvent = socket->m_server?
289 FD_ACCEPT : (FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
290
291 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent);
292 }
293 }
294
295 /* _GSocket_Disable_Events:
296 * Disable event notifications (when shutdowning the socket)
297 */
298 void _GSocket_Disable_Events(GSocket *socket)
299 {
300 assert (socket != NULL);
301
302 if (socket->m_fd != INVALID_SOCKET)
303 {
304 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0);
305 }
306 }
307
308 #else /* !wxUSE_SOCKETS */
309
310 /*
311 * Translation unit shouldn't be empty, so include this typedef to make the
312 * compiler (VC++ 6.0, for example) happy
313 */
314 typedef void (*wxDummy)();
315
316 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */