Cleaned up CurlStack.

This commit is contained in:
rakshasa
2014-04-18 22:16:24 +09:00
parent 38f39bdb23
commit d9f6f5aef4
2 changed files with 42 additions and 13 deletions
+40 -13
View File
@@ -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)));
+2
View File
@@ -125,6 +125,8 @@ class CurlStack : std::deque<CurlGet*> {
void receive_timeout();
bool process_done_handle();
void* m_handle;
unsigned int m_active;