]> git.saurik.com Git - wxWidgets.git/blame - src/msw/gsockmsw.c
mention ShouldInheritColours() change
[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
GRG
38#ifndef __GSOCKET_STANDALONE__
39#include "wx/setup.h"
40#endif
41
42#if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
43
44#ifndef __GSOCKET_STANDALONE__
45
46#include "wx/msw/gsockmsw.h"
47#include "wx/gsocket.h"
48
2341cf5f 49HINSTANCE wxGetInstance(void);
70988afb
GRG
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 85#define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
eccd1992
VZ
86
87/* implemented in utils.cpp */
487f2d58 88extern WXDLLIMPEXP_BASE HWND
eccd1992 89wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
70988afb
GRG
90
91/* Maximum number of different GSocket objects at a given time.
92 * This value can be modified at will, but it CANNOT be greater
93 * than (0x7FFF - WM_USER + 1)
94 */
95#define MAXSOCKETS 1024
96
97#if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
98#error "MAXSOCKETS is too big!"
99#endif
100
101
102/* Global variables */
103
104extern HINSTANCE INSTANCE;
105static HWND hWin;
106static CRITICAL_SECTION critical;
107static GSocket* socketList[MAXSOCKETS];
108static int firstAvailable;
109
110/* Global initializers */
111
38bb138f 112int _GSocket_GUI_Init(void)
70988afb 113{
978f48c6 114 static LPCTSTR pclassname = NULL;
70988afb
GRG
115 int i;
116
117 /* Create internal window for event notifications */
eccd1992
VZ
118 hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, _GSocket_Internal_WinProc);
119 if (!hWin)
120 return FALSE;
70988afb
GRG
121
122 /* Initialize socket list */
123 InitializeCriticalSection(&critical);
124
125 for (i = 0; i < MAXSOCKETS; i++)
126 {
127 socketList[i] = NULL;
128 }
129 firstAvailable = 0;
130
eccd1992 131 return TRUE;
70988afb
GRG
132}
133
38bb138f 134void _GSocket_GUI_Cleanup(void)
70988afb
GRG
135{
136 /* Destroy internal window */
137 DestroyWindow(hWin);
138 UnregisterClass(CLASSNAME, INSTANCE);
139
140 /* Delete critical section */
141 DeleteCriticalSection(&critical);
70988afb
GRG
142}
143
144/* Per-socket GUI initialization / cleanup */
145
38bb138f 146int _GSocket_GUI_Init_Socket(GSocket *socket)
70988afb
GRG
147{
148 int i;
149
150 /* Allocate a new message number for this socket */
151 EnterCriticalSection(&critical);
152
153 i = firstAvailable;
154 while (socketList[i] != NULL)
155 {
156 i = (i + 1) % MAXSOCKETS;
157
158 if (i == firstAvailable) /* abort! */
159 {
160 LeaveCriticalSection(&critical);
161 return FALSE;
162 }
163 }
164 socketList[i] = socket;
165 firstAvailable = (i + 1) % MAXSOCKETS;
166 socket->m_msgnumber = (i + WM_USER);
167
168 LeaveCriticalSection(&critical);
169
170 return TRUE;
171}
172
38bb138f 173void _GSocket_GUI_Destroy_Socket(GSocket *socket)
70988afb
GRG
174{
175 /* Remove the socket from the list */
176 EnterCriticalSection(&critical);
177 socketList[(socket->m_msgnumber - WM_USER)] = NULL;
178 LeaveCriticalSection(&critical);
179}
180
181/* Windows proc for asynchronous event handling */
182
183LRESULT CALLBACK _GSocket_Internal_WinProc(HWND hWnd,
184 UINT uMsg,
185 WPARAM wParam,
186 LPARAM lParam)
187{
188 GSocket *socket;
189 GSocketEvent event;
190 GSocketCallback cback;
191 char *data;
192
193 if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1))
194 {
195 EnterCriticalSection(&critical);
196 socket = socketList[(uMsg - WM_USER)];
ba14d986 197 event = (GSocketEvent) -1;
70988afb
GRG
198 cback = NULL;
199 data = NULL;
200
201 /* Check that the socket still exists (it has not been
202 * destroyed) and for safety, check that the m_fd field
203 * is what we expect it to be.
204 */
205 if ((socket != NULL) && (socket->m_fd == wParam))
206 {
207 switch WSAGETSELECTEVENT(lParam)
208 {
209 case FD_READ: event = GSOCK_INPUT; break;
210 case FD_WRITE: event = GSOCK_OUTPUT; break;
211 case FD_ACCEPT: event = GSOCK_CONNECTION; break;
212 case FD_CONNECT:
213 {
214 if (WSAGETSELECTERROR(lParam) != 0)
215 event = GSOCK_LOST;
216 else
217 event = GSOCK_CONNECTION;
218 break;
219 }
220 case FD_CLOSE: event = GSOCK_LOST; break;
221 }
222
223 if (event != -1)
224 {
225 cback = socket->m_cbacks[event];
226 data = socket->m_data[event];
227
228 if (event == GSOCK_LOST)
229 socket->m_detected = GSOCK_LOST_FLAG;
230 else
231 socket->m_detected |= (1 << event);
232 }
233 }
234
235 /* OK, we can now leave the critical section because we have
236 * already obtained the callback address (we make no further
237 * accesses to socket->whatever). However, the app should
238 * be prepared to handle events from a socket that has just
239 * been closed!
240 */
241 LeaveCriticalSection(&critical);
242
243 if (cback != NULL)
244 (cback)(socket, event, data);
245
246 return (LRESULT) 0;
247 }
248 else
249 return DefWindowProc(hWnd, uMsg, wParam, lParam);
250}
251
252/* _GSocket_Enable_Events:
253 * Enable all event notifications; we need to be notified of all
254 * events for internal processing, but we will only notify users
255 * when an appropiate callback function has been installed.
256 */
257void _GSocket_Enable_Events(GSocket *socket)
258{
259 assert (socket != NULL);
260
261 if (socket->m_fd != INVALID_SOCKET)
262 {
ed2eb9af
GRG
263 /* We could probably just subscribe to all events regardless
264 * of the socket type, but MS recommends to do it this way.
265 */
266 long lEvent = socket->m_server?
267 FD_ACCEPT : (FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
268
269 WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent);
70988afb
GRG
270 }
271}
272
273/* _GSocket_Disable_Events:
274 * Disable event notifications (when shutdowning the socket)
275 */
276void _GSocket_Disable_Events(GSocket *socket)
277{
278 assert (socket != NULL);
279
280 if (socket->m_fd != INVALID_SOCKET)
281 {
282 WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0);
283 }
284}
285
286#else /* !wxUSE_SOCKETS */
287
33ac7e6f 288/*
70988afb
GRG
289 * Translation unit shouldn't be empty, so include this typedef to make the
290 * compiler (VC++ 6.0, for example) happy
291 */
3a922bb4 292typedef void (*wxDummy)();
70988afb
GRG
293
294#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */