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