From 879a0197572c0159b9cc1e3e581017d78fcb27cc Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sun, 31 Aug 2008 20:49:55 +0000 Subject: [PATCH] * Various fixes to the new polling code to handle bad libcurl/cares behavior. Patch by Josef Drexler. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1067 e378c898-3ddf-0310-93e7-cc216c733640 --- src/core/curl_socket.cc | 18 ++++++++++++------ src/core/curl_stack.cc | 12 ++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/core/curl_socket.cc b/src/core/curl_socket.cc index 17a22393..c91668d9 100644 --- a/src/core/curl_socket.cc +++ b/src/core/curl_socket.cc @@ -99,29 +99,35 @@ CurlSocket::close() { if (m_fileDesc == -1) throw torrent::internal_error("CurlSocket::close() m_fileDesc == -1."); - // Extra cleanup to avoid leaving dangling polling events in case - // libcurl decides to leave the fd open for reuse. - control->poll()->remove_read(this); - control->poll()->remove_write(this); - control->poll()->remove_error(this); - control->poll()->closed(this); m_fileDesc = -1; } void CurlSocket::event_read() { +#if (LIBCURL_VERSION_NUM >= 0x071003) return m_stack->receive_action(this, CURL_CSELECT_IN); +#else + return m_stack->receive_action(this, 0); +#endif } void CurlSocket::event_write() { +#if (LIBCURL_VERSION_NUM >= 0x071003) return m_stack->receive_action(this, CURL_CSELECT_OUT); +#else + return m_stack->receive_action(this, 0); +#endif } void CurlSocket::event_error() { +#if (LIBCURL_VERSION_NUM >= 0x071003) return m_stack->receive_action(this, CURL_CSELECT_ERR); +#else + return m_stack->receive_action(this, 0); +#endif } } diff --git a/src/core/curl_stack.cc b/src/core/curl_stack.cc index 51ea8076..a4bc212a 100644 --- a/src/core/curl_stack.cc +++ b/src/core/curl_stack.cc @@ -132,6 +132,14 @@ CurlStack::receive_action(CurlSocket* socket, int events) { void CurlStack::receive_timeout() { receive_action(NULL, 0); + + // Sometimes libcurl forgets to reset the timeout. Try to poll the value in that case, or use 10 seconds. + if (!empty() && !m_taskTimeout.is_queued()) { + long timeout; + curl_multi_timeout((CURLM*)m_handle, &timeout); + priority_queue_insert(&taskScheduler, &m_taskTimeout, + cachedTime + rak::timer::from_milliseconds(std::max(timeout, 10000))); + } } void @@ -161,6 +169,10 @@ CurlStack::add_get(CurlGet* get) { if (curl_multi_add_handle((CURLM*)m_handle, get->handle()) > 0) throw torrent::internal_error("Error calling curl_multi_add_handle."); + +#if (LIBCURL_VERSION_NUM < 0x071000) + receive_timeout(); +#endif } void