curl - How to do curl_multi_perform() asynchronously in C++? -
i have come utilize curl
synchronously doing http request. question how can asynchronously?
i did searches lead me documentation of curl_multi_*
interface question , example didn't solve @ all.
my simplified code:
curlm *curlm; int handle_count = 0; curlm = curl_multi_init(); curl *curl = null; curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, curlopt_url, "http://stackoverflow.com/"); curl_easy_setopt(curl, curlopt_writefunction, writecallback); curl_multi_add_handle(curlm, curl); curl_multi_perform(curlm, &handle_count); } curl_global_cleanup();
the callback method writecallback
doesn't called , nil happens.
please advise me.
edit:
according @remy's below answer, got seems it's not quite needed. cause using loop still blocking one. please tell me if i'm doing wrong or misunderstanding something. i'm pretty new c++.
here's code again:
int main(int argc, const char * argv[]) { using namespace std; curlm *curlm; int handle_count; curlm = curl_multi_init(); curl *curl1 = null; curl1 = curl_easy_init(); curl *curl2 = null; curl2 = curl_easy_init(); if(curl1 && curl2) { curl_easy_setopt(curl1, curlopt_url, "http://stackoverflow.com/"); curl_easy_setopt(curl1, curlopt_writefunction, writecallback); curl_multi_add_handle(curlm, curl1); curl_easy_setopt(curl2, curlopt_url, "http://google.com/"); curl_easy_setopt(curl2, curlopt_writefunction, writecallback); curl_multi_add_handle(curlm, curl2); curlmcode code; while(1) { code = curl_multi_perform(curlm, &handle_count); if(handle_count == 0) { break; } } } curl_global_cleanup(); cout << "hello, world!\n"; homecoming 0; }
i can 2 http requests simultaneously. callbacks called still need finish before executing next lines. have think of thread?
read documentation 1 time again more carefully, particularly these portions:
http://curl.haxx.se/libcurl/c/libcurl-multi.html
your application can acquire knowledge libcurl when invoked transfer data, don't have busy-loop , phone call curl_multi_perform(3) crazy. curl_multi_fdset(3) offers interface using can extract fd_sets libcurl utilize in select() or poll() calls in order know when transfers in multi stack might need attention. makes easy programme wait input on own private file descriptors @ same time or perhaps timeout every , then, should want that.
http://curl.haxx.se/libcurl/c/curl_multi_perform.html
when application has found out there's info available multi_handle or timeout has elapsed, application should phone call function read/write whatever there read or write right now etc. curl_multi_perform() returns reads/writes done. function not require there info available reading or info can written, can called in case. write number of handles still transfer info in sec argument's integer-pointer.
if amount of running_handles changed previous phone call (or less amount of easy handles you've added multi handle), know there 1 or more transfers less "running". can phone call curl_multi_info_read(3) info each individual completed transfer, , returned info includes curlcode , more. if added handle fails quickly, may never counted running_handle.
when running_handles set 0 (0) on homecoming of function, there no longer transfers in progress.
in other words, need run loop polls libcurl status, calling curl_multi_perform()
whenever there info waiting transferred, repeating needed until there nil left transfer.
the blog article linked mentions looping:
the code can used like
http http; http:addrequest("http://www.google.com");
// in update loop called each frame http:update();
you not doing looping in code, why callback not beingness called. new info has not been received yet when phone call curl_multi_perform()
1 time.
c++ curl asynchronous libcurl
No comments:
Post a Comment