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