]>
Commit | Line | Data |
---|---|---|
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__ |
33ac7e6f | 39 | #include "wx/defs.h" |
70988afb GRG |
40 | #include "wx/setup.h" |
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 | 50 | HINSTANCE 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 VZ |
86 | #define CLASSNAME TEXT("_GSocket_Internal_Window_Class") |
87 | #define WINDOWNAME TEXT("_GSocket_Internal_Window_Name") | |
70988afb GRG |
88 | |
89 | /* Maximum number of different GSocket objects at a given time. | |
90 | * This value can be modified at will, but it CANNOT be greater | |
91 | * than (0x7FFF - WM_USER + 1) | |
92 | */ | |
93 | #define MAXSOCKETS 1024 | |
94 | ||
95 | #if (MAXSOCKETS > (0x7FFF - WM_USER + 1)) | |
96 | #error "MAXSOCKETS is too big!" | |
97 | #endif | |
98 | ||
99 | ||
100 | /* Global variables */ | |
101 | ||
102 | extern HINSTANCE INSTANCE; | |
103 | static HWND hWin; | |
104 | static CRITICAL_SECTION critical; | |
105 | static GSocket* socketList[MAXSOCKETS]; | |
106 | static int firstAvailable; | |
107 | ||
108 | /* Global initializers */ | |
109 | ||
38bb138f | 110 | int _GSocket_GUI_Init(void) |
70988afb | 111 | { |
70988afb GRG |
112 | WNDCLASS winClass; |
113 | int i; | |
114 | ||
115 | /* Create internal window for event notifications */ | |
116 | winClass.style = 0; | |
117 | winClass.lpfnWndProc = _GSocket_Internal_WinProc; | |
118 | winClass.cbClsExtra = 0; | |
119 | winClass.cbWndExtra = 0; | |
120 | winClass.hInstance = INSTANCE; | |
121 | winClass.hIcon = (HICON) NULL; | |
122 | winClass.hCursor = (HCURSOR) NULL; | |
123 | winClass.hbrBackground = (HBRUSH) NULL; | |
124 | winClass.lpszMenuName = (LPCTSTR) NULL; | |
125 | winClass.lpszClassName = CLASSNAME; | |
126 | ||
127 | RegisterClass(&winClass); | |
128 | hWin = CreateWindow(CLASSNAME, | |
129 | WINDOWNAME, | |
130 | 0, 0, 0, 0, 0, | |
131 | (HWND) NULL, (HMENU) NULL, INSTANCE, (LPVOID) NULL); | |
132 | ||
133 | if (!hWin) return FALSE; | |
134 | ||
135 | /* Initialize socket list */ | |
136 | InitializeCriticalSection(&critical); | |
137 | ||
138 | for (i = 0; i < MAXSOCKETS; i++) | |
139 | { | |
140 | socketList[i] = NULL; | |
141 | } | |
142 | firstAvailable = 0; | |
143 | ||
38bb138f | 144 | return 1; |
70988afb GRG |
145 | } |
146 | ||
38bb138f | 147 | void _GSocket_GUI_Cleanup(void) |
70988afb GRG |
148 | { |
149 | /* Destroy internal window */ | |
150 | DestroyWindow(hWin); | |
151 | UnregisterClass(CLASSNAME, INSTANCE); | |
152 | ||
153 | /* Delete critical section */ | |
154 | DeleteCriticalSection(&critical); | |
70988afb GRG |
155 | } |
156 | ||
157 | /* Per-socket GUI initialization / cleanup */ | |
158 | ||
38bb138f | 159 | int _GSocket_GUI_Init_Socket(GSocket *socket) |
70988afb GRG |
160 | { |
161 | int i; | |
162 | ||
163 | /* Allocate a new message number for this socket */ | |
164 | EnterCriticalSection(&critical); | |
165 | ||
166 | i = firstAvailable; | |
167 | while (socketList[i] != NULL) | |
168 | { | |
169 | i = (i + 1) % MAXSOCKETS; | |
170 | ||
171 | if (i == firstAvailable) /* abort! */ | |
172 | { | |
173 | LeaveCriticalSection(&critical); | |
174 | return FALSE; | |
175 | } | |
176 | } | |
177 | socketList[i] = socket; | |
178 | firstAvailable = (i + 1) % MAXSOCKETS; | |
179 | socket->m_msgnumber = (i + WM_USER); | |
180 | ||
181 | LeaveCriticalSection(&critical); | |
182 | ||
183 | return TRUE; | |
184 | } | |
185 | ||
38bb138f | 186 | void _GSocket_GUI_Destroy_Socket(GSocket *socket) |
70988afb GRG |
187 | { |
188 | /* Remove the socket from the list */ | |
189 | EnterCriticalSection(&critical); | |
190 | socketList[(socket->m_msgnumber - WM_USER)] = NULL; | |
191 | LeaveCriticalSection(&critical); | |
192 | } | |
193 | ||
194 | /* Windows proc for asynchronous event handling */ | |
195 | ||
196 | LRESULT CALLBACK _GSocket_Internal_WinProc(HWND hWnd, | |
197 | UINT uMsg, | |
198 | WPARAM wParam, | |
199 | LPARAM lParam) | |
200 | { | |
201 | GSocket *socket; | |
202 | GSocketEvent event; | |
203 | GSocketCallback cback; | |
204 | char *data; | |
205 | ||
206 | if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1)) | |
207 | { | |
208 | EnterCriticalSection(&critical); | |
209 | socket = socketList[(uMsg - WM_USER)]; | |
ba14d986 | 210 | event = (GSocketEvent) -1; |
70988afb GRG |
211 | cback = NULL; |
212 | data = NULL; | |
213 | ||
214 | /* Check that the socket still exists (it has not been | |
215 | * destroyed) and for safety, check that the m_fd field | |
216 | * is what we expect it to be. | |
217 | */ | |
218 | if ((socket != NULL) && (socket->m_fd == wParam)) | |
219 | { | |
220 | switch WSAGETSELECTEVENT(lParam) | |
221 | { | |
222 | case FD_READ: event = GSOCK_INPUT; break; | |
223 | case FD_WRITE: event = GSOCK_OUTPUT; break; | |
224 | case FD_ACCEPT: event = GSOCK_CONNECTION; break; | |
225 | case FD_CONNECT: | |
226 | { | |
227 | if (WSAGETSELECTERROR(lParam) != 0) | |
228 | event = GSOCK_LOST; | |
229 | else | |
230 | event = GSOCK_CONNECTION; | |
231 | break; | |
232 | } | |
233 | case FD_CLOSE: event = GSOCK_LOST; break; | |
234 | } | |
235 | ||
236 | if (event != -1) | |
237 | { | |
238 | cback = socket->m_cbacks[event]; | |
239 | data = socket->m_data[event]; | |
240 | ||
241 | if (event == GSOCK_LOST) | |
242 | socket->m_detected = GSOCK_LOST_FLAG; | |
243 | else | |
244 | socket->m_detected |= (1 << event); | |
245 | } | |
246 | } | |
247 | ||
248 | /* OK, we can now leave the critical section because we have | |
249 | * already obtained the callback address (we make no further | |
250 | * accesses to socket->whatever). However, the app should | |
251 | * be prepared to handle events from a socket that has just | |
252 | * been closed! | |
253 | */ | |
254 | LeaveCriticalSection(&critical); | |
255 | ||
256 | if (cback != NULL) | |
257 | (cback)(socket, event, data); | |
258 | ||
259 | return (LRESULT) 0; | |
260 | } | |
261 | else | |
262 | return DefWindowProc(hWnd, uMsg, wParam, lParam); | |
263 | } | |
264 | ||
265 | /* _GSocket_Enable_Events: | |
266 | * Enable all event notifications; we need to be notified of all | |
267 | * events for internal processing, but we will only notify users | |
268 | * when an appropiate callback function has been installed. | |
269 | */ | |
270 | void _GSocket_Enable_Events(GSocket *socket) | |
271 | { | |
272 | assert (socket != NULL); | |
273 | ||
274 | if (socket->m_fd != INVALID_SOCKET) | |
275 | { | |
ed2eb9af GRG |
276 | /* We could probably just subscribe to all events regardless |
277 | * of the socket type, but MS recommends to do it this way. | |
278 | */ | |
279 | long lEvent = socket->m_server? | |
280 | FD_ACCEPT : (FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE); | |
281 | ||
282 | WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, lEvent); | |
70988afb GRG |
283 | } |
284 | } | |
285 | ||
286 | /* _GSocket_Disable_Events: | |
287 | * Disable event notifications (when shutdowning the socket) | |
288 | */ | |
289 | void _GSocket_Disable_Events(GSocket *socket) | |
290 | { | |
291 | assert (socket != NULL); | |
292 | ||
293 | if (socket->m_fd != INVALID_SOCKET) | |
294 | { | |
295 | WSAAsyncSelect(socket->m_fd, hWin, socket->m_msgnumber, 0); | |
296 | } | |
297 | } | |
298 | ||
299 | #else /* !wxUSE_SOCKETS */ | |
300 | ||
33ac7e6f | 301 | /* |
70988afb GRG |
302 | * Translation unit shouldn't be empty, so include this typedef to make the |
303 | * compiler (VC++ 6.0, for example) happy | |
304 | */ | |
3a922bb4 | 305 | typedef void (*wxDummy)(); |
70988afb GRG |
306 | |
307 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |