1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket GUI-specific MSW code
7 * -------------------------------------------------------------------------
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.
19 * PLEASE don't put C++ comments here - this is a C source file.
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
27 # pragma warning(disable:4115) /* named type definition in parentheses */
30 /* This needs to be before the wx/defs/h inclusion
35 /* windows.h results in tons of warnings at max warning level */
37 # pragma warning(push, 1)
42 # pragma warning(disable:4514)
46 #ifndef __GSOCKET_STANDALONE__
47 # include "wx/platform.h"
48 # include "wx/setup.h"
51 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
53 #ifndef __GSOCKET_STANDALONE__
55 #include "wx/msw/gsockmsw.h"
56 #include "wx/gsocket.h"
58 HINSTANCE
wxGetInstance(void);
59 #define INSTANCE wxGetInstance()
66 /* If not using wxWidgets, a global var called hInst must
67 * be available and it must contain the app's instance
70 #define INSTANCE hInst
72 #endif /* __GSOCKET_STANDALONE__ */
79 #include "wx/msw/wince/net.h"
91 # pragma warning(default:4115) /* named type definition in parentheses */
94 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
96 /* implemented in utils.cpp */
97 extern WXDLLIMPEXP_BASE HWND
98 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
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)
104 #define MAXSOCKETS 1024
106 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
107 #error "MAXSOCKETS is too big!"
110 typedef int (PASCAL
*WSAAsyncSelectFunc
)(SOCKET
,HWND
,u_int
,long);
112 /* Global variables */
114 extern HINSTANCE INSTANCE
;
116 static CRITICAL_SECTION critical
;
117 static GSocket
* socketList
[MAXSOCKETS
];
118 static int firstAvailable
;
119 static WSAAsyncSelectFunc gs_WSAAsyncSelect
= NULL
;
120 static HMODULE gs_wsock32dll
= 0;
122 /* Global initializers */
124 int _GSocket_GUI_Init(void)
126 static LPCTSTR pclassname
= NULL
;
129 /* Create internal window for event notifications */
130 hWin
= wxCreateHiddenWindow(&pclassname
, CLASSNAME
, _GSocket_Internal_WinProc
);
134 /* Initialize socket list */
135 InitializeCriticalSection(&critical
);
137 for (i
= 0; i
< MAXSOCKETS
; i
++)
139 socketList
[i
] = NULL
;
143 /* Load WSAAsyncSelect from wsock32.dll (we don't link against it
144 statically to avoid dependency on wsock32.dll for apps that don't use
146 gs_wsock32dll
= LoadLibraryA("wsock32.dll");
149 gs_WSAAsyncSelect
=(WSAAsyncSelectFunc
)GetProcAddress(gs_wsock32dll
,
151 if (!gs_WSAAsyncSelect
)
157 void _GSocket_GUI_Cleanup(void)
159 /* Destroy internal window */
161 UnregisterClass(CLASSNAME
, INSTANCE
);
163 /* Unlock wsock32.dll */
166 FreeLibrary(gs_wsock32dll
);
170 /* Delete critical section */
171 DeleteCriticalSection(&critical
);
174 /* Per-socket GUI initialization / cleanup */
176 int _GSocket_GUI_Init_Socket(GSocket
*socket
)
180 /* Allocate a new message number for this socket */
181 EnterCriticalSection(&critical
);
184 while (socketList
[i
] != NULL
)
186 i
= (i
+ 1) % MAXSOCKETS
;
188 if (i
== firstAvailable
) /* abort! */
190 LeaveCriticalSection(&critical
);
194 socketList
[i
] = socket
;
195 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
196 socket
->m_msgnumber
= (i
+ WM_USER
);
198 LeaveCriticalSection(&critical
);
203 void _GSocket_GUI_Destroy_Socket(GSocket
*socket
)
205 /* Remove the socket from the list */
206 EnterCriticalSection(&critical
);
207 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
208 LeaveCriticalSection(&critical
);
211 /* Windows proc for asynchronous event handling */
213 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
220 GSocketCallback cback
;
223 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
225 EnterCriticalSection(&critical
);
226 socket
= socketList
[(uMsg
- WM_USER
)];
227 event
= (GSocketEvent
) -1;
231 /* Check that the socket still exists (it has not been
232 * destroyed) and for safety, check that the m_fd field
233 * is what we expect it to be.
235 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
237 switch WSAGETSELECTEVENT(lParam
)
239 case FD_READ
: event
= GSOCK_INPUT
; break;
240 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
241 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
244 if (WSAGETSELECTERROR(lParam
) != 0)
247 event
= GSOCK_CONNECTION
;
250 case FD_CLOSE
: event
= GSOCK_LOST
; break;
255 cback
= socket
->m_cbacks
[event
];
256 data
= socket
->m_data
[event
];
258 if (event
== GSOCK_LOST
)
259 socket
->m_detected
= GSOCK_LOST_FLAG
;
261 socket
->m_detected
|= (1 << event
);
265 /* OK, we can now leave the critical section because we have
266 * already obtained the callback address (we make no further
267 * accesses to socket->whatever). However, the app should
268 * be prepared to handle events from a socket that has just
271 LeaveCriticalSection(&critical
);
274 (cback
)(socket
, event
, data
);
279 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
282 /* _GSocket_Enable_Events:
283 * Enable all event notifications; we need to be notified of all
284 * events for internal processing, but we will only notify users
285 * when an appropiate callback function has been installed.
287 void _GSocket_Enable_Events(GSocket
*socket
)
289 assert (socket
!= NULL
);
291 if (socket
->m_fd
!= INVALID_SOCKET
)
293 /* We could probably just subscribe to all events regardless
294 * of the socket type, but MS recommends to do it this way.
296 long lEvent
= socket
->m_server
?
297 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
299 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
303 /* _GSocket_Disable_Events:
304 * Disable event notifications (when shutdowning the socket)
306 void _GSocket_Disable_Events(GSocket
*socket
)
308 assert (socket
!= NULL
);
310 if (socket
->m_fd
!= INVALID_SOCKET
)
312 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
316 #else /* !wxUSE_SOCKETS */
319 * Translation unit shouldn't be empty, so include this typedef to make the
320 * compiler (VC++ 6.0, for example) happy
322 typedef void (*wxDummy
)();
324 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */