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
This commit is contained in:
Vadim Zeitlin
2009-08-16 23:13:45 +00:00
parent 439293064d
commit 4f260c9c68
5 changed files with 33 additions and 23 deletions

View File

@@ -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

View File

@@ -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
//

View File

@@ -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

View File

@@ -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);

View File

@@ -66,13 +66,6 @@
// wxSocketImpl implementation
// ============================================================================
/* static */
wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket)
{
return new wxSocketImplUnix(wxsocket);
}
wxSocketError wxSocketImplUnix::GetLastError() const
{
switch ( errno )