#include "wx/private/socket.h"
#include "wx/apptrait.h"
+#include "wx/thread.h"
extern "C" WXDLLIMPEXP_BASE HINSTANCE wxGetInstance();
#define INSTANCE wxGetInstance()
/* Global variables */
static HWND hWin;
-static CRITICAL_SECTION critical;
+wxCRIT_SECT_DECLARE_MEMBER(gs_critical);
static wxSocketImplMSW *socketList[MAXSOCKETS];
static int firstAvailable;
virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
};
-/* Global initializers */
-
bool wxSocketMSWManager::OnInit()
{
static LPCTSTR pclassname = NULL;
return false;
/* Initialize socket list */
- InitializeCriticalSection(&critical);
-
for (i = 0; i < MAXSOCKETS; i++)
{
socketList[i] = NULL;
gs_wsock32dll = 0;
}
- /* Delete critical section */
- DeleteCriticalSection(&critical);
-
WSACleanup();
}
: wxSocketImpl(wxsocket)
{
/* Allocate a new message number for this socket */
- EnterCriticalSection(&critical);
+ wxCRIT_SECT_LOCKER(lock, gs_critical);
int i = firstAvailable;
while (socketList[i] != NULL)
if (i == firstAvailable) /* abort! */
{
- LeaveCriticalSection(&critical);
m_msgnumber = 0; // invalid
return;
}
socketList[i] = this;
firstAvailable = (i + 1) % MAXSOCKETS;
m_msgnumber = (i + WM_USER);
-
- LeaveCriticalSection(&critical);
}
wxSocketImplMSW::~wxSocketImplMSW()
{
/* Remove the socket from the list */
- EnterCriticalSection(&critical);
+ wxCRIT_SECT_LOCKER(lock, gs_critical);
if ( m_msgnumber )
{
socketList[m_msgnumber - WM_USER] = NULL;
}
//else: the socket has never been created successfully
-
- LeaveCriticalSection(&critical);
}
/* Windows proc for asynchronous event handling */
WPARAM wParam,
LPARAM lParam)
{
- wxSocketImplMSW *socket;
- wxSocketNotify event;
+ if ( uMsg < WM_USER || uMsg > (WM_USER + MAXSOCKETS - 1))
+ return DefWindowProc(hWnd, uMsg, wParam, lParam);
- if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1))
- {
- EnterCriticalSection(&critical);
- socket = socketList[(uMsg - WM_USER)];
- event = (wxSocketNotify) -1;
-
- /* Check that the socket still exists (it has not been
- * destroyed) and for safety, check that the m_fd field
- * is what we expect it to be.
- */
- if ((socket != NULL) && ((WPARAM)socket->m_fd == wParam))
+ wxSocketImplMSW *socket;
+ wxSocketNotify event;
{
- switch WSAGETSELECTEVENT(lParam)
- {
- case FD_READ: event = wxSOCKET_INPUT; break;
- case FD_WRITE: event = wxSOCKET_OUTPUT; break;
- case FD_ACCEPT: event = wxSOCKET_CONNECTION; break;
- case FD_CONNECT:
+ wxCRIT_SECT_LOCKER(lock, gs_critical);
+
+ socket = socketList[(uMsg - WM_USER)];
+ event = (wxSocketNotify) -1;
+
+ /* Check that the socket still exists (it has not been
+ * destroyed) and for safety, check that the m_fd field
+ * is what we expect it to be.
+ */
+ if ((socket != NULL) && ((WPARAM)socket->m_fd == wParam))
{
- if (WSAGETSELECTERROR(lParam) != 0)
- event = wxSOCKET_LOST;
- else
- event = wxSOCKET_CONNECTION;
- break;
- }
- case FD_CLOSE: event = wxSOCKET_LOST; break;
- }
-
- if (event != -1)
- {
- if (event == wxSOCKET_LOST)
- socket->m_detected = wxSOCKET_LOST_FLAG;
- else
- socket->m_detected |= (1 << event);
- }
- }
+ switch WSAGETSELECTEVENT(lParam)
+ {
+ case FD_READ: event = wxSOCKET_INPUT; break;
+ case FD_WRITE: event = wxSOCKET_OUTPUT; break;
+ case FD_ACCEPT: event = wxSOCKET_CONNECTION; break;
+ case FD_CONNECT:
+ {
+ if (WSAGETSELECTERROR(lParam) != 0)
+ event = wxSOCKET_LOST;
+ else
+ event = wxSOCKET_CONNECTION;
+ break;
+ }
+ case FD_CLOSE: event = wxSOCKET_LOST; break;
+ }
- LeaveCriticalSection(&critical);
+ if (event != -1)
+ {
+ if (event == wxSOCKET_LOST)
+ socket->m_detected = wxSOCKET_LOST_FLAG;
+ else
+ socket->m_detected |= (1 << event);
+ }
+ }
+ } // unlock gs_critical
if ( socket )
socket->NotifyOnStateChange(event);
return (LRESULT) 0;
- }
- else
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
/*