]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/sockmsw.cpp
Fix typo in last commit
[wxWidgets.git] / src / msw / sockmsw.cpp
index 33ce4ab540fc43388e06e9b74b4a09dabcba4419..d531813ca68a8c940e147c1485246a67b9b416b3 100644 (file)
@@ -7,7 +7,7 @@
 //             (C) 1999-2000, Guillermo Rodriguez Garcia
 //             (C) 2008 Vadim Zeitlin
 // RCS_ID:     $Id$
 //             (C) 1999-2000, Guillermo Rodriguez Garcia
 //             (C) 2008 Vadim Zeitlin
 // RCS_ID:     $Id$
-// License:    wxWindows licence
+// Licence:    wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 
 /////////////////////////////////////////////////////////////////////////////
 
 
 
 #include "wx/private/socket.h"
 #include "wx/msw/private.h"     // for wxGetInstance()
 
 #include "wx/private/socket.h"
 #include "wx/msw/private.h"     // for wxGetInstance()
+#include "wx/private/fd.h"
 #include "wx/apptrait.h"
 #include "wx/thread.h"
 #include "wx/dynlib.h"
 #include "wx/apptrait.h"
 #include "wx/thread.h"
 #include "wx/dynlib.h"
+#include "wx/link.h"
 
 #ifdef __WXWINCE__
 /*
 
 #ifdef __WXWINCE__
 /*
@@ -179,8 +181,10 @@ public:
     {
         return new wxSocketImplMSW(wxsocket);
     }
     {
         return new wxSocketImplMSW(wxsocket);
     }
-    virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event);
-    virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
+    virtual void Install_Callback(wxSocketImpl *socket,
+                                  wxSocketNotify event = wxSOCKET_LOST);
+    virtual void Uninstall_Callback(wxSocketImpl *socket,
+                                    wxSocketNotify event = wxSOCKET_LOST);
 
 private:
     static wxDynamicLibrary gs_wsock32dll;
 
 private:
     static wxDynamicLibrary gs_wsock32dll;
@@ -209,9 +213,9 @@ bool wxSocketMSWManager::OnInit()
   // dependencies on it for all the application using wx even if they don't use
   // sockets
 #ifdef __WXWINCE__
   // dependencies on it for all the application using wx even if they don't use
   // sockets
 #ifdef __WXWINCE__
-    #define WINSOCK_DLL_NAME _T("ws2.dll")
+    #define WINSOCK_DLL_NAME wxT("ws2.dll")
 #else
 #else
-    #define WINSOCK_DLL_NAME _T("wsock32.dll")
+    #define WINSOCK_DLL_NAME wxT("wsock32.dll")
 #endif
 
     gs_wsock32dll.Load(WINSOCK_DLL_NAME, wxDL_VERBATIM | wxDL_QUIET);
 #endif
 
     gs_wsock32dll.Load(WINSOCK_DLL_NAME, wxDL_VERBATIM | wxDL_QUIET);
@@ -325,12 +329,33 @@ LRESULT CALLBACK wxSocket_Internal_WinProc(HWND hWnd,
         if ( !socket )
             return 0;
 
         if ( !socket )
             return 0;
 
+        // the socket may be already closed but we could still receive
+        // notifications for it sent (asynchronously) before it got closed
+        if ( socket->m_fd == INVALID_SOCKET )
+            return 0;
+
         wxASSERT_MSG( socket->m_fd == (SOCKET)wParam,
                       "mismatch between message and socket?" );
 
         wxASSERT_MSG( socket->m_fd == (SOCKET)wParam,
                       "mismatch between message and socket?" );
 
-        switch WSAGETSELECTEVENT(lParam)
+        switch ( WSAGETSELECTEVENT(lParam) )
         {
             case FD_READ:
         {
             case FD_READ:
+                // We may get a FD_READ notification even when there is no data
+                // to read on the socket, in particular this happens on socket
+                // creation when we seem to always get FD_CONNECT, FD_WRITE and
+                // FD_READ notifications all at once (but it doesn't happen
+                // only then). Ignore such dummy notifications.
+                {
+                    fd_set fds;
+                    timeval tv = { 0, 0 };
+
+                    wxFD_ZERO(&fds);
+                    wxFD_SET(socket->m_fd, &fds);
+
+                    if ( select(socket->m_fd + 1, &fds, NULL, NULL, &tv) != 1 )
+                        return 0;
+                }
+
                 event = wxSOCKET_INPUT;
                 break;
 
                 event = wxSOCKET_INPUT;
                 break;
 
@@ -402,7 +427,7 @@ void wxSocketMSWManager::Install_Callback(wxSocketImpl *socket_,
  *  Disable event notifications (used when shutting down the socket)
  */
 void wxSocketMSWManager::Uninstall_Callback(wxSocketImpl *socket_,
  *  Disable event notifications (used when shutting down the socket)
  */
 void wxSocketMSWManager::Uninstall_Callback(wxSocketImpl *socket_,
-                                           wxSocketNotify WXUNUSED(event))
+                                            wxSocketNotify WXUNUSED(event))
 {
     wxSocketImplMSW * const socket = static_cast<wxSocketImplMSW *>(socket_);
 
 {
     wxSocketImplMSW * const socket = static_cast<wxSocketImplMSW *>(socket_);
 
@@ -430,20 +455,16 @@ static struct ManagerSetter
     }
 } gs_managerSetter;
 
     }
 } gs_managerSetter;
 
+// see the relative linker macro in socket.cpp
+wxFORCE_LINK_THIS_MODULE( mswsocket );
+
 // ============================================================================
 // wxSocketImpl implementation
 // ============================================================================
 
 // ============================================================================
 // wxSocketImpl implementation
 // ============================================================================
 
-/* static */
-wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket)
-{
-    return new wxSocketImplMSW(wxsocket);
-}
-
 void wxSocketImplMSW::DoClose()
 {
 void wxSocketImplMSW::DoClose()
 {
-    wxSocketManager::Get()->
-        Uninstall_Callback(this, wxSOCKET_MAX_EVENT /* unused anyhow */);
+    wxSocketManager::Get()->Uninstall_Callback(this);
 
     closesocket(m_fd);
 }
 
     closesocket(m_fd);
 }