X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/39b61b058123378e0e132f7f75f53f042d96eca0..263e3a8327f544990c7ba7edb8cd34994c908049:/src/common/socketiohandler.cpp diff --git a/src/common/socketiohandler.cpp b/src/common/socketiohandler.cpp index 89f8edafee..cadb78f4ec 100644 --- a/src/common/socketiohandler.cpp +++ b/src/common/socketiohandler.cpp @@ -6,7 +6,7 @@ // RCS-ID: $Id$ // Copyright: (c) 2006 Angel vidal // (c) 2007 Vadim Zeitlin -// License: wxWindows licence +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// // ============================================================================ @@ -20,82 +20,95 @@ // for compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" -#if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER +#ifdef __BORLANDC__ + #pragma hdrstop +#endif +#if wxUSE_SOCKETS + +#include "wx/app.h" #include "wx/apptrait.h" #include "wx/private/socket.h" +#include "wx/link.h" // ============================================================================ -// implementation +// wxSocketFDBasedManager implementation // ============================================================================ -// ---------------------------------------------------------------------------- -// wxSocketFDIOManager: socket manager using wxFDIODispatcher -// ---------------------------------------------------------------------------- - -class wxSocketFDIOManager : public wxSocketFDBasedManager -{ -public: - virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event); - virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event); -}; - -void wxSocketFDIOManager::Install_Callback(wxSocketImpl *socket_, - wxSocketNotify event) +bool wxSocketFDBasedManager::OnInit() { - wxSocketImplUnix * const socket = static_cast(socket_); + wxAppTraits * const traits = wxApp::GetTraitsIfExists(); + if ( !traits ) + return false; - const int fd = socket->m_fd; - - if ( fd == -1 ) - return; - - const SocketDir d = GetDirForEvent(socket, event); + m_fdioManager = traits->GetFDIOManager(); + return m_fdioManager != NULL; +} - wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); - if ( !dispatcher ) - return; +void wxSocketFDBasedManager::Install_Callback(wxSocketImpl *socket_, + wxSocketNotify event) +{ + wxSocketImplUnix * const + socket = static_cast(socket_); - FD(socket, d) = fd; + wxCHECK_RET( socket->m_fd != -1, + "shouldn't be called on invalid socket" ); - // register it when it's used for the first time, update it if it had been - // previously registered - const bool alreadyRegistered = socket->HasAnyEnabledCallbacks(); + const wxFDIOManager::Direction d = GetDirForEvent(socket, event); - socket->EnableCallback(d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT); + int& fd = FD(socket, d); + if ( fd != -1 ) + m_fdioManager->RemoveInput(socket, fd, d); - if ( alreadyRegistered ) - dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks()); - else - dispatcher->RegisterFD(fd, socket, socket->GetEnabledCallbacks()); + fd = m_fdioManager->AddInput(socket, socket->m_fd, d); } -void wxSocketFDIOManager::Uninstall_Callback(wxSocketImpl *socket_, - wxSocketNotify event) +void wxSocketFDBasedManager::Uninstall_Callback(wxSocketImpl *socket_, + wxSocketNotify event) { - wxSocketImplUnix * const socket = static_cast(socket_); - - const SocketDir d = GetDirForEvent(socket, event); + wxSocketImplUnix * const + socket = static_cast(socket_); - const int fd = FD(socket, d); - if ( fd == -1 ) - return; + const wxFDIOManager::Direction d = GetDirForEvent(socket, event); - FD(socket, d) = -1; - - const wxFDIODispatcherEntryFlags - flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT; - - wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); - if ( !dispatcher ) - return; - - socket->DisableCallback(flag); + int& fd = FD(socket, d); + if ( fd != -1 ) + { + m_fdioManager->RemoveInput(socket, fd, d); + fd = -1; + } +} - if ( !socket->HasAnyEnabledCallbacks() ) - dispatcher->UnregisterFD(fd); - else - dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks()); +wxFDIOManager::Direction +wxSocketFDBasedManager::GetDirForEvent(wxSocketImpl *socket, + wxSocketNotify event) +{ + switch ( event ) + { + default: + wxFAIL_MSG( "unknown socket event" ); + return wxFDIOManager::INPUT; // we must return something + + case wxSOCKET_LOST: + wxFAIL_MSG( "unexpected socket event" ); + return wxFDIOManager::INPUT; // as above + + case wxSOCKET_INPUT: + return wxFDIOManager::INPUT; + + case wxSOCKET_OUTPUT: + return wxFDIOManager::OUTPUT; + + case wxSOCKET_CONNECTION: + // for server sockets we're interested in events indicating + // that a new connection is pending, i.e. that accept() will + // succeed and this is indicated by socket becoming ready for + // reading, while for the other ones we're interested in the + // completion of non-blocking connect() which is indicated by + // the socket becoming ready for writing + return socket->IsServer() ? wxFDIOManager::INPUT + : wxFDIOManager::OUTPUT; + } } // set the wxBase variable to point to our wxSocketManager implementation @@ -106,9 +119,13 @@ static struct ManagerSetter { ManagerSetter() { - static wxSocketFDIOManager s_manager; + static wxSocketFDBasedManager s_manager; wxAppTraits::SetDefaultSocketManager(&s_manager); } } gs_managerSetter; + +// see the relative linker macro in socket.cpp +wxFORCE_LINK_THIS_MODULE( socketiohandler ); + #endif // wxUSE_SOCKETS