1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/sockmsw.cpp
3 // Purpose: MSW-specific socket code
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
6 // Copyright: (C) 1999-1997, Guilhem Lavaux
7 // (C) 1999-2000, Guillermo Rodriguez Garcia
8 // (C) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
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 #include "wx/private/socket.h"
31 #include "wx/msw/private.h" // for wxGetInstance()
32 #include "wx/private/fd.h"
33 #include "wx/apptrait.h"
34 #include "wx/thread.h"
35 #include "wx/dynlib.h"
40 * As WSAAsyncSelect is not present on WinCE, it now uses WSACreateEvent,
41 * WSAEventSelect, WSAWaitForMultipleEvents and WSAEnumNetworkEvents. When
42 * enabling eventhandling for a socket a new thread it created that keeps track
43 * of the events and posts a messageto the hidden window to use the standard
46 #include "wx/msw/wince/net.h"
47 #include "wx/hashmap.h"
48 WX_DECLARE_HASH_MAP(int,bool,wxIntegerHash
,wxIntegerEqual
,SocketHash
);
51 #define isdigit(x) (x > 47 && x < 58)
53 #include "wx/msw/wince/net.h"
58 # pragma warning(default:4115) /* named type definition in parentheses */
61 #include "wx/msw/private/hiddenwin.h"
63 #define CLASSNAME TEXT("_wxSocket_Internal_Window_Class")
65 /* Maximum number of different wxSocket objects at a given time.
66 * This value can be modified at will, but it CANNOT be greater
67 * than (0x7FFF - WM_USER + 1)
69 #define MAXSOCKETS 1024
71 #if (MAXSOCKETS > (0x7FFF - WM_USER + 1))
72 #error "MAXSOCKETS is too big!"
76 typedef int (PASCAL
*WSAAsyncSelect_t
)(SOCKET
,HWND
,u_int
,long);
78 /* Typedef the needed function prototypes and the WSANETWORKEVENTS structure
80 typedef struct _WSANETWORKEVENTS
{
83 } WSANETWORKEVENTS
, FAR
* LPWSANETWORKEVENTS
;
84 typedef HANDLE (PASCAL
*WSACreateEvent_t
)();
85 typedef int (PASCAL
*WSAEventSelect_t
)(SOCKET
,HANDLE
,long);
86 typedef int (PASCAL
*WSAWaitForMultipleEvents_t
)(long,HANDLE
,BOOL
,long,BOOL
);
87 typedef int (PASCAL
*WSAEnumNetworkEvents_t
)(SOCKET
,HANDLE
,LPWSANETWORKEVENTS
);
90 LRESULT CALLBACK
wxSocket_Internal_WinProc(HWND
, UINT
, WPARAM
, LPARAM
);
92 /* Global variables */
95 wxCRIT_SECT_DECLARE_MEMBER(gs_critical
);
96 static wxSocketImplMSW
*socketList
[MAXSOCKETS
];
97 static int firstAvailable
;
100 static WSAAsyncSelect_t gs_WSAAsyncSelect
= NULL
;
102 static SocketHash socketHash
;
103 static unsigned int currSocket
;
104 HANDLE hThread
[MAXSOCKETS
];
105 static WSACreateEvent_t gs_WSACreateEvent
= NULL
;
106 static WSAEventSelect_t gs_WSAEventSelect
= NULL
;
107 static WSAWaitForMultipleEvents_t gs_WSAWaitForMultipleEvents
= NULL
;
108 static WSAEnumNetworkEvents_t gs_WSAEnumNetworkEvents
= NULL
;
109 /* This structure will be used to pass data on to the thread that handles socket events.
111 typedef struct thread_data
{
113 unsigned long msgnumber
;
115 unsigned long lEvent
;
120 /* This thread handles socket events on WinCE using WSAEventSelect() as
121 * WSAAsyncSelect is not supported. When an event occurs for the socket, it is
122 * checked what kind of event happened and the correct message gets posted so
123 * that the hidden window can handle it as it would in other MSW builds.
125 DWORD WINAPI
SocketThread(LPVOID data
)
127 WSANETWORKEVENTS NetworkEvents
;
128 thread_data
* d
= (thread_data
*)data
;
130 HANDLE NetworkEvent
= gs_WSACreateEvent();
131 gs_WSAEventSelect(d
->fd
, NetworkEvent
, d
->lEvent
);
133 while(socketHash
[d
->fd
] == true)
135 if ((gs_WSAWaitForMultipleEvents(1, &NetworkEvent
, FALSE
,INFINITE
, FALSE
)) == WAIT_FAILED
)
137 printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
140 if (gs_WSAEnumNetworkEvents(d
->fd
,NetworkEvent
, &NetworkEvents
) == SOCKET_ERROR
)
142 printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
146 long flags
= NetworkEvents
.lNetworkEvents
;
148 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_READ
);
149 if (flags
& FD_WRITE
)
150 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_WRITE
);
152 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_OOB
);
153 if (flags
& FD_ACCEPT
)
154 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_ACCEPT
);
155 if (flags
& FD_CONNECT
)
156 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_CONNECT
);
157 if (flags
& FD_CLOSE
)
158 ::PostMessage(d
->hEvtWin
, d
->msgnumber
,d
->fd
, FD_CLOSE
);
161 gs_WSAEventSelect(d
->fd
, NetworkEvent
, 0);
167 // ----------------------------------------------------------------------------
168 // MSW implementation of wxSocketManager
169 // ----------------------------------------------------------------------------
171 class wxSocketMSWManager
: public wxSocketManager
174 virtual bool OnInit();
175 virtual void OnExit();
177 virtual wxSocketImpl
*CreateSocket(wxSocketBase
& wxsocket
)
179 return new wxSocketImplMSW(wxsocket
);
181 virtual void Install_Callback(wxSocketImpl
*socket
,
182 wxSocketNotify event
= wxSOCKET_LOST
);
183 virtual void Uninstall_Callback(wxSocketImpl
*socket
,
184 wxSocketNotify event
= wxSOCKET_LOST
);
187 static wxDynamicLibrary gs_wsock32dll
;
190 wxDynamicLibrary
wxSocketMSWManager::gs_wsock32dll
;
192 bool wxSocketMSWManager::OnInit()
194 static LPCTSTR pclassname
= NULL
;
197 /* Create internal window for event notifications */
198 hWin
= wxCreateHiddenWindow(&pclassname
, CLASSNAME
, wxSocket_Internal_WinProc
);
202 /* Initialize socket list */
203 for (i
= 0; i
< MAXSOCKETS
; i
++)
205 socketList
[i
] = NULL
;
209 // we don't link with wsock32.dll (or ws2 in CE case) statically to avoid
210 // dependencies on it for all the application using wx even if they don't use
213 #define WINSOCK_DLL_NAME wxT("ws2.dll")
215 #define WINSOCK_DLL_NAME wxT("wsock32.dll")
218 gs_wsock32dll
.Load(WINSOCK_DLL_NAME
, wxDL_VERBATIM
| wxDL_QUIET
);
219 if ( !gs_wsock32dll
.IsLoaded() )
223 wxDL_INIT_FUNC(gs_
, WSAAsyncSelect
, gs_wsock32dll
);
224 if ( !gs_WSAAsyncSelect
)
227 wxDL_INIT_FUNC(gs_
, WSAEventSelect
, gs_wsock32dll
);
228 if ( !gs_WSAEventSelect
)
231 wxDL_INIT_FUNC(gs_
, WSACreateEvent
, gs_wsock32dll
);
232 if ( !gs_WSACreateEvent
)
235 wxDL_INIT_FUNC(gs_
, WSAWaitForMultipleEvents
, gs_wsock32dll
);
236 if ( !gs_WSAWaitForMultipleEvents
)
239 wxDL_INIT_FUNC(gs_
, WSAEnumNetworkEvents
, gs_wsock32dll
);
240 if ( !gs_WSAEnumNetworkEvents
)
244 #endif // !__WXWINCE__/__WXWINCE__
246 // finally initialize WinSock
248 return WSAStartup((1 << 8) | 1, &wsaData
) == 0;
251 void wxSocketMSWManager::OnExit()
254 /* Delete the threads here */
255 for(unsigned int i
=0; i
< currSocket
; i
++)
256 CloseHandle(hThread
[i
]);
258 /* Destroy internal window */
260 UnregisterClass(CLASSNAME
, wxGetInstance());
264 gs_wsock32dll
.Unload();
267 /* Per-socket GUI initialization / cleanup */
269 wxSocketImplMSW::wxSocketImplMSW(wxSocketBase
& wxsocket
)
270 : wxSocketImpl(wxsocket
)
272 /* Allocate a new message number for this socket */
273 wxCRIT_SECT_LOCKER(lock
, gs_critical
);
275 int i
= firstAvailable
;
276 while (socketList
[i
] != NULL
)
278 i
= (i
+ 1) % MAXSOCKETS
;
280 if (i
== firstAvailable
) /* abort! */
282 m_msgnumber
= 0; // invalid
286 socketList
[i
] = this;
287 firstAvailable
= (i
+ 1) % MAXSOCKETS
;
288 m_msgnumber
= (i
+ WM_USER
);
291 wxSocketImplMSW::~wxSocketImplMSW()
293 /* Remove the socket from the list */
294 wxCRIT_SECT_LOCKER(lock
, gs_critical
);
298 // we need to remove any pending messages for this socket to avoid having
299 // them sent to a new socket which could reuse the same message number as
300 // soon as we destroy this one
302 while ( ::PeekMessage(&msg
, hWin
, m_msgnumber
, m_msgnumber
, PM_REMOVE
) )
305 socketList
[m_msgnumber
- WM_USER
] = NULL
;
307 //else: the socket has never been created successfully
310 /* Windows proc for asynchronous event handling */
312 LRESULT CALLBACK
wxSocket_Internal_WinProc(HWND hWnd
,
317 if ( uMsg
< WM_USER
|| uMsg
> (WM_USER
+ MAXSOCKETS
- 1))
318 return DefWindowProc(hWnd
, uMsg
, wParam
, lParam
);
320 wxSocketImplMSW
*socket
;
321 wxSocketNotify event
= (wxSocketNotify
)-1;
323 wxCRIT_SECT_LOCKER(lock
, gs_critical
);
325 socket
= socketList
[(uMsg
- WM_USER
)];
329 // the socket may be already closed but we could still receive
330 // notifications for it sent (asynchronously) before it got closed
331 if ( socket
->m_fd
== INVALID_SOCKET
)
334 wxASSERT_MSG( socket
->m_fd
== (SOCKET
)wParam
,
335 "mismatch between message and socket?" );
337 switch ( WSAGETSELECTEVENT(lParam
) )
340 // We may get a FD_READ notification even when there is no data
341 // to read on the socket, in particular this happens on socket
342 // creation when we seem to always get FD_CONNECT, FD_WRITE and
343 // FD_READ notifications all at once (but it doesn't happen
344 // only then). Ignore such dummy notifications.
347 timeval tv
= { 0, 0 };
350 wxFD_SET(socket
->m_fd
, &fds
);
352 if ( select(socket
->m_fd
+ 1, &fds
, NULL
, NULL
, &tv
) != 1 )
356 event
= wxSOCKET_INPUT
;
360 event
= wxSOCKET_OUTPUT
;
364 event
= wxSOCKET_CONNECTION
;
368 event
= WSAGETSELECTERROR(lParam
) ? wxSOCKET_LOST
369 : wxSOCKET_CONNECTION
;
373 event
= wxSOCKET_LOST
;
377 wxFAIL_MSG( "unexpected socket notification" );
380 } // unlock gs_critical
382 socket
->NotifyOnStateChange(event
);
388 * Enable all event notifications; we need to be notified of all
389 * events for internal processing, but we will only notify users
390 * when an appropriate callback function has been installed.
392 void wxSocketMSWManager::Install_Callback(wxSocketImpl
*socket_
,
393 wxSocketNotify
WXUNUSED(event
))
395 wxSocketImplMSW
* const socket
= static_cast<wxSocketImplMSW
*>(socket_
);
397 if (socket
->m_fd
!= INVALID_SOCKET
)
399 /* We could probably just subscribe to all events regardless
400 * of the socket type, but MS recommends to do it this way.
402 long lEvent
= socket
->m_server
?
403 FD_ACCEPT
: (FD_READ
| FD_WRITE
| FD_CONNECT
| FD_CLOSE
);
405 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, lEvent
);
408 * WinCE creates a thread for socket event handling.
409 * All needed parameters get passed through the thread_data structure.
412 thread_data
* d
= new thread_data
;
415 d
->msgnumber
= socket
->m_msgnumber
;
416 d
->fd
= socket
->m_fd
;
417 socketHash
[socket
->m_fd
] = true;
418 hThread
[currSocket
++] = CreateThread(NULL
, 0, &SocketThread
,(LPVOID
)d
, 0, NULL
);
424 * Disable event notifications (used when shutting down the socket)
426 void wxSocketMSWManager::Uninstall_Callback(wxSocketImpl
*socket_
,
427 wxSocketNotify
WXUNUSED(event
))
429 wxSocketImplMSW
* const socket
= static_cast<wxSocketImplMSW
*>(socket_
);
431 if (socket
->m_fd
!= INVALID_SOCKET
)
434 gs_WSAAsyncSelect(socket
->m_fd
, hWin
, socket
->m_msgnumber
, 0);
437 socketHash
[socket
->m_fd
] = false;
442 // set the wxBase variable to point to our wxSocketManager implementation
444 // see comments in wx/apptrait.h for the explanation of why do we do it
446 static struct ManagerSetter
450 static wxSocketMSWManager s_manager
;
451 wxAppTraits::SetDefaultSocketManager(&s_manager
);
455 // see the relative linker macro in socket.cpp
456 wxFORCE_LINK_THIS_MODULE( mswsocket
);
458 // ============================================================================
459 // wxSocketImpl implementation
460 // ============================================================================
462 void wxSocketImplMSW::DoClose()
464 wxSocketManager::Get()->Uninstall_Callback(this);
469 wxSocketError
wxSocketImplMSW::GetLastError() const
471 switch ( WSAGetLastError() )
474 return wxSOCKET_NOERROR
;
477 return wxSOCKET_INVSOCK
;
480 return wxSOCKET_WOULDBLOCK
;
483 return wxSOCKET_IOERR
;
487 #endif // wxUSE_SOCKETS