From d9f6f5aef42ff154f95dd65e3a35cd55453b2f3a Mon Sep 17 00:00:00 2001 From: rakshasa Date: Fri, 18 Apr 2014 22:16:24 +0900 Subject: [PATCH] Cleaned up CurlStack. --- src/core/curl_stack.cc | 53 +++++++++++++++++++++++++++++++----------- src/core/curl_stack.h | 2 ++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/core/curl_stack.cc b/src/core/curl_stack.cc index f0e6e8ec..a5e0e499 100644 --- a/src/core/curl_stack.cc +++ b/src/core/curl_stack.cc @@ -86,34 +86,44 @@ CurlStack::new_socket(int fd) { void CurlStack::receive_action(CurlSocket* socket, int events) { + if (this <= (CurlStack*)0x100) + throw torrent::internal_error("CurlStack::receive_action(...) called with shit == NULL"); + + if (this >= (CurlStack*)0x00007fff00000000) + throw torrent::internal_error("CurlStack::receive_action(...) called with this == 0x00007fff00000002"); + + if (socket != NULL && socket <= (CurlSocket*)0x100) + throw torrent::internal_error("CurlStack::receive_action(...) called with socket == NULL"); + + if (socket >= (CurlSocket*)0x00007fff00000000) + throw torrent::internal_error("CurlStack::receive_action(...) called with socket == 0x00007fff00000002"); + CURLMcode code; do { int count; #if (LIBCURL_VERSION_NUM >= 0x071003) - code = curl_multi_socket_action((CURLM*)m_handle, socket != NULL ? socket->file_descriptor() : CURL_SOCKET_TIMEOUT, events, &count); + code = curl_multi_socket_action((CURLM*)m_handle, + socket != NULL ? socket->file_descriptor() : CURL_SOCKET_TIMEOUT, + events, + &count); #else - code = curl_multi_socket((CURLM*)m_handle, socket != NULL ? socket->file_descriptor() : CURL_SOCKET_TIMEOUT, &count); + code = curl_multi_socket((CURLM*)m_handle, + socket != NULL ? socket->file_descriptor() : CURL_SOCKET_TIMEOUT, + &count); #endif if (code > 0) throw torrent::internal_error("Error calling curl_multi_socket_action."); - // Socket might be removed when cleaning handles below, future calls should not use it. + // Socket might be removed when cleaning handles below, future + // calls should not use it. socket = NULL; events = 0; if ((unsigned int)count != size()) { - // Done with some handles. - int t; - CURLMsg* msg; - - while ((msg = curl_multi_info_read((CURLM*)m_handle, &t)) != NULL) { - if (msg->msg != CURLMSG_DONE) - throw torrent::internal_error("CurlStack::receive_action() msg->msg != CURLMSG_DONE."); - - transfer_done(msg->easy_handle, msg->data.result == CURLE_OK ? NULL : curl_easy_strerror(msg->data.result)); - } + while (process_done_handle()) + ; // Do nothing. if (empty()) priority_queue_erase(&taskScheduler, &m_taskTimeout); @@ -122,6 +132,23 @@ CurlStack::receive_action(CurlSocket* socket, int events) { } while (code == CURLM_CALL_MULTI_PERFORM); } +bool +CurlStack::process_done_handle() { + int remaining_msgs = 0; + CURLMsg* msg = curl_multi_info_read((CURLM*)m_handle, &remaining_msgs); + + if (msg == NULL) + return false; + + if (msg->msg != CURLMSG_DONE) + throw torrent::internal_error("CurlStack::receive_action() msg->msg != CURLMSG_DONE."); + + transfer_done(msg->easy_handle, + msg->data.result == CURLE_OK ? NULL : curl_easy_strerror(msg->data.result)); + + return remaining_msgs != 0; +} + void CurlStack::transfer_done(void* handle, const char* msg) { iterator itr = std::find_if(begin(), end(), rak::equal(handle, std::mem_fun(&CurlGet::handle))); diff --git a/src/core/curl_stack.h b/src/core/curl_stack.h index d17f825c..3b8b341b 100644 --- a/src/core/curl_stack.h +++ b/src/core/curl_stack.h @@ -125,6 +125,8 @@ class CurlStack : std::deque { void receive_timeout(); + bool process_done_handle(); + void* m_handle; unsigned int m_active;