]> git.saurik.com Git - wxWidgets.git/blob - src/msw/gsockmsw.c
0d9c591cb683850940341e3b85d9b8c3c9520fd1
[wxWidgets.git] / src / msw / gsockmsw.c
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
10 /*
11 * PLEASE don't put C++ comments here - this is a C source file.
12 */
13
14 /* including rasasync.h (included from windows.h itself included from
15 * wx/setup.h and/or winsock.h results in this warning for
16 * RPCNOTIFICATION_ROUTINE
17 */
18 #ifdef _MSC_VER
19 # pragma warning(disable:4115) /* named type definition in parentheses */
20 #endif
21
22 #ifndef __GSOCKET_STANDALONE__
23 #include "wx/defs.h"
24 #include "wx/setup.h"
25 #endif
26
27 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
28
29 #ifndef __GSOCKET_STANDALONE__
30
31 #include "wx/msw/gsockmsw.h"
32 #include "wx/gsocket.h"
33
34 #define INSTANCE wxGetInstance()
35
36 #else
37
38 #include "gsockmsw.h"
39 #include "gsocket.h"
40
41 /* If not using wxWindows, a global var called hInst must
42 * be available and it must contain the app's instance
43 * handle.
44 */
45 #define INSTANCE hInst
46
47 #endif /* __GSOCKET_STANDALONE__ */
48
49 #include <assert.h>
50 #include <string.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <stddef.h>
54 #include <ctype.h>
55
56 #include <winsock.h>
57
58 /* don't use C++ TRUE/FALSE definition which we get from wx/defs.h */
59 #undef TRUE
60 #undef FALSE
61 #define TRUE 1
62 #define FALSE 0
63
64 #ifdef _MSC_VER
65 # pragma warning(default:4115) /* named type definition in parentheses */
66 #endif
67
68 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
69 #define WINDOWNAME TEXT("_GSocket_Internal_Window_Name")
70
71 /* Maximum number of different GSocket objects at a given time.
72 * This value can be modified at will, but it CANNOT be greater
73 * than (0x7FFF - WM_USER + 1)
74 */
75 #define MAXSOCKETS 1024
76
77 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
78 #error "MAXSOCKETS is too big!"
79 #endif
80
81
82 /* Global variables */
83
84 extern HINSTANCE INSTANCE;
85 static HWND hWin;
86 static CRITICAL_SECTION critical;
87 static GSocket* socketList[MAXSOCKETS];
88 static int firstAvailable;
89
90 /* Global initializers */
91
92 int GSocket_Init(void)
93 {
94 WSADATA wsaData;
95 WNDCLASS winClass;
96 int i;
97
98 /* Create internal window for event notifications */
99 winClass.style = 0;
100 winClass.lpfnWndProc = _GSocket_Internal_WinProc;
101 winClass.cbClsExtra = 0;
102 winClass.cbWndExtra = 0;
103 winClass.hInstance = INSTANCE;
104 winClass.hIcon = (HICON) NULL;
105 winClass.hCursor = (HCURSOR) NULL;
106 winClass.hbrBackground = (HBRUSH) NULL;
107 winClass.lpszMenuName = (LPCTSTR) NULL;
108 winClass.lpszClassName = CLASSNAME;
109
110 RegisterClass(&winClass);
111 hWin = CreateWindow(CLASSNAME,
112 WINDOWNAME,
113 0, 0, 0, 0, 0,
114 (HWND) NULL, (HMENU) NULL, INSTANCE, (LPVOID) NULL);
115
116 if (!hWin) return FALSE;
117
118 /* Initialize socket list */
119 InitializeCriticalSection(&critical);
120
121 for (i = 0; i < MAXSOCKETS; i++)
122 {
123 socketList[i] = NULL;
124 }
125 firstAvailable = 0;
126
127 /* Initialize WinSocket */
128 return (WSAStartup((1 << 8) | 1, &wsaData) == 0);
129 }
130
131 void GSocket_Cleanup(void)
132 {
133 /* Destroy internal window */
134 DestroyWindow(hWin);
135 UnregisterClass(CLASSNAME, INSTANCE);
136
137 /* Delete critical section */
138 DeleteCriticalSection(&critical);
139
140 /* Cleanup WinSocket */
141 WSACleanup();
142 }
143
144 /* Per-socket GUI initialization / cleanup */
145
146 int _GSocket_GUI_Init(GSocket *socket)
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
173 void _GSocket_GUI_Destroy(GSocket *socket)
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
183 LRESULT 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)];
197 event = -1;
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 */
257 void _GSocket_Enable_Events(GSocket *socket)
258 {
259 assert (socket != NULL);
260
261 if (socket->m_fd != INVALID_SOCKET)
262 {
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);
270 }
271 }
272
273 /* _GSocket_Disable_Events:
274 * Disable event notifications (when shutdowning the socket)
275 */
276 void _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
288 /*
289 * Translation unit shouldn't be empty, so include this typedef to make the
290 * compiler (VC++ 6.0, for example) happy
291 */
292 typedef void (*wxDummy)();
293
294 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */