X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/999836aacade840a5d9a0cbb5fad7b1d4de62c93..c48908df67c6504891cccd34088cad955b1357ca:/src/common/socket.cpp diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 3706c62893..bbbdc01e59 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -54,7 +54,7 @@ WX_CHECK_BUILD_OPTIONS("wxNet") // what to do within waits: we have 2 cases: from the main thread itself we // have to call wxYield() to let the events (including the GUI events and the -// low-level (not wxWindows) events from GSocket) be processed. From another +// low-level (not wxWidgets) events from GSocket) be processed. From another // thread it is enough to just call wxThread::Yield() which will give away the // rest of our time slice: the explanation is that the events will be processed // by the main thread anyhow, without calling wxYield(), but we don't want to @@ -122,6 +122,26 @@ bool wxSocketBase::Initialize() { if ( !m_countInit++ ) { +#ifdef __WXMSW__ + /* + The following asserting might be neccessary for linux as well, + but I cannot verify this. + */ + wxASSERT(wxThread::IsMain(), + wxT("To use sockets in a secondary thread, ") + wxT("call wxSocketBase::Initialize() from the main thread.")); + /* + Details: Initialize() creates a hidden window as a sink for socket + events, such as 'read completed'. wxMSW has only one message loop + for the main thread. If Initialize is called in a secondary thread, + the socket window will be created for the secondary thread, but + since there is no message loop on this thread, it will never + receive events and all socket operations will time out. + BTW, the main thread must not be stopped using sleep or block + on a semaphore (a bad idea in any case) or socket operations + will time out. + */ +#endif wxAppTraits *traits = wxAppConsole::GetInstance() ? wxAppConsole::GetInstance()->GetTraits() : NULL; GSocketGUIFunctionsTable *functions = @@ -678,6 +698,10 @@ bool wxSocketBase::_Wait(long seconds, else timeout = m_timeout * 1000; +#if !defined(wxUSE_GUI) || !wxUSE_GUI + GSocket_SetTimeout(m_socket, timeout); +#endif + // Wait in an active polling loop. // // NOTE: We duplicate some of the code in OnRequest, but this doesn't @@ -750,6 +774,7 @@ bool wxSocketBase::WaitForRead(long seconds, long milliseconds) GSOCK_LOST_FLAG); } + bool wxSocketBase::WaitForWrite(long seconds, long milliseconds) { return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG); @@ -1122,6 +1147,27 @@ bool wxSocketServer::WaitForAccept(long seconds, long milliseconds) return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG); } +bool wxSocketBase::GetOption(int level, int optname, void *optval, int *optlen) +{ + if (GSocket_GetSockOpt(m_socket, level, optname, optval, optlen) + != GSOCK_NOERROR) + { + return FALSE; + } + return TRUE; +} + +bool wxSocketBase::SetOption(int level, int optname, const void *optval, + int optlen) +{ + if (GSocket_SetSockOpt(m_socket, level, optname, optval, optlen) + != GSOCK_NOERROR) + { + return FALSE; + } + return TRUE; +} + // ========================================================================== // wxSocketClient // ========================================================================== @@ -1217,9 +1263,12 @@ wxDatagramSocket::wxDatagramSocket( wxSockAddress& addr, m_socket = GSocket_new(); if(!m_socket) + { + wxASSERT_MSG( 0, _T("datagram socket not new'd") ); return; - + } // Setup the socket as non connection oriented + GSocket_Unstreamed(m_socket); GSocket_SetLocal(m_socket, addr.GetAddress()); if( GSocket_SetNonOriented(m_socket) != GSOCK_NOERROR ) {