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