]> git.saurik.com Git - wxWidgets.git/blame - src/msw/gsockmsw.cpp
Docstring fixes
[wxWidgets.git] / src / msw / gsockmsw.cpp
CommitLineData
b7ceceb1
DE
1/* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
79630806 3 * Name: gsockmsw.cpp
b7ceceb1
DE
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket GUI-specific MSW code
6 * CVSID: $Id$
7 * -------------------------------------------------------------------------
8 */
9
86afa786
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14 #pragma hdrstop
15#endif
16
b7ceceb1
DE
17/*
18 * TODO: for WinCE we need to replace WSAAsyncSelect
19 * (Windows message-based notification of network events for a socket)
20 * with another mechanism.
21 * We may need to have a separate thread that polls for socket events
22 * using select() and sends a message to the main thread.
23 */
24
b7ceceb1
DE
25/* including rasasync.h (included from windows.h itself included from
26 * wx/setup.h and/or winsock.h results in this warning for
27 * RPCNOTIFICATION_ROUTINE
28 */
29#ifdef _MSC_VER
1c8681b4 30# pragma warning(disable:4115) /* named type definition in parentheses */
b7ceceb1
DE
31#endif
32
33/* This needs to be before the wx/defs/h inclusion
34 * for some reason
35 */
36
37#ifdef __WXWINCE__
1c8681b4
DE
38 /* windows.h results in tons of warnings at max warning level */
39# ifdef _MSC_VER
40# pragma warning(push, 1)
41# endif
42# include <windows.h>
43# ifdef _MSC_VER
44# pragma warning(pop)
45# pragma warning(disable:4514)
46# endif
b7ceceb1
DE
47#endif
48
49#ifndef __GSOCKET_STANDALONE__
50# include "wx/platform.h"
51# include "wx/setup.h"
52#endif
53
54#if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
55
56#ifndef __GSOCKET_STANDALONE__
57
58#include "wx/msw/gsockmsw.h"
59#include "wx/gsocket.h"
60
ebb97a41 61extern "C" WXDLLIMPEXP_BASE HINSTANCE wxGetInstance(void);
b7ceceb1
DE
62#define INSTANCE wxGetInstance()
63
64#else
65
66#include "gsockmsw.h"
67#include "gsocket.h"
68
77ffb593 69/* If not using wxWidgets, a global var called hInst must
b7ceceb1
DE
70 * be available and it must contain the app's instance
71 * handle.
72 */
5f315f78 73extern HINSTANCE hInst;
b7ceceb1
DE
74#define INSTANCE hInst
75
76#endif /* __GSOCKET_STANDALONE__ */
77
78#ifndef __WXWINCE__
79#include <assert.h>
80#else
81#define assert(x)
82#include <winsock.h>
83#include "wx/msw/wince/net.h"
84#endif
85
86#include <string.h>
87#include <stdio.h>
88#include <stdlib.h>
89#include <stddef.h>
90#include <ctype.h>
91
92#include <winsock.h>
93
94#ifdef _MSC_VER
95# pragma warning(default:4115) /* named type definition in parentheses */
96#endif
97
98#define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
99
100/* implemented in utils.cpp */
32524c8f 101extern "C" WXDLLIMPEXP_BASE HWND
b7ceceb1
DE
102wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
103
104/* Maximum number of different GSocket objects at a given time.
105 * This value can be modified at will, but it CANNOT be greater
106 * than (0x7FFF - WM_USER + 1)
107 */
108#define MAXSOCKETS 1024
109
110#if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
111#error "MAXSOCKETS is too big!"
112#endif
113
1c8681b4 114typedef int (PASCAL *WSAAsyncSelectFunc)(SOCKET,HWND,u_int,long);
b7ceceb1 115
c90cc42e
DE
116LRESULT CALLBACK _GSocket_Internal_WinProc(HWND, UINT, WPARAM, LPARAM);
117
b7ceceb1
DE
118/* Global variables */
119
b7ceceb1
DE
120static HWND hWin;
121static CRITICAL_SECTION critical;
122static GSocket* socketList[MAXSOCKETS];
123static int firstAvailable;
1c8681b4
DE
124static WSAAsyncSelectFunc gs_WSAAsyncSelect = NULL;
125static HMODULE gs_wsock32dll = 0;
b7ceceb1 126
c90cc42e
DE
127bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
128{ return true; }
129
b7ceceb1
DE
130/* Global initializers */
131
c90cc42e 132bool GSocketGUIFunctionsTableConcrete::OnInit()
b7ceceb1
DE
133{
134 static LPCTSTR pclassname = NULL;
135 int i;
136
137 /* Create internal window for event notifications */
138 hWin = wxCreateHiddenWindow(&pclassname, CLASSNAME, _GSocket_Internal_WinProc);
139 if (!hWin)
c90cc42e 140 return false;
b7ceceb1
DE
141
142 /* Initialize socket list */
143 InitializeCriticalSection(&critical);
144
145 for (i = 0; i < MAXSOCKETS; i++)
146 {
147 socketList[i] = NULL;
148 }
149 firstAvailable = 0;
150
1c8681b4
DE
151 /* Load WSAAsyncSelect from wsock32.dll (we don't link against it
152 statically to avoid dependency on wsock32.dll for apps that don't use
153 sockets): */
154 gs_wsock32dll = LoadLibraryA("wsock32.dll");
155 if (!gs_wsock32dll)
c90cc42e 156 return false;
1c8681b4
DE
157 gs_WSAAsyncSelect =(WSAAsyncSelectFunc)GetProcAddress(gs_wsock32dll,
158 "WSAAsyncSelect");
159 if (!gs_WSAAsyncSelect)
c90cc42e 160 return false;
1c8681b4 161
c90cc42e 162 return true;
b7ceceb1
DE
163}
164
c90cc42e 165void GSocketGUIFunctionsTableConcrete::OnExit()
b7ceceb1
DE
166{
167 /* Destroy internal window */
168 DestroyWindow(hWin);
169 UnregisterClass(CLASSNAME, INSTANCE);
170
1c8681b4
DE
171 /* Unlock wsock32.dll */
172 if (gs_wsock32dll)
173 {
174 FreeLibrary(gs_wsock32dll);
175 gs_wsock32dll = 0;
176 }
177
b7ceceb1
DE
178 /* Delete critical section */
179 DeleteCriticalSection(&critical);
180}
181
182/* Per-socket GUI initialization / cleanup */
183
c90cc42e 184bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket)
b7ceceb1
DE
185{
186 int i;
187
188 /* Allocate a new message number for this socket */
189 EnterCriticalSection(&critical);
190
191 i = firstAvailable;
192 while (socketList[i] != NULL)
193 {
194 i = (i + 1) % MAXSOCKETS;
195
196 if (i == firstAvailable) /* abort! */
197 {
198 LeaveCriticalSection(&critical);
c90cc42e 199 return false;
b7ceceb1
DE
200 }
201 }
202 socketList[i] = socket;
203 firstAvailable = (i + 1) % MAXSOCKETS;
204 socket->m_msgnumber = (i + WM_USER);
205
206 LeaveCriticalSection(&critical);
207
c90cc42e 208 return true;
b7ceceb1
DE
209}
210
c90cc42e 211void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
b7ceceb1
DE
212{
213 /* Remove the socket from the list */
214 EnterCriticalSection(&critical);
215 socketList[(socket->m_msgnumber - WM_USER)] = NULL;
216 LeaveCriticalSection(&critical);
217}
218
219/* Windows proc for asynchronous event handling */
220
221LRESULT CALLBACK _GSocket_Internal_WinProc(HWND hWnd,
222 UINT uMsg,
223 WPARAM wParam,
224 LPARAM lParam)
225{
226 GSocket *socket;
227 GSocketEvent event;
228 GSocketCallback cback;
229 char *data;
230
231 if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1))
232 {
233 EnterCriticalSection(&critical);
234 socket = socketList[(uMsg - WM_USER)];
235 event = (GSocketEvent) -1;
236 cback = NULL;
237 data = NULL;
238
239 /* Check that the socket still exists (it has not been
240 * destroyed) and for safety, check that the m_fd field
241 * is what we expect it to be.
242 */
243 if ((socket != NULL) && (socket->m_fd == wParam))
244 {
245 switch WSAGETSELECTEVENT(lParam)
246 {
247 case FD_READ: event = GSOCK_INPUT; break;
248 case FD_WRITE: event = GSOCK_OUTPUT; break;
249 case FD_ACCEPT: event = GSOCK_CONNECTION; break;
250 case FD_CONNECT:
251 {
252 if (WSAGETSELECTERROR(lParam) != 0)
253 event = GSOCK_LOST;
254 else
255 event = GSOCK_CONNECTION;
256 break;
257 }
258 case FD_CLOSE: event = GSOCK_LOST; break;
259 }
260
261 if (event != -1)
262 {
263 cback = socket->m_cbacks[event];
264 data = socket->m_data[event];
265
266 if (event == GSOCK_LOST)
267 socket->m_detected = GSOCK_LOST_FLAG;
268 else
269 socket->m_detected |= (1 << event);
270 }
271 }
272
273 /* OK, we can now leave the critical section because we have
274 * already obtained the callback address (we make no further
275 * accesses to socket->whatever). However, the app should
276 * be prepared to handle events from a socket that has just
277 * been closed!
278 */
279 LeaveCriticalSection(&critical);
280
281 if (cback != NULL)
282 (cback)(socket, event, data);
283
284 return (LRESULT) 0;
285 }
286 else
287 return DefWindowProc(hWnd, uMsg, wParam, lParam);
288}
289
290/* _GSocket_Enable_Events:
291 * Enable all event notifications; we need to be notified of all
292 * events for internal processing, but we will only notify users
293 * when an appropiate callback function has been installed.
294 */
c90cc42e 295void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket)
b7ceceb1
DE
296{
297 assert (socket != NULL);
298
299 if (socket->m_fd != INVALID_SOCKET)
300 {
301 /* We could probably just subscribe to all events regardless
302 * of the socket type, but MS recommends to do it this way.
303 */
304 long lEvent = socket->m_server?
305 FD_ACCEPT : (FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
306
1c8681b4 307 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent);
b7ceceb1
DE
308 }
309}
310
311/* _GSocket_Disable_Events:
312 * Disable event notifications (when shutdowning the socket)
313 */
c90cc42e 314void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket)
b7ceceb1
DE
315{
316 assert (socket != NULL);
317
318 if (socket->m_fd != INVALID_SOCKET)
319 {
1c8681b4 320 gs_WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0);
b7ceceb1
DE
321 }
322}
323
324#else /* !wxUSE_SOCKETS */
325
326/*
327 * Translation unit shouldn't be empty, so include this typedef to make the
328 * compiler (VC++ 6.0, for example) happy
329 */
330typedef void (*wxDummy)();
331
332#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */