Skip to content

Commit 248071a

Browse files
authored
Merge pull request #106 from desertkun/get_next_finished
Added method get_next_finished
2 parents 863eb0f + b639f3d commit 248071a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

include/curl_multi.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <memory>
3030
#include <vector>
31+
#include <unordered_map>
3132

3233
#include "curl_easy.h"
3334

@@ -268,6 +269,10 @@ namespace curl {
268269
* This method checks if the transfer on a curl_easy object is finished.
269270
*/
270271
bool is_finished(const curl_easy &);
272+
/**
273+
* This method returhns the next finished curl_easy (if there is), otherwise nullptr is returned
274+
*/
275+
curl_easy* get_next_finished();
271276
/**
272277
* Perform all the operations. Go baby! If the performing operations
273278
* have finished, the method returns true. Else, returns false. Check
@@ -326,6 +331,7 @@ namespace curl {
326331
int message_queued;
327332
int active_transfers;
328333
multi_ptr curl;
334+
std::unordered_map<CURL*, curl_easy*> handles;
329335
};
330336

331337
// Implementation of add method
@@ -378,4 +384,4 @@ namespace curl {
378384
}
379385

380386
#undef CURLCPP_DEFINE_OPTION
381-
#endif /* defined(__curlcpp__curl_multi__) */
387+
#endif /* defined(__curlcpp__curl_multi__) */

src/curl_multi.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,19 @@ curl_multi::~curl_multi() NOEXCEPT
5353
// Implementation of add method for easy handlers.
5454
void curl_multi::add(const curl_easy &easy) {
5555
const CURLMcode code = curl_multi_add_handle(this->curl.get(),easy.get_curl());
56-
if (code != CURLM_OK) {
56+
if (code == CURLM_OK) {
57+
handles[easy.get_curl()] = (curl_easy*)&easy;
58+
} else {
5759
throw curl_multi_exception(code,__FUNCTION__);
5860
}
5961
}
6062

6163
// Implementation of remove for easy handlers.
6264
void curl_multi::remove(const curl_easy &easy) {
6365
const CURLMcode code = curl_multi_remove_handle(this->curl.get(),easy.get_curl());
64-
if (code != CURLM_OK) {
66+
if (code == CURLM_OK) {
67+
handles.erase(easy.get_curl());
68+
} else {
6569
throw curl_multi_exception(code,__FUNCTION__);
6670
}
6771
}
@@ -88,6 +92,20 @@ unique_ptr<curl_multi::curl_message> curl_multi::get_info(const curl_easy &easy)
8892
return nullptr;
8993
}
9094

95+
// Implementation of get_next_finished method.
96+
curl_easy* curl_multi::get_next_finished() {
97+
CURLMsg *message = curl_multi_info_read(this->curl.get(),&this->message_queued);
98+
if (!message)
99+
return nullptr;
100+
if (message->msg == CURLMSG_DONE) {
101+
std::unordered_map<CURL*, curl_easy*>::const_iterator it = handles.find(message->easy_handle);
102+
if (it != handles.end()) {
103+
return it->second;
104+
}
105+
}
106+
return nullptr;
107+
}
108+
91109
// Implementation of is_finished method.
92110
bool curl_multi::is_finished(const curl_easy &easy) {
93111
CURLMsg *message = nullptr;

0 commit comments

Comments
 (0)