]> git.saurik.com Git - wxWidgets.git/blame - src/msw/gsockmsw.c
Use wxVisualAttributes (partially #if'd out until tested further.)
[wxWidgets.git] / src / msw / gsockmsw.c
CommitLineData
70988afb
GRG
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
882dfc67
JS
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
70988afb
GRG
18/*
19 * PLEASE don't put C++ comments here - this is a C source file.
20 */
21
8a9c2246
VZ
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
8b0107e4 27# pragma warning(disable:4115) /* named type definition in parentheses */
8a9c2246
VZ
28#endif
29
882dfc67
JS
30/* This needs to be before the wx/defs/h inclusion
31 * for some reason
32 */
33
34#ifdef __WXWINCE__
8b0107e4
VZ
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
882dfc67
JS
49#endif
50
70988afb 51#ifndef __GSOCKET_STANDALONE__
a3f7fa62
VZ
52# include "wx/platform.h"
53# include "wx/setup.h"
70988afb
GRG
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
2341cf5f 63HINSTANCE wxGetInstance(void);
70988afb
GRG
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
f4929e86 72 * be available and it must contain the app's instance
70988afb
GRG
73 * handle.
74 */
75#define INSTANCE hInst
76
77#endif /* __GSOCKET_STANDALONE__ */
78
882dfc67 79#ifndef __WXWINCE__
70988afb 80#include <assert.h>
882dfc67
JS
81#else
82#define assert(x)
83#include <winsock.h>
84#include "wx/msw/wince/net.h"
85#endif
86
70988afb
GRG
87#include <string.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <stddef.h>
91#include <ctype.h>
8a9c2246 92
70988afb
GRG
93#include <winsock.h>
94
8a9c2246
VZ
95#ifdef _MSC_VER
96# pragma warning(default:4115) /* named type definition in parentheses */
97#endif
98
9aee09a3 99#define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
eccd1992
VZ
100
101/* implemented in utils.cpp */
487f2d58 102extern WXDLLIMPEXP_BASE HWND
eccd1992 103wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
70988afb
GRG
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
fe16045d 115typedef int (PASCAL *WSAAsyncSelectFunc)(SOCKET,HWND,u_int,long);
70988afb
GRG
116
117/* Global variables */
118
119extern HINSTANCE INSTANCE;
120static HWND hWin;
121static CRITICAL_SECTION critical;
122static GSocket* socketList[MAXSOCKETS];
123static int firstAvailable;
fb2e90c1
VS
124static WSAAsyncSelectFunc gs_WSAAsyncSelect = NULL;
125static HMODULE gs_wsock32dll = 0;
70988afb
GRG
126
127/* Global initializers */
128
38bb138f 129int _GSocket_GUI_Init(void)
70988afb 130{
978f48c6 131 static LPCTSTR pclassname = NULL;
70988afb
GRG
132 int i;
133
134 /* Create internal window for event notifications */
eccd1992
VZ
135 hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, _GSocket_Internal_WinProc);
136 if (!hWin)
137 return FALSE;
70988afb
GRG
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
fb2e90c1
VS
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
eccd1992 159 return TRUE;
70988afb
GRG
160}
161
38bb138f 162void _GSocket_GUI_Cleanup(void)
70988afb
GRG
163{
164 /* Destroy internal window */
165 DestroyWindow(hWin);
166 UnregisterClass(CLASSNAME, INSTANCE);
167
fb2e90c1
VS
168 /* Unlock wsock32.dll */
169 if (gs_wsock32dll)
170 {
171 FreeLibrary(gs_wsock32dll);
172 gs_wsock32dll = 0;
173 }
174
70988afb
GRG
175 /* Delete critical section */
176 DeleteCriticalSection(&critical);
70988afb
GRG
177}
178
179/* Per-socket GUI initialization / cleanup */
180
38bb138f 181int _GSocket_GUI_Init_Socket(GSocket *socket)
70988afb
GRG
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
38bb138f 208void _GSocket_GUI_Destroy_Socket(GSocket *socket)
70988afb
GRG
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
218LRESULT 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)];
ba14d986 232 event = (GSocketEvent) -1;
70988afb
GRG
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 */
292void _GSocket_Enable_Events(GSocket *socket)
293{
294 assert (socket != NULL);
295
296 if (socket->m_fd != INVALID_SOCKET)
297 {
ed2eb9af
GRG
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
fb2e90c1 304 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent);
70988afb
GRG
305 }
306}
307
308/* _GSocket_Disable_Events:
309 * Disable event notifications (when shutdowning the socket)
310 */
311void _GSocket_Disable_Events(GSocket *socket)
312{
313 assert (socket != NULL);
314
315 if (socket->m_fd != INVALID_SOCKET)
316 {
fb2e90c1 317 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0);
70988afb
GRG
318 }
319}
320
321#else /* !wxUSE_SOCKETS */
322
33ac7e6f 323/*
70988afb
GRG
324 * Translation unit shouldn't be empty, so include this typedef to make the
325 * compiler (VC++ 6.0, for example) happy
326 */
3a922bb4 327typedef void (*wxDummy)();
70988afb
GRG
328
329#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */