本文整理汇总了C++中curl_multi_cleanup函数的典型用法代码示例。如果您正苦于以下问题:C++ curl_multi_cleanup函数的具体用法?C++ curl_multi_cleanup怎么用?C++ curl_multi_cleanup使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curl_multi_cleanup函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test
int test(char *URL)
{
CURL *c;
CURLM *m = NULL;
int res = 0;
int running=1;
double connect_time = 0.0;
struct timeval mp_start;
char mp_timedout = FALSE;
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
fprintf(stderr, "curl_global_init() failed\n");
return TEST_ERR_MAJOR_BAD;
}
if ((c = curl_easy_init()) == NULL) {
fprintf(stderr, "curl_easy_init() failed\n");
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
test_setopt(c, CURLOPT_HEADER, 1L);
test_setopt(c, CURLOPT_URL, URL);
if ((m = curl_multi_init()) == NULL) {
fprintf(stderr, "curl_multi_init() failed\n");
curl_easy_cleanup(c);
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
if ((res = (int)curl_multi_add_handle(m, c)) != CURLM_OK) {
fprintf(stderr, "curl_multi_add_handle() failed, "
"with code %d\n", res);
curl_multi_cleanup(m);
curl_easy_cleanup(c);
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
mp_timedout = FALSE;
mp_start = tutil_tvnow();
while (running) {
res = (int)curl_multi_perform(m, &running);
if (tutil_tvdiff(tutil_tvnow(), mp_start) >
MULTI_PERFORM_HANG_TIMEOUT) {
mp_timedout = TRUE;
break;
}
if (running <= 0) {
fprintf(stderr, "nothing left running.\n");
break;
}
}
if (mp_timedout) {
if (mp_timedout) fprintf(stderr, "mp_timedout\n");
fprintf(stderr, "ABORTING TEST, since it seems "
"that it would have run forever.\n");
res = TEST_ERR_RUNS_FOREVER;
}
curl_easy_getinfo(c, CURLINFO_CONNECT_TIME, &connect_time);
if (connect_time <= 0.0) {
fprintf(stderr, "connect time is <=0.0\n");
res = TEST_ERR_MAJOR_BAD;
}
test_cleanup:
if(m) {
curl_multi_remove_handle(m, c);
curl_multi_cleanup(m);
}
curl_easy_cleanup(c);
curl_global_cleanup();
return res;
}
示例2: main
//.........这里部分代码省略.........
num_transfers = 3;
/* init a multi stack */
multi_handle = curl_multi_init();
for(i = 0; i<num_transfers; i++) {
setup(&trans[i], i, filename);
/* add the individual transfer */
curl_multi_add_handle(multi_handle, trans[i].hnd);
}
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
/* We do HTTP/2 so let's stick to one connection per host */
curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
/* we start some action by calling perform right away */
curl_multi_perform(multi_handle, &still_running);
while(still_running) {
struct timeval timeout;
int rc; /* select() return code */
CURLMcode mc; /* curl_multi_fdset() return code */
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
long curl_timeo = -1;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
curl_multi_timeout(multi_handle, &curl_timeo);
if(curl_timeo >= 0) {
timeout.tv_sec = curl_timeo / 1000;
if(timeout.tv_sec > 1)
timeout.tv_sec = 1;
else
timeout.tv_usec = (curl_timeo % 1000) * 1000;
}
/* get file descriptors from the transfers */
mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
if(mc != CURLM_OK) {
fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
break;
}
/* On success the value of maxfd is guaranteed to be >= -1. We call
select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
to sleep 100ms, which is the minimum suggested value in the
curl_multi_fdset() doc. */
if(maxfd == -1) {
#ifdef _WIN32
Sleep(100);
rc = 0;
#else
/* Portable sleep for platforms other than Windows. */
struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
rc = select(0, NULL, NULL, NULL, &wait);
#endif
}
else {
/* Note that on some platforms 'timeout' may be modified by select().
If you need access to the original value save a copy beforehand. */
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
}
switch(rc) {
case -1:
/* select error */
break;
case 0:
default:
/* timeout or readable/writable sockets */
curl_multi_perform(multi_handle, &still_running);
break;
}
}
curl_multi_cleanup(multi_handle);
for(i = 0; i<num_transfers; i++) {
curl_multi_remove_handle(multi_handle, trans[i].hnd);
curl_easy_cleanup(trans[i].hnd);
}
return 0;
}
示例3: test
//.........这里部分代码省略.........
upload = fopen(libtest_arg3, "rb");
if(!upload) {
error = ERRNO;
fprintf(stderr, "fopen() failed with error: %d (%s)\n",
error, strerror(error));
fprintf(stderr, "Error opening file: (%s)\n", libtest_arg3);
return TEST_ERR_FOPEN;
}
res_global_init(CURL_GLOBAL_ALL);
if(res) {
fclose(upload);
return res;
}
easy_init(easy);
/* go verbose */
easy_setopt(easy, CURLOPT_VERBOSE, 1L);
/* specify target */
easy_setopt(easy, CURLOPT_URL, URL);
/* enable uploading */
easy_setopt(easy, CURLOPT_UPLOAD, 1L);
/* data pointer for the file read function */
easy_setopt(easy, CURLOPT_READDATA, upload);
/* use active mode FTP */
easy_setopt(easy, CURLOPT_FTPPORT, "-");
/* server connection timeout */
easy_setopt(easy, CURLOPT_ACCEPTTIMEOUT_MS,
strtol(libtest_arg2, NULL, 10)*1000);
multi_init(multi);
multi_add_handle(multi, easy);
for(;;) {
struct timeval interval;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
long timeout = -99;
int maxfd = -99;
multi_perform(multi, &running);
abort_on_test_timeout();
if(!running)
break; /* done */
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
/* At this point, maxfd is guaranteed to be greater or equal than -1. */
multi_timeout(multi, &timeout);
/* At this point, timeout is guaranteed to be greater or equal than -1. */
if(timeout != -1L) {
int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
interval.tv_sec = itimeout/1000;
interval.tv_usec = (itimeout%1000)*1000;
}
else {
interval.tv_sec = 0;
interval.tv_usec = 100000L; /* 100 ms */
}
select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &interval);
abort_on_test_timeout();
}
msg = curl_multi_info_read(multi, &msgs_left);
if(msg)
res = msg->data.result;
test_cleanup:
/* undocumented cleanup sequence - type UA */
curl_multi_cleanup(multi);
curl_easy_cleanup(easy);
curl_global_cleanup();
/* close the local file */
fclose(upload);
return res;
}
示例4: cetcd_multi_watch
int cetcd_multi_watch(cetcd_client *cli) {
int i, count;
int maxfd, left, added;
long timeout;
long backoff, backoff_max;
fd_set r, w, e;
cetcd_array *watchers;
cetcd_watcher *watcher;
CURLM *mcurl;
struct timeval tv;
mcurl = curl_multi_init();
watchers = &cli->watchers;
count = cetcd_array_size(watchers);
for (i = 0; i < count; ++i) {
watcher = cetcd_array_get(watchers, i);
curl_easy_setopt(watcher->curl, CURLOPT_PRIVATE, watcher);
curl_multi_add_handle(mcurl, watcher->curl);
}
backoff = 100; /*100ms*/
backoff_max = 1000; /*1 sec*/
for(;;) {
curl_multi_perform(mcurl, &left);
if (left) {
FD_ZERO(&r);
FD_ZERO(&w);
FD_ZERO(&e);
curl_multi_fdset(mcurl, &r, &w, &e, &maxfd);
curl_multi_timeout(mcurl, &timeout);
if (timeout == -1) {
timeout = 100; /*wait for 0.1 seconds*/
}
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
/*TODO handle errors*/
select(maxfd+1, &r, &w, &e, &tv);
}
added = cetcd_reap_watchers(cli, mcurl);
if (added == 0 && left == 0) {
/* It will call curl_multi_perform immediately if:
* 1. left is 0
* 2. a new attempt should be issued
* It is expected to sleep a mount time between attempts.
* So we fix this by increasing added counter only
* when a new request should be issued.
* When added is 0, maybe there are retring requests or nothing.
* Either situations should wait before issuing the request.
* */
if (backoff < backoff_max) {
backoff = 2 * backoff;
} else {
backoff = backoff_max;
}
tv.tv_sec = backoff/1000;
tv.tv_usec = (backoff%1000) * 1000;
select(1, 0, 0, 0, &tv);
}
}
curl_multi_cleanup(mcurl);
return count;
}
示例5: main
/*
* Simply download two HTTP files!
*/
int main(void)
{
CURL *http_handle;
CURL *http_handle2;
CURLM *multi_handle;
int still_running; /* keep number of running handles */
http_handle = curl_easy_init();
http_handle2 = curl_easy_init();
/* set options */
curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
/* set options */
curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
/* init a multi stack */
multi_handle = curl_multi_init();
/* add the individual transfers */
curl_multi_add_handle(multi_handle, http_handle);
curl_multi_add_handle(multi_handle, http_handle2);
/* we start some action by calling perform right away */
curl_multi_perform(multi_handle, &still_running);
while(still_running) {
struct timeval timeout;
int rc; /* select() return code */
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
long curl_timeo = -1;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
curl_multi_timeout(multi_handle, &curl_timeo);
if(curl_timeo >= 0) {
timeout.tv_sec = curl_timeo / 1000;
if(timeout.tv_sec > 1)
timeout.tv_sec = 1;
else
timeout.tv_usec = (curl_timeo % 1000) * 1000;
}
/* get file descriptors from the transfers */
curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
/* In a real-world program you OF COURSE check the return code of the
function calls. On success, the value of maxfd is guaranteed to be
greater or equal than -1. We call select(maxfd + 1, ...), specially in
case of (maxfd == -1), we call select(0, ...), which is basically equal
to sleep. */
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
switch(rc) {
case -1:
/* select error */
break;
case 0:
default:
/* timeout or readable/writable sockets */
curl_multi_perform(multi_handle, &still_running);
break;
}
}
curl_multi_cleanup(multi_handle);
curl_easy_cleanup(http_handle);
curl_easy_cleanup(http_handle2);
return 0;
}
示例6: logMsg
//.........这里部分代码省略.........
// TODO: if no internet, then this gets stuck... put a timeout here.
while(fdMax < 0 && fdsetTimeoutCount < 20) {
//TODO: Get a better heuristic on the sleep milliseconds
//sleeps for 100 msec and calls perform and fdset to see if multi perform has started its job
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cres = curl_multi_perform(multiHandle, &numHandles);
prevHandle = numHandles;
curl_multi_fdset(multiHandle, &fdRead, &fdWrite, &fdExcep, &fdMax);
std::cout<<"Here\n"; //TODO: Remove this. Its here to test how many times this loop runs till
//multi_perform starts doing stuff
fdsetTimeoutCount++;
}
if(fdMax < 0) {
logMsg("fdMax set timeout: fdmax still not set by curl_multi_fdset. Internet connection??");
for(auto i = 0; i < urls.size(); i++) {
delete out[i];
}
urls.clear();
return false;
}
//select blocks the thread until the fd set by curl is ready with data.
rc = select(fdMax+1, &fdRead, &fdWrite, &fdExcep, &timeout);
// helper variables to convert extracted data to Json on the spot instead of waiting for all urls to be
// fetched and then converting the extracted data to json
char *url;
char *tmpOutData; //to read the CURLINFO_PRIVATE data which is type casted to char* from stringstream*
std::string tmpJsonData;
int length;
std::shared_ptr<Json::Value> jsonVal(new Json::Value);
Json::Reader jsonReader;
// see what select returned
switch(rc) {
case -1:
//select call ERRORed
break;
case 0:
std::cout<<"Here timeout\n"; //TODO: Remove this. Its here to test how many times select times out.
// So far never with 1 sec of timeout.
//select call Timed out. No fd ready to read anything.
fetchTry++;
if(fetchTry == MAX_FETCH_TRY) {
curl_multi_cleanup(multiHandle);
curl_global_cleanup();
for(auto i = 0; i < urls.size(); i++) {
delete out[i];
}
urls.clear();
return false;
}
break;
default:
// sleep for 5 msec to give enough time for curl to read data for any of the file descriptors.
std::this_thread::sleep_for(std::chrono::milliseconds(5));
std::cout<<"Possible Change\n"; //TODO: Remove this. Its here to test how many times fd is ready and
// will result in a complete data read
//Perform again to see what happened with individual easy handles
curl_multi_perform(multiHandle,&numHandles);
// if easy handle status changed some urls are done.
if(prevHandle != numHandles) {
std::cout<<"Change happened\n";//TODO: Remove this. Only here for testing
prevHandle = numHandles;
handleMsg = curl_multi_info_read(multiHandle, &queuedHandles);
// for every url done fill the jsonValue
for(auto qHandItr = 0; qHandItr <= queuedHandles; qHandItr++) {
if(handleMsg->msg == CURLMSG_DONE) {
//get the url from the easyHandle
curl_easy_getinfo(handleMsg->easy_handle, CURLINFO_EFFECTIVE_URL , &url);
//get the tmpOutData which is holding the extracted info from the url
curl_easy_getinfo(handleMsg->easy_handle, CURLINFO_PRIVATE , &tmpOutData);
// typecast back from char* to std::stringstream
tmpJsonData = ((std::stringstream *)tmpOutData)->str();
length = tmpJsonData.size();
jsonReader.parse(tmpJsonData.c_str(), tmpJsonData.c_str() + length, *(jsonVal.get()));
// no way to get what ID this url was for so have to extract ID from url
m_JsonRoots[extractIDFromUrl(std::string(url))] = jsonVal;
logMsg("R: %d - %s <%s>\n", handleMsg->data.result, curl_easy_strerror(handleMsg->data.result), url);
curl_multi_remove_handle(multiHandle, handleMsg->easy_handle);
curl_easy_cleanup(handleMsg->easy_handle);
}
}
}
break;
}
}while(numHandles);
curl_multi_cleanup(multiHandle);
curl_global_cleanup();
}
for(auto i = 0; i < urls.size(); i++) {
delete out[i];
}
urls.clear();
return true;
}
示例7: tr_webThreadFunc
//.........这里部分代码省略.........
CURLMsg * msg;
CURLMcode mcode;
if( web->close_mode == TR_WEB_CLOSE_NOW )
break;
if( ( web->close_mode == TR_WEB_CLOSE_WHEN_IDLE ) && ( web->tasks == NULL ) )
break;
/* add tasks from the queue */
tr_lockLock( web->taskLock );
while(( task = tr_list_pop_front( &web->tasks )))
{
dbgmsg( "adding task to curl: [%s]", task->url );
curl_multi_add_handle( multi, createEasy( session, web, task ));
/*fprintf( stderr, "adding a task.. taskCount is now %d\n", taskCount );*/
++taskCount;
}
tr_lockUnlock( web->taskLock );
/* maybe wait a little while before calling curl_multi_perform() */
msec = 0;
curl_multi_timeout( multi, &msec );
if( msec < 0 )
msec = THREADFUNC_MAX_SLEEP_MSEC;
if( session->isClosed )
msec = 100; /* on shutdown, call perform() more frequently */
if( msec > 0 )
{
int usec;
int max_fd;
struct timeval t;
fd_set r_fd_set, w_fd_set, c_fd_set;
max_fd = 0;
FD_ZERO( &r_fd_set );
FD_ZERO( &w_fd_set );
FD_ZERO( &c_fd_set );
curl_multi_fdset( multi, &r_fd_set, &w_fd_set, &c_fd_set, &max_fd );
if( msec > THREADFUNC_MAX_SLEEP_MSEC )
msec = THREADFUNC_MAX_SLEEP_MSEC;
usec = msec * 1000;
t.tv_sec = usec / 1000000;
t.tv_usec = usec % 1000000;
tr_select( max_fd+1, &r_fd_set, &w_fd_set, &c_fd_set, &t );
}
/* call curl_multi_perform() */
do {
mcode = curl_multi_perform( multi, &unused );
} while( mcode == CURLM_CALL_MULTI_PERFORM );
/* pump completed tasks from the multi */
while(( msg = curl_multi_info_read( multi, &unused )))
{
if(( msg->msg == CURLMSG_DONE ) && ( msg->easy_handle != NULL ))
{
double total_time;
struct tr_web_task * task;
long req_bytes_sent;
CURL * e = msg->easy_handle;
curl_easy_getinfo( e, CURLINFO_PRIVATE, (void*)&task );
curl_easy_getinfo( e, CURLINFO_RESPONSE_CODE, &task->code );
curl_easy_getinfo( e, CURLINFO_REQUEST_SIZE, &req_bytes_sent );
curl_easy_getinfo( e, CURLINFO_TOTAL_TIME, &total_time );
task->did_connect = task->code>0 || req_bytes_sent>0;
task->did_timeout = !task->code && ( total_time >= task->timeout_secs );
curl_multi_remove_handle( multi, e );
curl_easy_cleanup( e );
/*fprintf( stderr, "removing a completed task.. taskCount is now %d (response code: %d, response len: %d)\n", taskCount, (int)task->code, (int)evbuffer_get_length(task->response) );*/
tr_runInEventThread( task->session, task_finish_func, task );
--taskCount;
}
}
#if 0
{
tr_list * l;
for( l=web->tasks; l!=NULL; l=l->next )
fprintf( stderr, "still pending: %s\n", ((struct tr_web_task*)l->data)->url );
}
fprintf( stderr, "loop is ending... web is closing\n" );
#endif
}
/* Discard any remaining tasks.
* This is rare, but can happen on shutdown with unresponsive trackers. */
while(( task = tr_list_pop_front( &web->tasks ))) {
dbgmsg( "Discarding task \"%s\"", task->url );
task_free( task );
}
/* cleanup */
curl_multi_cleanup( multi );
tr_lockFree( web->taskLock );
tr_free( web->cookie_filename );
tr_free( web );
session->web = NULL;
}
示例8: testExternalPut
static int
testExternalPut ()
{
struct MHD_Daemon *d;
CURL *c;
char buf[2048];
struct CBC cbc;
CURLM *multi;
CURLMcode mret;
fd_set rs;
fd_set ws;
fd_set es;
int max;
int running;
struct CURLMsg *msg;
time_t start;
struct timeval tv;
unsigned int pos = 0;
int done_flag = 0;
multi = NULL;
cbc.buf = buf;
cbc.size = 2048;
cbc.pos = 0;
d = MHD_start_daemon (MHD_USE_DEBUG,
11082,
NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
if (d == NULL)
return 256;
c = curl_easy_init ();
curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11082/hello_world");
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer);
curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
curl_easy_setopt (c, CURLOPT_READDATA, &pos);
curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
/*
// by not giving the file size, we force chunking!
curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
*/
curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
// NOTE: use of CONNECTTIMEOUT without also
// setting NOSIGNAL results in really weird
// crashes on my system!
curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
multi = curl_multi_init ();
if (multi == NULL)
{
curl_easy_cleanup (c);
MHD_stop_daemon (d);
return 512;
}
mret = curl_multi_add_handle (multi, c);
if (mret != CURLM_OK)
{
curl_multi_cleanup (multi);
curl_easy_cleanup (c);
MHD_stop_daemon (d);
return 1024;
}
start = time (NULL);
while ((time (NULL) - start < 5) && (multi != NULL))
{
max = 0;
FD_ZERO (&rs);
FD_ZERO (&ws);
FD_ZERO (&es);
curl_multi_perform (multi, &running);
mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
if (mret != CURLM_OK)
{
curl_multi_remove_handle (multi, c);
curl_multi_cleanup (multi);
curl_easy_cleanup (c);
MHD_stop_daemon (d);
return 2048;
}
if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
{
curl_multi_remove_handle (multi, c);
curl_multi_cleanup (multi);
curl_easy_cleanup (c);
MHD_stop_daemon (d);
return 4096;
}
tv.tv_sec = 0;
tv.tv_usec = 1000;
select (max + 1, &rs, &ws, &es, &tv);
curl_multi_perform (multi, &running);
if (running == 0)
{
msg = curl_multi_info_read (multi, &running);
if (msg == NULL)
break;
if (msg->msg == CURLMSG_DONE)
//.........这里部分代码省略.........
示例9: main
//.........这里部分代码省略.........
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This will retrieve message 1 from the user's mailbox */
curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
/* Tell the multi stack about our easy handle */
curl_multi_add_handle(mcurl, curl);
/* Record the start time which we can use later */
mp_start = tvnow();
/* We start some action by calling perform right away */
curl_multi_perform(mcurl, &still_running);
while(still_running) {
struct timeval timeout;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
int rc;
CURLMcode mc; /* curl_multi_fdset() return code */
long curl_timeo = -1;
/* Initialise the file descriptors */
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* Set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
curl_multi_timeout(mcurl, &curl_timeo);
if(curl_timeo >= 0) {
timeout.tv_sec = curl_timeo / 1000;
if(timeout.tv_sec > 1)
timeout.tv_sec = 1;
else
timeout.tv_usec = (curl_timeo % 1000) * 1000;
}
/* get file descriptors from the transfers */
mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
if(mc != CURLM_OK)
{
fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
break;
}
/* On success the value of maxfd is guaranteed to be >= -1. We call
select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
to sleep 100ms, which is the minimum suggested value in the
curl_multi_fdset() doc. */
if(maxfd == -1) {
#ifdef _WIN32
Sleep(100);
rc = 0;
#else
/* Portable sleep for platforms other than Windows. */
struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
rc = select(0, NULL, NULL, NULL, &wait);
#endif
}
else {
/* Note that on some platforms 'timeout' may be modified by select().
If you need access to the original value save a copy beforehand. */
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
}
if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
fprintf(stderr,
"ABORTING: Since it seems that we would have run forever.\n");
break;
}
switch(rc) {
case -1: /* select error */
break;
case 0: /* timeout */
default: /* action */
curl_multi_perform(mcurl, &still_running);
break;
}
}
/* Always cleanup */
curl_multi_remove_handle(mcurl, curl);
curl_multi_cleanup(mcurl);
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
示例10: main
int main(void)
{
CURL *curl;
CURLM *mcurl;
int still_running = 1;
struct timeval mp_start;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(!curl)
return 1;
mcurl = curl_multi_init();
if(!mcurl)
return 2;
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* This will fetch message 1 from the user's inbox */
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX/;UID=1");
/* Tell the multi stack about our easy handle */
curl_multi_add_handle(mcurl, curl);
/* Record the start time which we can use later */
mp_start = tvnow();
/* We start some action by calling perform right away */
curl_multi_perform(mcurl, &still_running);
while(still_running) {
struct timeval timeout;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
int rc;
long curl_timeo = -1;
/* Initialise the file descriptors */
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* Set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
curl_multi_timeout(mcurl, &curl_timeo);
if(curl_timeo >= 0) {
timeout.tv_sec = curl_timeo / 1000;
if(timeout.tv_sec > 1)
timeout.tv_sec = 1;
else
timeout.tv_usec = (curl_timeo % 1000) * 1000;
}
/* Get file descriptors from the transfers */
curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
/* In a real-world program you OF COURSE check the return code of the
function calls. On success, the value of maxfd is guaranteed to be
greater or equal than -1. We call select(maxfd + 1, ...), specially in
case of (maxfd == -1), we call select(0, ...), which is basically equal
to sleep. */
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
fprintf(stderr,
"ABORTING: Since it seems that we would have run forever.\n");
break;
}
switch(rc) {
case -1: /* select error */
break;
case 0: /* timeout */
default: /* action */
curl_multi_perform(mcurl, &still_running);
break;
}
}
/* Always cleanup */
curl_multi_remove_handle(mcurl, curl);
curl_multi_cleanup(mcurl);
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
示例11: test
int test(char *URL)
{
CURL *easy = NULL;
CURLM *multi = NULL;
int res = 0;
int running;
int msgs_left;
int phase;
CURLMsg *msg;
start_test_timing();
res_global_init(CURL_GLOBAL_ALL);
if(res) {
return res;
}
easy_init(easy);
multi_init(multi);
for (phase = CONNECT_ONLY_PHASE; phase < LAST_PHASE; ++phase) {
/* go verbose */
easy_setopt(easy, CURLOPT_VERBOSE, 1L);
/* specify target */
easy_setopt(easy, CURLOPT_URL, URL);
/* enable 'CONNECT_ONLY' option when in connect phase */
if (phase == CONNECT_ONLY_PHASE)
easy_setopt(easy, CURLOPT_CONNECT_ONLY, 1L);
/* enable 'NOBODY' option to send 'QUIT' command in quit phase */
if (phase == QUIT_PHASE) {
easy_setopt(easy, CURLOPT_CONNECT_ONLY, 0L);
easy_setopt(easy, CURLOPT_NOBODY, 1L);
easy_setopt(easy, CURLOPT_FORBID_REUSE, 1L);
}
multi_add_handle(multi, easy);
for(;;) {
struct timeval interval;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
long timeout = -99;
int maxfd = -99;
multi_perform(multi, &running);
abort_on_test_timeout();
if(!running)
break; /* done */
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
/* At this point, maxfd is guaranteed to be greater or equal than -1. */
multi_timeout(multi, &timeout);
/* At this point, timeout is guaranteed to be greater or equal than -1. */
if(timeout != -1L) {
int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
interval.tv_sec = itimeout/1000;
interval.tv_usec = (itimeout%1000)*1000;
}
else {
interval.tv_sec = TEST_HANG_TIMEOUT/1000+1;
interval.tv_usec = 0;
}
select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &interval);
abort_on_test_timeout();
}
msg = curl_multi_info_read(multi, &msgs_left);
if(msg)
res = msg->data.result;
multi_remove_handle(multi, easy);
}
test_cleanup:
/* undocumented cleanup sequence - type UA */
curl_multi_cleanup(multi);
curl_easy_cleanup(easy);
curl_global_cleanup();
return res;
}
示例12: curl_easy_init
std::set<std::string> YoutubeCrawler::getVideoFile(std::map<std::string, std::string> &result){
std::set<std::string> unFinishVids;
if(result.size() == 0){
return unFinishVids;
}
CURL *handle = curl_easy_init();
if(handle == NULL){
return unFinishVids;
}
CURLM *mhandle = curl_multi_init();
if(mhandle == NULL){
curl_easy_cleanup(handle);
return unFinishVids;
}
CURLcode code;
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writeFileCallback);
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(handle, CURLOPT_MAXREDIRS, 10);
curl_easy_setopt(handle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(handle, CURLOPT_LOW_SPEED_LIMIT, 1);
curl_easy_setopt(handle, CURLOPT_LOW_SPEED_TIME, 60);
std::map<std::string, CURL*> handleList;
std::map<std::string, RecInfo> addrsList;
BDB::AddrType tmpAddr;
std::pair<std::map<std::string, RecInfo>::iterator, bool> addrIt;
RecInfo recInfo;
recInfo.ybdb = &ybdb;
for(std::map<std::string, std::string>::iterator it = result.begin(); it != result.end(); it++){
tmpAddr = ybdb.put("", 0);
if(tmpAddr == -1){
continue;
}
recInfo.addr = tmpAddr;
recInfo.size = 0;
addrIt = addrsList.insert(std::pair<std::string, RecInfo>(it->first, recInfo));
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &(addrIt.first->second));
curl_easy_setopt(handle, CURLOPT_URL, it->second.c_str());
handleList.insert(std::pair<std::string, CURL*>(it->first, handle));
curl_multi_add_handle(mhandle, handle);
handle = curl_easy_duphandle(handle);
}
curl_easy_cleanup(handle);
if(handleList.size() == 0){
curl_multi_cleanup(mhandle);
return unFinishVids;
}
int runningHandles = 0;
while(curl_multi_perform(mhandle, &runningHandles) == CURLM_CALL_MULTI_PERFORM){}
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int max_fd = 0;
fd_set read_fd_set;
fd_set write_fd_set;
fd_set exc_fd_set;
FD_ZERO(&read_fd_set);
FD_ZERO(&write_fd_set);
FD_ZERO(&exc_fd_set);
while(runningHandles > 0){
curl_multi_fdset(mhandle, &read_fd_set, &write_fd_set, &exc_fd_set, &max_fd);
curl_multi_timeout(mhandle, &(timeout.tv_usec));
if(timeout.tv_usec > 0){
select(max_fd + 1, &read_fd_set, &write_fd_set, &exc_fd_set, &timeout);
}
while(curl_multi_perform(mhandle, &runningHandles) == CURLM_CALL_MULTI_PERFORM){}
}
for(std::map<std::string, CURL*>::iterator it = handleList.begin(); it != handleList.end(); it++){
curl_multi_remove_handle(mhandle, it->second);
curl_easy_cleanup(it->second);
}
curl_multi_cleanup(mhandle);
std::map<std::string, std::string>::iterator uIt;
for(std::map<std::string, RecInfo>::iterator it = addrsList.begin(); it != addrsList.end(); it++){
if(it->second.size == 0){
unFinishVids.insert(it->first);
ybdb.del(it->second.addr);
}else{
uIt = result.find(it->first);
logRids << std::setw(11) << it->first
<< std::setfill('0') << std::setw(9) << std::hex << it->second.addr
<< std::setfill('0') << std::setw(9) << std::hex << it->second.size
<< getExtensionName(uIt->second) << std::endl;
logRids.flush();
}
}
return unFinishVids;
}
示例13: test
//.........这里部分代码省略.........
multi_init(m);
create_handles();
multi_setopt(m, CURLMOPT_PIPELINING, 1L);
multi_setopt(m, CURLMOPT_MAX_HOST_CONNECTIONS, 2L);
multi_setopt(m, CURLMOPT_MAX_PIPELINE_LENGTH, 3L);
multi_setopt(m, CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, 15000L);
multi_setopt(m, CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, 10000L);
multi_setopt(m, CURLMOPT_PIPELINING_SITE_BL, site_blacklist);
multi_setopt(m, CURLMOPT_PIPELINING_SERVER_BL, server_blacklist);
last_handle_add = tutil_tvnow();
for(;;) {
struct timeval interval;
struct timeval now;
long int msnow, mslast;
fd_set rd, wr, exc;
int maxfd = -99;
long timeout;
interval.tv_sec = 1;
interval.tv_usec = 0;
if(handlenum < num_handles) {
now = tutil_tvnow();
msnow = now.tv_sec * 1000 + now.tv_usec / 1000;
mslast = last_handle_add.tv_sec * 1000 + last_handle_add.tv_usec / 1000;
if(msnow - mslast >= urltime[handlenum] && handlenum < num_handles) {
fprintf(stdout, "Adding handle %d\n", handlenum);
setup_handle(URL, m, handlenum);
last_handle_add = now;
handlenum++;
}
}
curl_multi_perform(m, &running);
abort_on_test_timeout();
/* See how the transfers went */
while ((msg = curl_multi_info_read(m, &msgs_left))) {
if (msg->msg == CURLMSG_DONE) {
int i, found = 0;
/* Find out which handle this message is about */
for (i = 0; i < num_handles; i++) {
found = (msg->easy_handle == handles[i]);
if(found)
break;
}
printf("Handle %d Completed with status %d\n", i, msg->data.result);
curl_multi_remove_handle(m, handles[i]);
}
}
if(handlenum == num_handles && !running) {
break; /* done */
}
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_ZERO(&exc);
curl_multi_fdset(m, &rd, &wr, &exc, &maxfd);
/* At this point, maxfd is guaranteed to be greater or equal than -1. */
curl_multi_timeout(m, &timeout);
if(timeout < 0)
timeout = 1;
interval.tv_sec = timeout / 1000;
interval.tv_usec = (timeout % 1000) * 1000;
interval.tv_sec = 0;
interval.tv_usec = 1000;
select_test(maxfd+1, &rd, &wr, &exc, &interval);
abort_on_test_timeout();
}
test_cleanup:
remove_handles();
/* undocumented cleanup sequence - type UB */
curl_multi_cleanup(m);
curl_global_cleanup();
free_urls();
return res;
}
示例14: main
/*
* Simply download a HTTP file.
*/
int main(int argc, char **argv)
{
CURL *http_handle;
CURLM *multi_handle;
int still_running; /* keep number of running handles */
http_handle = curl_easy_init();
/* set the options (I left out a few, you'll get the point anyway) */
curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
/* init a multi stack */
multi_handle = curl_multi_init();
/* add the individual transfers */
curl_multi_add_handle(multi_handle, http_handle);
/* we start some action by calling perform right away */
while(CURLM_CALL_MULTI_PERFORM ==
curl_multi_perform(multi_handle, &still_running));
while(still_running) {
struct timeval timeout;
int rc; /* select() return code */
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
long curl_timeo = -1;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
curl_multi_timeout(multi_handle, &curl_timeo);
if(curl_timeo >= 0) {
timeout.tv_sec = curl_timeo / 1000;
if(timeout.tv_sec > 1)
timeout.tv_sec = 1;
else
timeout.tv_usec = (curl_timeo % 1000) * 1000;
}
/* get file descriptors from the transfers */
curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
/* In a real-world program you OF COURSE check the return code of the
function calls. On success, the value of maxfd is guaranteed to be
greater or equal than -1. We call select(maxfd + 1, ...), specially in
case of (maxfd == -1), we call select(0, ...), which is basically equal
to sleep. */
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
switch(rc) {
case -1:
/* select error */
still_running = 0;
printf("select() returns error, this is badness\n");
break;
case 0:
default:
/* timeout or readable/writable sockets */
while(CURLM_CALL_MULTI_PERFORM ==
curl_multi_perform(multi_handle, &still_running));
break;
}
}
curl_multi_cleanup(multi_handle);
curl_easy_cleanup(http_handle);
return 0;
}
示例15: test
/*
* Source code in here hugely as reported in bug report 651464 by
* Christopher R. Palmer.
*
* Use multi interface to get document over proxy with bad port number.
* This caused the interface to "hang" in libcurl 7.10.2.
*/
int test(char *URL)
{
CURL *c;
int ret=0;
CURLM *m;
fd_set rd, wr, exc;
CURLMcode res;
char done = FALSE;
int running;
int max_fd;
int rc;
struct timeval ml_start;
struct timeval mp_start;
char ml_timedout = FALSE;
char mp_timedout = FALSE;
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
fprintf(stderr, "curl_global_init() failed\n");
return TEST_ERR_MAJOR_BAD;
}
if ((c = curl_easy_init()) == NULL) {
fprintf(stderr, "curl_easy_init() failed\n");
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
/* the point here being that there must not run anything on the given
proxy port */
curl_easy_setopt(c, CURLOPT_PROXY, arg2);
curl_easy_setopt(c, CURLOPT_URL, URL);
curl_easy_setopt(c, CURLOPT_VERBOSE, 1);
if ((m = curl_multi_init()) == NULL) {
fprintf(stderr, "curl_multi_init() failed\n");
curl_easy_cleanup(c);
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
if ((res = curl_multi_add_handle(m, c)) != CURLM_OK) {
fprintf(stderr, "curl_multi_add_handle() failed, "
"with code %d\n", res);
curl_multi_cleanup(m);
curl_easy_cleanup(c);
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
ml_timedout = FALSE;
ml_start = curlx_tvnow();
while (!done) {
struct timeval interval;
interval.tv_sec = 1;
interval.tv_usec = 0;
if (curlx_tvdiff(curlx_tvnow(), ml_start) >
MAIN_LOOP_HANG_TIMEOUT) {
ml_timedout = TRUE;
break;
}
mp_timedout = FALSE;
mp_start = curlx_tvnow();
fprintf(stderr, "curl_multi_perform()\n");
res = CURLM_CALL_MULTI_PERFORM;
while (res == CURLM_CALL_MULTI_PERFORM) {
res = curl_multi_perform(m, &running);
if (curlx_tvdiff(curlx_tvnow(), mp_start) >
MULTI_PERFORM_HANG_TIMEOUT) {
mp_timedout = TRUE;
break;
}
}
if (mp_timedout)
break;
if(!running) {
/* This is where this code is expected to reach */
int numleft;
CURLMsg *msg = curl_multi_info_read(m, &numleft);
fprintf(stderr, "Expected: not running\n");
if(msg && !numleft)
ret = 100; /* this is where we should be */
else
ret = 99; /* not correct */
break;
}
fprintf(stderr, "running == %d, res == %d\n", running, res);
//.........这里部分代码省略.........