From 4f260c9c68b42e6ccc82710efe6ca6e8418e721d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 16 Aug 2009 23:13:45 +0000 Subject: [PATCH] Virtualize wxSocketImpl creation by routing it via wxSocketManager. This is necessary to create different kinds of sockets for the console and GUI applications under OS X: unlike Unix which use different socket managers for the console and GUI programs but still use the same data structures in both cases as X11 and derived toolkits APIs are very similar, Core Foundation sockets don't have anything in common with their console counterparts and so we need to use different wxSocketImpl versions too. A side effect of this commit is that now we need to force linking of src/msw/sockmsw.cpp when using sockets: this wasn't necessary before because it contained wxSocketImpl method definition but now that there are no more direct dependencies on it, MSVC linker simply discards the object file unless we force it to link with it. Notice that this commit doesn't change anything yet, it simply refactors the code to use wxSocketManager::CreateSocket() instead of wxSocketImpl::Create() in preparation for the next change. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61675 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/private/socket.h | 11 +++++------ include/wx/unix/private/sockunix.h | 5 +++++ src/common/socket.cpp | 23 +++++++++++++++++++---- src/msw/sockmsw.cpp | 10 ++++------ src/unix/sockunix.cpp | 7 ------- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/include/wx/private/socket.h b/include/wx/private/socket.h index 936eb13b5e..89a5bd0004 100644 --- a/include/wx/private/socket.h +++ b/include/wx/private/socket.h @@ -139,6 +139,9 @@ public: virtual void OnExit() = 0; + // create the socket implementation object matching this manager + virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket) = 0; + // these functions enable or disable monitoring of the given socket for the // specified events inside the currently running event loop (but notice // that both BSD and Winsock implementations actually use socket->m_server @@ -167,16 +170,12 @@ private: Base class for all socket implementations providing functionality common to BSD and Winsock sockets. - Objects of this class are not created directly but only via its static - Create() method which is implemented in port-specific code. + Objects of this class are not created directly but only via the factory + function wxSocketManager::CreateSocket(). */ class wxSocketImpl { public: - // static factory function: creates the low-level socket associated with - // the given wxSocket (and inherits its attributes such as timeout) - static wxSocketImpl *Create(wxSocketBase& wxsocket); - virtual ~wxSocketImpl(); // set various socket properties: all of those can only be called before diff --git a/include/wx/unix/private/sockunix.h b/include/wx/unix/private/sockunix.h index 9ba332b842..a5832a9443 100644 --- a/include/wx/unix/private/sockunix.h +++ b/include/wx/unix/private/sockunix.h @@ -117,6 +117,11 @@ public: virtual bool OnInit() { return true; } virtual void OnExit() { } + virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket) + { + return new wxSocketImplUnix(wxsocket); + } + protected: // identifies either input or output direction // diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 09324181ae..0bb79b219f 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -533,7 +533,14 @@ wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket) if ( fd == INVALID_SOCKET ) return NULL; - wxSocketImpl * const sock = Create(wxsocket); + wxSocketManager * const manager = wxSocketManager::Get(); + if ( !manager ) + return NULL; + + wxSocketImpl * const sock = manager->CreateSocket(wxsocket); + if ( !sock ) + return NULL; + sock->m_fd = fd; sock->m_peer = wxSockAddressImpl(from.addr, fromlen); @@ -1739,7 +1746,8 @@ wxSocketServer::wxSocketServer(const wxSockAddress& addr, { wxLogTrace( wxTRACE_Socket, wxT("Opening wxSocketServer") ); - m_impl = wxSocketImpl::Create(*this); + wxSocketManager * const manager = wxSocketManager::Get(); + m_impl = manager ? manager->CreateSocket(*this) : NULL; if (!m_impl) { @@ -1897,7 +1905,8 @@ bool wxSocketClient::DoConnect(const wxSockAddress& remote, m_establishing = false; // Create and set up the new one - m_impl = wxSocketImpl::Create(*this); + wxSocketManager * const manager = wxSocketManager::Get(); + m_impl = manager ? manager->CreateSocket(*this) : NULL; if ( !m_impl ) return false; @@ -1979,7 +1988,8 @@ wxDatagramSocket::wxDatagramSocket( const wxSockAddress& addr, : wxSocketBase( flags, wxSOCKET_DATAGRAM ) { // Create the socket - m_impl = wxSocketImpl::Create(*this); + wxSocketManager * const manager = wxSocketManager::Get(); + m_impl = manager ? manager->CreateSocket(*this) : NULL; if (!m_impl) return; @@ -2064,4 +2074,9 @@ IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) wxFORCE_LINK_MODULE( socketiohandler ) #endif +// same for ManagerSetter in the MSW file +#ifdef __WXMSW__ + wxFORCE_LINK_MODULE( mswsocket ) +#endif + #endif // wxUSE_SOCKETS diff --git a/src/msw/sockmsw.cpp b/src/msw/sockmsw.cpp index 96b354fa30..1561c69f8f 100644 --- a/src/msw/sockmsw.cpp +++ b/src/msw/sockmsw.cpp @@ -33,6 +33,7 @@ #include "wx/apptrait.h" #include "wx/thread.h" #include "wx/dynlib.h" +#include "wx/link.h" #ifdef __WXWINCE__ /* @@ -437,16 +438,13 @@ static struct ManagerSetter } } gs_managerSetter; +// see the relative linker macro in socket.cpp +wxFORCE_LINK_THIS_MODULE( mswsocket ); + // ============================================================================ // wxSocketImpl implementation // ============================================================================ -/* static */ -wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket) -{ - return new wxSocketImplMSW(wxsocket); -} - void wxSocketImplMSW::DoClose() { wxSocketManager::Get()->Uninstall_Callback(this); diff --git a/src/unix/sockunix.cpp b/src/unix/sockunix.cpp index 51a158cab6..b2e298d83f 100644 --- a/src/unix/sockunix.cpp +++ b/src/unix/sockunix.cpp @@ -66,13 +66,6 @@ // wxSocketImpl implementation // ============================================================================ -/* static */ -wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket) -{ - return new wxSocketImplUnix(wxsocket); -} - - wxSocketError wxSocketImplUnix::GetLastError() const { switch ( errno ) -- 2.45.2