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
38 #ifndef __GSOCKET_STANDALONE__
39 # include "wx/platform.h"
40 # include "wx/setup.h"
43 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
45 #ifndef __GSOCKET_STANDALONE__
47 #include "wx/msw/gsockmsw.h"
48 #include "wx/gsocket.h"
50 HINSTANCE
wxGetInstance(void);
51 #define INSTANCE wxGetInstance()
58 /* If not using wxWindows, a global var called hInst must
59 * be available and it must contain the app's instance
62 #define INSTANCE hInst
64 #endif /* __GSOCKET_STANDALONE__ */
71 #include "wx/msw/wince/net.h"
83 # pragma warning(default:4115) /* named type definition in parentheses */
86 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
88 /* implemented in utils.cpp */
89 extern WXDLLIMPEXP_BASE HWND
90 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
92 /* Maximum number of different GSocket objects at a given time.
93 * This value can be modified at will, but it CANNOT be greater
94 * than (0x7FFF - WM_USER + 1)
96 #define MAXSOCKETS 1024
98 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
99 #error "MAXSOCKETS is too big!"
102 typedef int (PASCAL
*WSAAsyncSelectFunc
)(SOCKET
,HWND
,u_int
,long);
104 /* Global variables */
106 extern HINSTANCE INSTANCE
;
108 static CRITICAL_SECTION critical
;
109 static GSocket
* socketList
[MAXSOCKETS
];
110 static int firstAvailable
;
111 static WSAAsyncSelectFunc gs_WSAAsyncSelect
= NULL
;
112 static HMODULE gs_wsock32dll
= 0;
114 /* Global initializers */
116 int _GSocket_GUI_Init(void)
118 static LPCTSTR pclassname
= NULL
;
121 /* Create internal window for event notifications */
122 hWin
= wxCreateHiddenWindow(&pclassname
, CLASSNAME
, _GSocket_Internal_WinProc
);
126 /* Initialize socket list */
127 InitializeCriticalSection(&critical
);
129 for (i
= 0; i
< MAXSOCKETS
; i
++)
131 socketList
[i
] = NULL
;
135 /* Load WSAAsyncSelect from wsock32.dll (we don't link against it
136 statically to avoid dependency on wsock32.dll for apps that don't use
138 gs_wsock32dll
= LoadLibraryA("wsock32.dll");
141 gs_WSAAsyncSelect
=(WSAAsyncSelectFunc
)GetProcAddress(gs_wsock32dll
,
143 if (!gs_WSAAsyncSelect
)
149 void _GSocket_GUI_Cleanup(void)
151 /* Destroy internal window */
153 UnregisterClass(CLASSNAME
, INSTANCE
);
155 /* Unlock wsock32.dll */
158 FreeLibrary(gs_wsock32dll
);
162 /* Delete critical section */
163 DeleteCriticalSection(&critical
);
166 /* Per-socket GUI initialization / cleanup */
168 int _GSocket_GUI_Init_Socket(GSocket
*socket
)
172 /* Allocate a new message number for this socket */
173 EnterCriticalSection(&critical
);
176 while (socketList
[i
] != NULL
)
178 i
= (i
+ 1) % MAXSOCKETS
;
180 if (i
== firstAvailable
) /* abort! */
182 LeaveCriticalSection(&critical
);
186 socketList
[i
] = socket
;
187 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
188 socket
->m_msgnumber
= (i
+ WM_USER
);
190 LeaveCriticalSection(&critical
);
195 void _GSocket_GUI_Destroy_Socket(GSocket
*socket
)
197 /* Remove the socket from the list */
198 EnterCriticalSection(&critical
);
199 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
200 LeaveCriticalSection(&critical
);
203 /* Windows proc for asynchronous event handling */
205 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
212 GSocketCallback cback
;
215 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
217 EnterCriticalSection(&critical
);
218 socket
= socketList
[(uMsg
- WM_USER
)];
219 event
= (GSocketEvent
) -1;
223 /* Check that the socket still exists (it has not been
224 * destroyed) and for safety, check that the m_fd field
225 * is what we expect it to be.
227 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
229 switch WSAGETSELECTEVENT(lParam
)
231 case FD_READ
: event
= GSOCK_INPUT
; break;
232 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
233 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
236 if (WSAGETSELECTERROR(lParam
) != 0)
239 event
= GSOCK_CONNECTION
;
242 case FD_CLOSE
: event
= GSOCK_LOST
; break;
247 cback
= socket
->m_cbacks
[event
];
248 data
= socket
->m_data
[event
];
250 if (event
== GSOCK_LOST
)
251 socket
->m_detected
= GSOCK_LOST_FLAG
;
253 socket
->m_detected
|= (1 << event
);
257 /* OK, we can now leave the critical section because we have
258 * already obtained the callback address (we make no further
259 * accesses to socket->whatever). However, the app should
260 * be prepared to handle events from a socket that has just
263 LeaveCriticalSection(&critical
);
266 (cback
)(socket
, event
, data
);
271 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
274 /* _GSocket_Enable_Events:
275 * Enable all event notifications; we need to be notified of all
276 * events for internal processing, but we will only notify users
277 * when an appropiate callback function has been installed.
279 void _GSocket_Enable_Events(GSocket
*socket
)
281 assert (socket
!= NULL
);
283 if (socket
->m_fd
!= INVALID_SOCKET
)
285 /* We could probably just subscribe to all events regardless
286 * of the socket type, but MS recommends to do it this way.
288 long lEvent
= socket
->m_server
?
289 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
291 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
295 /* _GSocket_Disable_Events:
296 * Disable event notifications (when shutdowning the socket)
298 void _GSocket_Disable_Events(GSocket
*socket
)
300 assert (socket
!= NULL
);
302 if (socket
->m_fd
!= INVALID_SOCKET
)
304 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
308 #else /* !wxUSE_SOCKETS */
311 * Translation unit shouldn't be empty, so include this typedef to make the
312 * compiler (VC++ 6.0, for example) happy
314 typedef void (*wxDummy
)();
316 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */