X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c3e646b47f99fd832ab29c07554b533ea8a807e4..10d1f4131db5405eaee6a49d3a96dd0ca4706294:/src/common/socket.cpp diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 7ec0743ec1..d79c4986c2 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -76,6 +76,8 @@ #define PROCESS_EVENTS() #endif // wxUSE_GUI/!wxUSE_GUI +#define wxTRACE_Socket _T("wxSocket") + // -------------------------------------------------------------------------- // wxWin macros // -------------------------------------------------------------------------- @@ -104,12 +106,52 @@ public: public: wxSocketState() : wxObject() {} + + DECLARE_NO_COPY_CLASS(wxSocketState) }; // ========================================================================== // wxSocketBase // ========================================================================== +// -------------------------------------------------------------------------- +// Initialization and shutdown +// -------------------------------------------------------------------------- + +// FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses +// to m_countInit with a crit section +size_t wxSocketBase::m_countInit = 0; + +bool wxSocketBase::IsInitialized() +{ + return m_countInit > 0; +} + +bool wxSocketBase::Initialize() +{ + if ( !m_countInit++ ) + { + if ( !GSocket_Init() ) + { + m_countInit--; + + return FALSE; + } + } + + return TRUE; +} + +void wxSocketBase::Shutdown() +{ + // we should be initialized + wxASSERT_MSG( m_countInit, _T("extra call to Shutdown()") ); + if ( !--m_countInit ) + { + GSocket_Cleanup(); + } +} + // -------------------------------------------------------------------------- // Ctor and dtor // -------------------------------------------------------------------------- @@ -145,6 +187,13 @@ void wxSocketBase::Init() m_cbk = NULL; m_cdata = NULL; #endif // WXWIN_COMPATIBILITY + + if ( !IsInitialized() ) + { + // this Initialize() will be undone by wxSocketModule::OnExit(), all the + // other calls to it should be matched by a call to Shutdown() + Initialize(); + } } wxSocketBase::wxSocketBase() @@ -732,6 +781,12 @@ bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const return FALSE; peer = GSocket_GetPeer(m_socket); + + // copying a null address would just trigger an assert anyway + + if (!peer) + return FALSE; + addr_man.SetAddress(peer); GAddress_destroy(peer); @@ -779,11 +834,11 @@ void wxSocketBase::RestoreState() wxNode *node; wxSocketState *state; - node = m_states.Last(); + node = m_states.GetLast(); if (!node) return; - state = (wxSocketState *)node->Data(); + state = (wxSocketState *)node->GetData(); m_flags = state->m_flags; m_notify = state->m_notify; @@ -862,9 +917,10 @@ char *wxSocketBase::CallbackData(char *data) // decoupled from wx_socket_callback and thus they suffer from a variety // of problems. Avoid them where possible and use events instead. -static void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket), - GSocketEvent notification, - char *cdata) +extern "C" +void LINKAGEMODE wx_socket_callback(GSocket * WXUNUSED(socket), + GSocketEvent notification, + char *cdata) { wxSocketBase *sckobj = (wxSocketBase *)cdata; @@ -1021,26 +1077,32 @@ wxSocketServer::wxSocketServer(wxSockAddress& addr_man, wxSocketFlags flags) : wxSocketBase(flags, wxSOCKET_SERVER) { - // Create the socket - m_socket = GSocket_new(); + wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") ); - if (!m_socket) - return; + m_socket = GSocket_new(); - // Setup the socket as server - GSocket_SetLocal(m_socket, addr_man.GetAddress()); - if (GSocket_SetServer(m_socket) != GSOCK_NOERROR) - { - GSocket_destroy(m_socket); - m_socket = NULL; - return; - } + if (!m_socket) + { + wxLogTrace( wxTRACE_Socket, _T("*** GSocket_new failed") ); + return; + } - GSocket_SetTimeout(m_socket, m_timeout * 1000); - GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | - GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, - wx_socket_callback, (char *)this); + // Setup the socket as server + + GSocket_SetLocal(m_socket, addr_man.GetAddress()); + if (GSocket_SetServer(m_socket) != GSOCK_NOERROR) + { + GSocket_destroy(m_socket); + m_socket = NULL; + wxLogTrace( wxTRACE_Socket, _T("*** GSocket_SetServer failed") ); + return; + } + + GSocket_SetTimeout(m_socket, m_timeout * 1000); + GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG | + GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG, + wx_socket_callback, (char *)this); } // -------------------------------------------------------------------------- @@ -1235,39 +1297,32 @@ wxDatagramSocket& wxDatagramSocket::SendTo( wxSockAddress& addr, return (*this); } -// ========================================================================== -// wxSocketEvent -// ========================================================================== - -wxSocketEvent::wxSocketEvent(int id) : wxEvent(id) -{ - SetEventType( (wxEventType)wxEVT_SOCKET ); -} - -void wxSocketEvent::CopyObject(wxObject& object_dest) const -{ - wxSocketEvent *event = (wxSocketEvent *)&object_dest; - - wxEvent::CopyObject(object_dest); - - event->m_event = m_event; - event->m_clientData = m_clientData; -} - // ========================================================================== // wxSocketModule // ========================================================================== class WXDLLEXPORT wxSocketModule : public wxModule { - DECLARE_DYNAMIC_CLASS(wxSocketModule) - public: - bool OnInit() { return GSocket_Init() != 0; } - void OnExit() { GSocket_Cleanup(); } + virtual bool OnInit() + { + // wxSocketBase will call GSocket_Init() itself when/if needed + return TRUE; + } + + virtual void OnExit() + { + if ( wxSocketBase::IsInitialized() ) + wxSocketBase::Shutdown(); + } + +private: + DECLARE_DYNAMIC_CLASS(wxSocketModule) }; IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule) #endif // wxUSE_SOCKETS + +// vi:sts=4:sw=4:et