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