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