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