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