1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket GUI-specific MSW code
7 * -------------------------------------------------------------------------
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
18 * TODO: for WinCE we need to replace WSAAsyncSelect
19 * (Windows message-based notification of network events for a socket)
20 * with another mechanism.
21 * We may need to have a separate thread that polls for socket events
22 * using select() and sends a message to the main thread.
25 /* including rasasync.h (included from windows.h itself included from
26 * wx/setup.h and/or winsock.h results in this warning for
27 * RPCNOTIFICATION_ROUTINE
30 # pragma warning(disable:4115) /* named type definition in parentheses */
33 /* This needs to be before the wx/defs/h inclusion
38 /* windows.h results in tons of warnings at max warning level */
40 # pragma warning(push, 1)
45 # pragma warning(disable:4514)
49 #ifndef __GSOCKET_STANDALONE__
50 # include "wx/platform.h"
51 # include "wx/setup.h"
54 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
56 #ifndef __GSOCKET_STANDALONE__
58 #include "wx/msw/gsockmsw.h"
59 #include "wx/gsocket.h"
61 extern "C" HINSTANCE
wxGetInstance(void);
62 #define INSTANCE wxGetInstance()
69 /* If not using wxWidgets, a global var called hInst must
70 * be available and it must contain the app's instance
73 #define INSTANCE hInst
75 #endif /* __GSOCKET_STANDALONE__ */
82 #include "wx/msw/wince/net.h"
94 # pragma warning(default:4115) /* named type definition in parentheses */
97 #define CLASSNAME TEXT("_GSocket_Internal_Window_Class")
99 /* implemented in utils.cpp */
100 extern "C" WXDLLIMPEXP_BASE HWND
101 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
103 /* Maximum number of different GSocket objects at a given time.
104 * This value can be modified at will, but it CANNOT be greater
105 * than (0x7FFF - WM_USER + 1)
107 #define MAXSOCKETS 1024
109 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
110 #error "MAXSOCKETS is too big!"
113 typedef int (PASCAL
*WSAAsyncSelectFunc
)(SOCKET
,HWND
,u_int
,long);
115 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND
, UINT
, WPARAM
, LPARAM
);
117 /* Global variables */
119 extern HINSTANCE INSTANCE
;
121 static CRITICAL_SECTION critical
;
122 static GSocket
* socketList
[MAXSOCKETS
];
123 static int firstAvailable
;
124 static WSAAsyncSelectFunc gs_WSAAsyncSelect
= NULL
;
125 static HMODULE gs_wsock32dll
= 0;
127 bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
130 /* Global initializers */
132 bool GSocketGUIFunctionsTableConcrete::OnInit()
134 static LPCTSTR pclassname
= NULL
;
137 /* Create internal window for event notifications */
138 hWin
= wxCreateHiddenWindow(&pclassname
, CLASSNAME
, _GSocket_Internal_WinProc
);
142 /* Initialize socket list */
143 InitializeCriticalSection(&critical
);
145 for (i
= 0; i
< MAXSOCKETS
; i
++)
147 socketList
[i
] = NULL
;
151 /* Load WSAAsyncSelect from wsock32.dll (we don't link against it
152 statically to avoid dependency on wsock32.dll for apps that don't use
154 gs_wsock32dll
= LoadLibraryA("wsock32.dll");
157 gs_WSAAsyncSelect
=(WSAAsyncSelectFunc
)GetProcAddress(gs_wsock32dll
,
159 if (!gs_WSAAsyncSelect
)
165 void GSocketGUIFunctionsTableConcrete::OnExit()
167 /* Destroy internal window */
169 UnregisterClass(CLASSNAME
, INSTANCE
);
171 /* Unlock wsock32.dll */
174 FreeLibrary(gs_wsock32dll
);
178 /* Delete critical section */
179 DeleteCriticalSection(&critical
);
182 /* Per-socket GUI initialization / cleanup */
184 bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket
*socket
)
188 /* Allocate a new message number for this socket */
189 EnterCriticalSection(&critical
);
192 while (socketList
[i
] != NULL
)
194 i
= (i
+ 1) % MAXSOCKETS
;
196 if (i
== firstAvailable
) /* abort! */
198 LeaveCriticalSection(&critical
);
202 socketList
[i
] = socket
;
203 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
204 socket
->m_msgnumber
= (i
+ WM_USER
);
206 LeaveCriticalSection(&critical
);
211 void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket
*socket
)
213 /* Remove the socket from the list */
214 EnterCriticalSection(&critical
);
215 socketList
[(socket
->m_msgnumber
- WM_USER
)] = NULL
;
216 LeaveCriticalSection(&critical
);
219 /* Windows proc for asynchronous event handling */
221 LRESULT CALLBACK
_GSocket_Internal_WinProc(HWND hWnd
,
228 GSocketCallback cback
;
231 if (uMsg
>= WM_USER
&& uMsg
<= (WM_USER
+ MAXSOCKETS
- 1))
233 EnterCriticalSection(&critical
);
234 socket
= socketList
[(uMsg
- WM_USER
)];
235 event
= (GSocketEvent
) -1;
239 /* Check that the socket still exists (it has not been
240 * destroyed) and for safety, check that the m_fd field
241 * is what we expect it to be.
243 if ((socket
!= NULL
) && (socket
->m_fd
== wParam
))
245 switch WSAGETSELECTEVENT(lParam
)
247 case FD_READ
: event
= GSOCK_INPUT
; break;
248 case FD_WRITE
: event
= GSOCK_OUTPUT
; break;
249 case FD_ACCEPT
: event
= GSOCK_CONNECTION
; break;
252 if (WSAGETSELECTERROR(lParam
) != 0)
255 event
= GSOCK_CONNECTION
;
258 case FD_CLOSE
: event
= GSOCK_LOST
; break;
263 cback
= socket
->m_cbacks
[event
];
264 data
= socket
->m_data
[event
];
266 if (event
== GSOCK_LOST
)
267 socket
->m_detected
= GSOCK_LOST_FLAG
;
269 socket
->m_detected
|= (1 << event
);
273 /* OK, we can now leave the critical section because we have
274 * already obtained the callback address (we make no further
275 * accesses to socket->whatever). However, the app should
276 * be prepared to handle events from a socket that has just
279 LeaveCriticalSection(&critical
);
282 (cback
)(socket
, event
, data
);
287 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
290 /* _GSocket_Enable_Events:
291 * Enable all event notifications; we need to be notified of all
292 * events for internal processing, but we will only notify users
293 * when an appropiate callback function has been installed.
295 void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket
*socket
)
297 assert (socket
!= NULL
);
299 if (socket
->m_fd
!= INVALID_SOCKET
)
301 /* We could probably just subscribe to all events regardless
302 * of the socket type, but MS recommends to do it this way.
304 long lEvent
= socket
->m_server
?
305 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
307 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
311 /* _GSocket_Disable_Events:
312 * Disable event notifications (when shutdowning the socket)
314 void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket
*socket
)
316 assert (socket
!= NULL
);
318 if (socket
->m_fd
!= INVALID_SOCKET
)
320 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
324 #else /* !wxUSE_SOCKETS */
327 * Translation unit shouldn't be empty, so include this typedef to make the
328 * compiler (VC++ 6.0, for example) happy
330 typedef void (*wxDummy
)();
332 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */