X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/204b046ea0f1732af4026f92e5a65e6070feaf4c..b6b59e43ca3972b4b3d30f0e75440870410dc321:/src/common/socket.cpp diff --git a/src/common/socket.cpp b/src/common/socket.cpp index ee1365dbaa..05f337bfc3 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -159,7 +159,7 @@ void wxSocketBase::Shutdown() { // we should be initialized wxASSERT_MSG( m_countInit, _T("extra call to Shutdown()") ); - if ( !--m_countInit ) + if ( --m_countInit == 0 ) { GSocket_Cleanup(); } @@ -694,6 +694,8 @@ bool wxSocketBase::_Wait(long seconds, else timeout = m_timeout * 1000; + bool has_event_loop = wxTheApp ? (wxTheApp->GetTraits() ? true : false) : false; + // Wait in an active polling loop. // // NOTE: We duplicate some of the code in OnRequest, but this doesn't @@ -709,14 +711,15 @@ bool wxSocketBase::_Wait(long seconds, bool done = false; bool valid_result = false; -#if !defined(wxUSE_GUI) || !wxUSE_GUI + if (!has_event_loop) + { // This is used to avoid a busy loop on wxBase - having a select // timeout of 50 ms per iteration should be enough. if (timeout > 50) m_socket->SetTimeout(50); else m_socket->SetTimeout(timeout); -#endif + } while (!done) { @@ -754,20 +757,22 @@ bool wxSocketBase::_Wait(long seconds, done = true; else { -#if !defined(wxUSE_GUI) || !wxUSE_GUI + if (has_event_loop) + { + PROCESS_EVENTS(); + } + else + { // If there's less than 50 ms left, just call select with that timeout. if (time_left < 50) m_socket->SetTimeout(time_left); -#else - PROCESS_EVENTS(); -#endif - } + } + } } // Set timeout back to original value (we overwrote it for polling) -#if !defined(wxUSE_GUI) || !wxUSE_GUI - m_socket->SetTimeout(m_timeout*1000); -#endif + if (!has_event_loop) + m_socket->SetTimeout(m_timeout*1000); return valid_result; } @@ -1198,6 +1203,21 @@ bool wxSocketBase::SetOption(int level, int optname, const void *optval, return true; } +bool wxSocketBase::SetLocal(wxIPV4address& local) +{ + GAddress* la = local.GetAddress(); + + // If the address is valid, save it for use when we call Connect + if (la && la->m_addr) + { + m_localAddress = local; + + return true; + } + + return false; +} + // ========================================================================== // wxSocketClient // ========================================================================== @@ -1219,7 +1239,7 @@ wxSocketClient::~wxSocketClient() // Connect // -------------------------------------------------------------------------- -bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait) +bool wxSocketClient::DoConnect(wxSockAddress& addr_man, wxSockAddress* local, bool wait) { GSocketError err; @@ -1249,6 +1269,27 @@ bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait) if (!wait) m_socket->SetNonBlocking(1); + // Reuse makes sense for clients too, if we are trying to rebind to the same port + if (GetFlags() & wxSOCKET_REUSEADDR) + { + m_socket->SetReusable(); + } + + // If no local address was passed and one has been set, use the one that was Set + if (!local && m_localAddress.GetAddress()) + { + local = &m_localAddress; + } + + // Bind to the local IP address and port, when provided + if (local) + { + GAddress* la = local->GetAddress(); + + if (la && la->m_addr) + m_socket->SetLocal(la); + } + m_socket->SetPeer(addr_man.GetAddress()); err = m_socket->Connect(GSOCK_STREAMED); @@ -1267,6 +1308,16 @@ bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait) return true; } +bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait) +{ + return (DoConnect(addr_man, NULL, wait)); +} + +bool wxSocketClient::Connect(wxSockAddress& addr_man, wxSockAddress& local, bool wait) +{ + return (DoConnect(addr_man, &local, wait)); +} + bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds) { if (m_connected) // Already connected