本文整理汇总了C++中curl_multi_add_handle函数的典型用法代码示例。如果您正苦于以下问题:C++ curl_multi_add_handle函数的具体用法?C++ curl_multi_add_handle怎么用?C++ curl_multi_add_handle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curl_multi_add_handle函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: testExternalGet
static int
testExternalGet ()
{
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;
MHD_socket maxsock;
#ifdef MHD_WINSOCK_SOCKETS
int maxposixs; /* Max socket number unused on W32 */
#else /* MHD_POSIX_SOCKETS */
#define maxposixs maxsock
#endif /* MHD_POSIX_SOCKETS */
int running;
struct CURLMsg *msg;
time_t start;
struct timeval tv;
int i;
MHD_socket fd;
multi = NULL;
cbc.buf = buf;
cbc.size = 2048;
cbc.pos = 0;
d = MHD_start_daemon (MHD_USE_DEBUG,
11080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
if (d == NULL)
return 256;
c = setupCURL(&cbc);
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;
}
for (i = 0; i < 2; i++) {
start = time (NULL);
while ((time (NULL) - start < 5) && (multi != NULL))
{
maxsock = MHD_INVALID_SOCKET;
maxposixs = -1;
FD_ZERO (&rs);
FD_ZERO (&ws);
FD_ZERO (&es);
curl_multi_perform (multi, &running);
mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
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, &maxsock))
{
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 (maxposixs + 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)
{
if (i == 0 && msg->data.result != CURLE_OK)
printf ("%s failed at %s:%d: `%s'\n",
"curl_multi_perform",
__FILE__,
__LINE__, curl_easy_strerror (msg->data.result));
else if (i == 1 && msg->data.result == CURLE_OK)
printf ("%s should have failed at %s:%d\n",
"curl_multi_perform",
__FILE__,
__LINE__);
curl_multi_remove_handle (multi, c);
//.........这里部分代码省略.........
开发者ID:Paxxi,项目名称:libmicrohttpd,代码行数:101,代码来源:test_quiesce.c
示例2: curl_easy_perform
/***************************************************************************
* This function is still only for testing purposes. It makes a great way
* to run the full test suite on the multi interface instead of the easy one.
***************************************************************************
*
* The *new* curl_easy_perform() is the external interface that performs a
* transfer previously setup.
*
* Wrapper-function that: creates a multi handle, adds the easy handle to it,
* runs curl_multi_perform() until the transfer is done, then detaches the
* easy handle, destroys the multi handle and returns the easy handle's return
* code. This will make everything internally use and assume multi interface.
*/
CURLcode curl_easy_perform(CURL *easy)
{
CURLM *multi;
CURLMcode mcode;
CURLcode code = CURLE_OK;
int still_running;
struct timeval timeout;
int rc;
CURLMsg *msg;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd;
if(!easy)
return CURLE_BAD_FUNCTION_ARGUMENT;
multi = curl_multi_init();
if(!multi)
return CURLE_OUT_OF_MEMORY;
mcode = curl_multi_add_handle(multi, easy);
if(mcode) {
curl_multi_cleanup(multi);
if(mcode == CURLM_OUT_OF_MEMORY)
return CURLE_OUT_OF_MEMORY;
else
return CURLE_FAILED_INIT;
}
/* we start some action by calling perform right away */
do {
while(CURLM_CALL_MULTI_PERFORM ==
curl_multi_perform(multi, &still_running));
if(!still_running)
break;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* timeout once per second */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
/* Old deprecated style: get file descriptors from the transfers */
curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
rc = Curl_select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
/* The way is to extract the sockets and wait for them without using
select. This whole alternative version should probably rather use the
curl_multi_socket() approach. */
if(rc == -1)
/* select error */
break;
/* timeout or data to send/receive => loop! */
} while(still_running);
msg = curl_multi_info_read(multi, &rc);
if(msg)
code = msg->data.result;
mcode = curl_multi_remove_handle(multi, easy);
/* what to do if it fails? */
mcode = curl_multi_cleanup(multi);
/* what to do if it fails? */
return code;
}
开发者ID:DISTRHO,项目名称:DISTRHO-Ports,代码行数:87,代码来源:easy.c
示例3: main
/*
* Simply download two HTTP files!
*/
int main(int argc, char **argv)
{
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.haxx.se/");
/* 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 */
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 */
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);
curl_easy_cleanup(http_handle2);
return 0;
}
开发者ID:Krazygamr,项目名称:D-Touch,代码行数:90,代码来源:multi-double.c
示例4: main
int main(int argc, char *argv[])
{
CURL *curl;
CURLM *multi_handle;
int still_running;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";
/* Fill in the file upload field. This makes libcurl load data from
the given file name when curl_easy_perform() is called. */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "sendfile",
CURLFORM_FILE, "postit2.c",
CURLFORM_END);
/* Fill in the filename field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, "postit2.c",
CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);
curl = curl_easy_init();
multi_handle = curl_multi_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl && multi_handle) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL,
"http://www.fillinyoururl.com/upload.cgi");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_multi_add_handle(multi_handle, curl);
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;
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;
/* 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, *and* you make sure that maxfd is bigger than -1
so that the call to select() below makes sense! */
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
switch(rc) {
case -1:
/* select error */
break;
case 0:
printf("timeout!\n");
default:
/* timeout or readable/writable sockets */
printf("perform!\n");
while(CURLM_CALL_MULTI_PERFORM ==
curl_multi_perform(multi_handle, &still_running));
printf("running: %d!\n", still_running);
break;
}
}
curl_multi_cleanup(multi_handle);
/* always cleanup */
//.........这里部分代码省略.........
开发者ID:499940913,项目名称:moon,代码行数:101,代码来源:multi-post.c
示例5: while
// overloaded to additionally move data to/from the webserver
void LLHTTPAssetStorage::checkForTimeouts()
{
CURLMcode mcode;
LLAssetRequest *req;
while ( (req = findNextRequest(mPendingDownloads, mRunningDownloads)) )
{
// Setup this curl download request
// We need to generate a new request here
// since the one in the list could go away
char tmp_url[MAX_STRING]; /*Flawfinder: ignore*/
char uuid_str[UUID_STR_LENGTH]; /*Flawfinder: ignore*/
req->getUUID().toString(uuid_str);
std::string base_url = getBaseURL(req->getUUID(), req->getType());
snprintf(tmp_url, sizeof(tmp_url), "%s/%36s.%s", base_url.c_str() , uuid_str, LLAssetType::lookup(req->getType())); /* Flawfinder: ignore */
LLHTTPAssetRequest *new_req = new LLHTTPAssetRequest(this, req->getUUID(),
req->getType(), RT_DOWNLOAD, tmp_url, mCurlMultiHandle);
new_req->mTmpUUID.generate();
// Sets pending download flag internally
new_req->setupCurlHandle();
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_WRITEFUNCTION, &curlDownCallback);
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_WRITEDATA, new_req->mCurlHandle);
mcode = curl_multi_add_handle(mCurlMultiHandle, new_req->mCurlHandle);
if (mcode > CURLM_OK)
{
// Failure. Deleting the pending request will remove it from the running
// queue, and push it to the end of the pending queue.
new_req->cleanupCurlHandle();
deletePendingRequest(RT_DOWNLOAD, new_req->getType(), new_req->getUUID());
break;
}
else
{
llinfos << "Requesting " << new_req->mURLBuffer << llendl;
}
}
while ( (req = findNextRequest(mPendingUploads, mRunningUploads)) )
{
// setup this curl upload request
bool do_compress = req->getType() == LLAssetType::AT_OBJECT;
char tmp_url[MAX_STRING];/*Flawfinder: ignore*/
char uuid_str[UUID_STR_LENGTH];/*Flawfinder: ignore*/
req->getUUID().toString(uuid_str);
snprintf(tmp_url, sizeof(tmp_url), /* Flawfinder: ignore */
do_compress ? "%s/%s.%s.gz" : "%s/%s.%s",
mBaseURL.c_str(), uuid_str, LLAssetType::lookup(req->getType()));
LLHTTPAssetRequest *new_req = new LLHTTPAssetRequest(this, req->getUUID(),
req->getType(), RT_UPLOAD, tmp_url, mCurlMultiHandle);
if (do_compress)
{
new_req->prepareCompressedUpload();
}
// Sets pending upload flag internally
new_req->setupCurlHandle();
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_UPLOAD, 1);
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_WRITEFUNCTION, &nullOutputCallback);
if (do_compress)
{
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_READFUNCTION,
&LLHTTPAssetRequest::curlCompressedUploadCallback);
}
else
{
LLVFile file(mVFS, req->getUUID(), req->getType());
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_INFILESIZE, file.getSize());
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_READFUNCTION,
&curlUpCallback);
}
curl_easy_setopt(new_req->mCurlHandle, CURLOPT_READDATA, new_req->mCurlHandle);
mcode = curl_multi_add_handle(mCurlMultiHandle, new_req->mCurlHandle);
if (mcode > CURLM_OK)
{
// Failure. Deleting the pending request will remove it from the running
// queue, and push it to the end of the pending queue.
new_req->cleanupCurlHandle();
deletePendingRequest(RT_UPLOAD, new_req->getType(), new_req->getUUID());
break;
}
else
{
llinfos << "Requesting PUT " << new_req->mURLBuffer << llendl;
}
// Pending upload will have been flagged by the request
}
while ( (req = findNextRequest(mPendingLocalUploads, mRunningLocalUploads)) )
{
// setup this curl upload request
LLVFile file(mVFS, req->getUUID(), req->getType());
//.........这里部分代码省略.........
开发者ID:xinyaojiejie,项目名称:Dale,代码行数:101,代码来源:llhttpassetstorage.cpp
示例6: 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/");
/* 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;
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;
/* 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, *and* you make sure that maxfd is bigger than -1 so
that the call to select() below makes sense! */
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;
}
开发者ID:WiseMan787,项目名称:ralink_sdk,代码行数:72,代码来源:multi-single.c
示例7: sdb_execute_multi
/**
* Execute a command using Curl's multi interface
*
* @param sdb the SimpleDB handle
* @param cmd the command name
* @param _params the parameters
* @param next_token the next token (optional)
* @param user_data the user data (optional)
* @param user_data_2 the user data (optional)
* @return the handle to the deferred call, or SDB_MULTI_ERROR on error
*/
sdb_multi sdb_execute_multi(struct SDB* sdb, const char* cmd, struct sdb_params* _params, char* next_token, void* user_data, void* user_data_2)
{
// Prepare the command execution
struct sdb_params* params = sdb_params_alloc(_params->size + 8);
if (SDB_FAILED(sdb_params_add(params, "Action", cmd))) return SDB_MULTI_ERROR;
char timestamp[32]; sdb_timestamp(timestamp);
if (SDB_FAILED(sdb_params_add(params, "Timestamp", timestamp))) return SDB_MULTI_ERROR;
// Add the command parameters
if (SDB_FAILED(sdb_params_add_all(params, _params))) return SDB_MULTI_ERROR;
// Add the next token
if (next_token != NULL) {
if (SDB_FAILED(sdb_params_add(params, "NextToken", next_token))) return SDB_MULTI_ERROR;
}
// Add the required parameters
if (SDB_FAILED(sdb_params_add_required(sdb, params))) return SDB_MULTI_ERROR;
char* post = sdb_post(sdb, params);
long postsize = strlen(post);
sdb_params_free(params);
// Allocate a multi-data structure
struct sdb_multi_data* m = sdb_multi_alloc(sdb);
assert(m);
m->post = post;
strncpy(m->command, cmd, SDB_LEN_COMMAND - 1);
m->command[SDB_LEN_COMMAND - 1] = '\0';
m->params = sdb_params_deep_copy(_params);
m->user_data = user_data;
m->user_data_2 = user_data_2;
m->post_size = postsize;
// Create a Curl handle and defer it
sdb->rec.size = 0;
curl_easy_setopt(m->curl, CURLOPT_URL, AWS_URL);
curl_easy_setopt(m->curl, CURLOPT_WRITEDATA, &m->rec);
curl_easy_setopt(m->curl, CURLOPT_POST, 1L);
curl_easy_setopt(m->curl, CURLOPT_POSTFIELDS, post);
CURLMcode cr = curl_multi_add_handle(sdb->curl_multi, m->curl);
// Handle Curl errors
if (cr != CURLM_OK) {
sdb_multi_free_one(sdb, m);
return SDB_MULTI_ERROR;
}
// Statistics (the size statistics would be updated when the response is received)
sdb->stat.num_commands++;
if (strncmp(cmd, "Put", 3) == 0) sdb->stat.num_puts++;
return m->curl;
}
开发者ID:Grantley,项目名称:libsdb,代码行数:81,代码来源:private.c
示例8: test
int test(char *URL)
{
CURLMcode m;
CURL *handle = NULL, *duphandle;
CURLM *mhandle = NULL;
int res = 0;
int still_running = 0;
if(curl_global_init(CURL_GLOBAL_ALL)) {
fprintf(stderr, "curl_global_init() failed\n");
goto test_cleanup;
}
handle = curl_easy_init();
if(!handle) {
res = CURLE_OUT_OF_MEMORY;
goto test_cleanup;
}
test_setopt(handle, CURLOPT_URL, URL);
test_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
test_setopt(handle, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(handle);
if(res)
goto test_cleanup;
res = curl_easy_perform(handle);
if(res)
goto test_cleanup;
duphandle = curl_easy_duphandle(handle);
if(!duphandle)
goto test_cleanup;
curl_easy_cleanup(handle);
handle = duphandle;
mhandle = curl_multi_init();
if(!mhandle) {
fprintf(stderr, "curl_multi_init() failed\n");
goto test_cleanup;
}
curl_multi_add_handle(mhandle, handle);
curl_multi_perform(mhandle, &still_running);
while(still_running) {
static struct timeval timeout = { /* 100 ms */
0, 100000L
};
int rc;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int max_fdset = -1;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
m = curl_multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &max_fdset);
if(m != CURLM_OK) {
fprintf(stderr, "curl_multi_fdset() error\n");
goto test_cleanup;
}
/* We call select(max_fdset + 1, ...), specially in case of (maxfd == -1),
* we call select(0, ...), which is basically equal to sleep. */
rc = select(max_fdset + 1, &fdread, &fdwrite, &fdexcep, &timeout);
if(rc == -1) {
fprintf(stderr, "select() error\n");
goto test_cleanup;
}
else {
curl_multi_perform(mhandle, &still_running);
}
}
test_cleanup:
if(mhandle)
curl_multi_cleanup(mhandle);
if(handle)
curl_easy_cleanup(handle);
curl_global_cleanup();
return res;
}
开发者ID:dims,项目名称:curl,代码行数:85,代码来源:lib575.c
示例9: curl_multi_add_handle
void ConnectionManager::registerEasyHandle(CURL *easy) const {
curl_multi_add_handle(_multi, easy);
}
开发者ID:AReim1982,项目名称:scummvm,代码行数:3,代码来源:connectionmanager.cpp
示例10: 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;
}
开发者ID:Huang-lin,项目名称:cetcd-test,代码行数:63,代码来源:cetcd.c
示例11: HTTP_DEBUG
bool HTTPClient::Init(void* _ptr) {
if(_ptr) {
is_raise_error_multi_handle = true;
code_multi = *(CURLMcode*)_ptr;
return true;
}
if(is_raise_error_multi_handle)
return true;
//디비에서 URL이랑 매치되는 필드가져오기
sqlite3* _db = NULL;
char* _db_message = NULL;
HTTP_DEBUG((sqlite3_open(HTTPManager::Share()->GetDBPath().c_str(), &_db)),
"sqlite 디비 열기에 실패하였습니다." << opened_file << "\nError Message:" << sqlite3_errmsg(_db) << "\n");
std::string _query = "SELECT * FROM request WHERE url='" + request.url + "' LIMIT 1";
HTTP_DEBUG((sqlite3_exec(_db, _query.c_str(), SQLMatchURLCallback, this, &_db_message)),
"sqllite 디비 검색에 실패하였습니다." << "\nError Message:" << _db_message);
HTTP_DEBUG((sqlite3_close(_db)),
"sqlite 디비 닫기에 실패하였습니다." << "\nError Message:" << sqlite3_errmsg(_db) << "\n");
if(db_id.length()) {
if(db_expires > HTTP::CurrentTime()) {
//저장된것 사용
cache_type = HTTPResponse::CacheType_Expires;
} else if(db_max_age > 0) {
if(last_updated - db_last_updated < db_max_age) {
//저장된것 사용
cache_type = HTTPResponse::CacheType_Expires;
} else {
//연결(이후 Last-Modified로 캐쉬 체크)
cache_type = HTTPResponse::CacheType_Last_Modified;
}
} else {
//연결(이후 Last-Modified로 캐쉬 체크)
cache_type = HTTPResponse::CacheType_Last_Modified;
}
} else {
//처음 다운
cache_type = HTTPResponse::CacheType_None;
}
if(!request.use_cache)
cache_type = HTTPResponse::CacheType_None;
//파일 준비
ReadyFile();
#ifdef ANDROID_NDK
if (is_https)
return true;
#endif
//연결이 필요한경우는 curl생성
if(cache_type != HTTPResponse::CacheType_Expires)
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, request.GetURL().c_str());
ReadyHeader();
ReadyBody();
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HTTPClient::ReadBody);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, ReadHeader);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, this);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, HTTPClient::Progress);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
curl_multi_add_handle(HTTPManager::Share()->GetCURLMulti(), curl);
}
return true;
}
开发者ID:devmario,项目名称:http_cpp,代码行数:80,代码来源:HTTPClient.cpp
示例12: cetcd_reap_watchers
static int cetcd_reap_watchers(cetcd_client *cli, CURLM *mcurl) {
int added, ignore;
CURLMsg *msg;
CURL *curl;
cetcd_string url;
cetcd_watcher *watcher;
cetcd_response *resp;
added = 0;
while ((msg = curl_multi_info_read(mcurl, &ignore)) != NULL) {
if (msg->msg == CURLMSG_DONE) {
curl = msg->easy_handle;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &watcher);
resp = watcher->parser->resp;
if (msg->data.result != CURLE_OK) {
/*try next in round-robin ways*/
/*FIXME There is a race condition if multiple watchers failed*/
if (watcher->attempts) {
cli->picked = (cli->picked+1)%(cetcd_array_size(cli->addresses));
url = cetcd_watcher_build_url(cli, watcher);
curl_easy_setopt(watcher->curl, CURLOPT_URL, url);
sdsfree(url);
curl_multi_remove_handle(mcurl, curl);
curl_multi_add_handle(mcurl, curl);
/*++added;
*watcher->attempts --;
*/
continue;
} else {
resp->err = calloc(1, sizeof(cetcd_error));
resp->err->ecode = error_cluster_failed;
resp->err->message = sdsnew("cetcd_reap_watchers: all cluster servers failed.");
}
}
if (watcher->callback) {
watcher->callback(watcher->userdata, resp);
if (resp->err) {
curl_multi_remove_handle(mcurl, curl);
cetcd_watcher_release(watcher);
break;
}
cetcd_response_release(resp);
watcher->parser->resp = NULL; /*surpress it be freed again by cetcd_watcher_release*/
}
if (!watcher->once) {
sdsclear(watcher->parser->buf);
watcher->parser->st = 0;
watcher->parser->resp = calloc(1, sizeof(cetcd_response));
if (watcher->index) {
watcher->index ++;
url = cetcd_watcher_build_url(cli, watcher);
curl_easy_setopt(watcher->curl, CURLOPT_URL, url);
sdsfree(url);
}
curl_multi_remove_handle(mcurl, curl);
curl_multi_add_handle(mcurl, curl);
++added;
continue;
}
curl_multi_remove_handle(mcurl, curl);
cetcd_watcher_release(watcher);
}
}
return added;
}
开发者ID:Huang-lin,项目名称:cetcd-test,代码行数:65,代码来源:cetcd.c
示例13: curl_easy_init
bool HTTP::Request(const std::string & url, std::ostream & error_output)
{
if (!multihandle)
{
error_output << "HTTP::Request: multihandle initialization failed" << std::endl;
return false;
}
// Each single transfer is built up with an easy handle.
CURL * easyhandle = curl_easy_init();
if (easyhandle)
{
// Setup the appropriate options for the easy handle.
curl_easy_setopt(easyhandle, CURLOPT_URL, url.c_str());
// This function call will make this multi_handle control the specified easy_handle.
// Furthermore, libcurl now initiates the connection associated with the specified easy_handle.
CURLMcode result = curl_multi_add_handle(multihandle, easyhandle);
if (result == CURLM_OK)
{
// Open the destination file for write.
std::string filepart = ExtractFilenameFromUrl(url);
if (filepart.empty())
{
error_output << "HTTP::Request: url \"" << url << "\" is invalid" << std::endl;
curl_multi_remove_handle(multihandle, easyhandle);
return false;
}
std::string filename = folder+"/"+filepart;
FILE * file = fopen(filename.c_str(),"wb");
if (!file)
{
error_output << "HTTP::Request: unable to open \"" << filename << "\" for writing" << std::endl;
curl_multi_remove_handle(multihandle, easyhandle);
return false;
}
// Setup file writing.
curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, file);
// Begin tracking the easyhandle.
REQUEST requestinfo(url,file);
requestinfo.progress_callback_data.http = this;
requestinfo.progress_callback_data.easyhandle = easyhandle;
easyhandles.insert(std::make_pair(easyhandle,requestinfo));
requests.insert(std::make_pair(url,HTTPINFO()));
// Setup the progress callback.
curl_easy_setopt(easyhandle, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(easyhandle, CURLOPT_PROGRESSFUNCTION, ProgressCallback);
curl_easy_setopt(easyhandle, CURLOPT_PROGRESSDATA, &(easyhandles.find(easyhandle)->second.progress_callback_data));
return true;
}
else
{
// Tell the multihandle to forget the handle.
curl_multi_remove_handle(multihandle, easyhandle);
error_output << "HTTP::Request: CURL is unable to request URL \"" << url << "\"" << std::endl;
return false;
}
}
else
{
error_output << "HTTP::Request: easyhandle initialization failed" << std::endl;
return false;
}
}
开发者ID:logzero,项目名称:vdrift,代码行数:73,代码来源:http.cpp
示例14: vdf_threadfunc
//.........这里部分代码省略.........
curl_handle = curl_easy_init();
//curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, curl_cback);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "vdf_stream");
paused = stopped = 1;
curl_multi = curl_multi_init();
// curl_multi_add_handle(curl_multi, curl_handle);
drv = vdf_drive_create(DRIVE_SIZE, DRIVE_FLAGS);
if(drv == NULL) {
perror("vdf_createdrive");
return 1;
}
bps = vdf_drive_sectorsize(drv);
filesize = (vdf_drive_dataclusters(drv) - 16) * vdf_drive_clustersize(drv); /* leave a bit of room */
bufflen = bps * BUFFCNT;
buffer = malloc(bufflen);
if(buffer == NULL) {
perror("malloc");
return 1;
}
bstart = bend = 0;
writeoff = 0;
paused = stopped = 1;
fil = vdf_add_file_virt(vdf_drive_root(drv), TRACK_NAME, filesize, file_cback, NULL, 0);
if(fil == NULL) {
perror("vdf_add_file_virt");
return 1;
}
trans = vdf_transport_open(VTD_NBD_CLI_STR, drv, NULL, argv[1], argv[2]);
if(trans == NULL) {
perror("vdf_transport_open");
return 1;
}
#ifdef _WIN32
if(CreateThread(NULL, 0, curl_threadfunc, NULL, 0, &tid) == NULL) {
perror("CreateThread");
return 1;
}
#else
if(pthread_create(&thd, NULL, curl_threadfunc, NULL) == -1) {
perror("pthread_create");
return 1;
}
#endif
#ifdef _WIN32
if(CreateThread(NULL, 0, vdf_threadfunc, NULL, 0, &tid) == NULL) {
perror("CreateThread");
return 1;
}
#else
if(pthread_create(&thd, NULL, vdf_threadfunc, NULL) == -1) {
perror("pthread_create");
return 1;
}
#endif
while(fgets(line, LINE_MAX, stdin) != NULL) {
if((p = strchr(line, '\r')) != NULL)
*p = 0;
if((p = strchr(line, '\n')) != NULL)
*p = 0;
switch(line[0]) {
case '>':
paused = 1;
if(line[1] != 0) {
if(!stopped)
curl_multi_remove_handle(curl_multi, curl_handle);
stopped = 0;
printf("Open URL: '%s'\n", line + 1);
curl_easy_setopt(curl_handle, CURLOPT_URL, line + 1);
curl_multi_add_handle(curl_multi, curl_handle);
} else {
if(stopped)
continue;
printf("Resume\n");
}
paused = stopped = 0;
break;
case '!':
printf("Stop\n");
stopped = paused = 1;
sleep(200);
bend = bstart = 0;
curl_multi_remove_handle(curl_multi, curl_handle);
break;
case '#':
printf("Pause\n");
paused = 1;
break;
}
}
return 0;
}
开发者ID:doogle,项目名称:vdf,代码行数:101,代码来源:vdf_stream2.c
示例15: wswcurl_perform
int wswcurl_perform()
{
int ret = 0;
wswcurl_req *r, *next;
if (!curlmulti) return 0;
// process requests in FIFO manner
// check for timed out requests and requests that need to be paused
r = http_requests_hnode;
while( r )
{
next = r->prev;
if (r->status == WSTATUS_QUEUED) {
// queued
if (curlmulti_num_handles < WMAXMULTIHANDLES) {
if (curl_multi_add_handle(curlmulti, r->curl)) {
CURLDBG(("OOPS: CURL MULTI ADD HANDLE FAIL!!!"));
}
r->status = WSTATUS_STARTED;
r->last_action = wswcurl_now();
curlmulti_num_handles++;
}
else {
// stay in queue
}
}
// handle pauses for synchronous requests
if( r->status == WSTATUS_STARTED && !r->callback_read ) {
if( r->rxreceived >= r->rxreturned + WMAXBUFFERING ) {
wswcurl_pause( r );
}
}
// handle timeouts
if( r->status == WSTATUS_STARTED ) {
time_t now = wswcurl_now();
if( r->paused ) {
// paused
r->last_action = now;
} else if( r->timeout && ( r->last_action + r->timeout <= now ) ) {
// timed out
r->respcode = -1;
r->status = -CURLE_OPERATION_TIMEDOUT;
if( r->callback_done ) {
r->callback_done( r, r->status, r->customp );
}
}
}
r = next;
}
//CURLDBG(("CURL BEFORE MULTI_PERFORM\n"));
while ( curl_multi_perform(curlmulti, &ret) == CURLM_CALL_MULTI_PERFORM) {
CURLDBG((" CURL MULTI LOOP\n"));
}
ret += wswcurl_checkmsg();
//CURLDBG(("CURL after checkmsg\n"));
return ret;
}
开发者ID:ewirch,项目名称:qfusion,代码行数:65,代码来源:wswcurl.c
示例16: test
int test(char *URL)
{
int res = 0;
CURL *curl;
int running;
char done=FALSE;
CURLM *m = NULL;
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 ((curl = curl_easy_init()) == NULL) {
fprintf(stderr, "curl_easy_init() failed\n");
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
test_setopt(curl, CURLOPT_URL, URL);
test_setopt(curl, CURLOPT_VERBOSE, 1);
test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
test_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
if ((m = curl_multi_init()) == NULL) {
fprintf(stderr, "curl_multi_init() failed\n");
curl_easy_cleanup(curl);
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
if ((res = (int)curl_multi_add_handle(m, curl)) != CURLM_OK) {
fprintf(stderr, "curl_multi_add_handle() failed, "
"with code %d\n", res);
curl_multi_cleanup(m);
curl_easy_cleanup(curl);
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
ml_timedout = FALSE;
ml_start = tutil_tvnow();
fprintf(stderr, "Start at URL 0\n");
while (!done) {
fd_set rd, wr, exc;
int max_fd;
struct timeval interval;
interval.tv_sec = 1;
interval.tv_usec = 0;
if (tutil_tvdiff(tutil_tvnow(), ml_start) >
MAIN_LOOP_HANG_TIMEOUT) {
ml_timedout = TRUE;
break;
}
mp_timedout = FALSE;
mp_start = tutil_tvnow();
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) {
done = TRUE; /* bail out */
break;
}
if (res != CURLM_OK) {
fprintf(stderr, "not okay???\n");
break;
}
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_ZERO(&exc);
max_fd = 0;
if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
fprintf(stderr, "unexpected failured of fdset.\n");
res = 189;
break;
}
if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
fprintf(stderr, "bad select??\n");
res = 195;
break;
}
}
if (ml_timedout || mp_timedout) {
//.........这里部分代码省略.........
开发者ID:dims,项目名称:curl,代码行数:101,代码来源:lib564.c
示例17: handler
static void handler (zsock_t *pipe, void *args) {
curl_global_init(CURL_GLOBAL_ALL);
CURLM *multi = curl_multi_init ();
CURLSH *share = curl_share_init ();
curl_share_setopt (share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt (share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
curl_share_setopt (share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
long verbose = (*(bool *) args) ? 1L : 0L;
long timeout = 30;
CURLMcode code;
SOCKET pipefd = zsock_fd (pipe);
struct curl_waitfd waitfd = {pipefd, CURL_WAIT_POLLIN};
// List to hold pending curl handles, in case we are destroy the client
// while request are inprogress
zlistx_t *pending_handles = zlistx_new ();
zlistx_set_destructor (pending_handles, (zlistx_destructor_fn *) curl_destructor);
zsock_signal (pipe, 0);
bool terminated = false;
while (!terminated) {
int events = zsock_events (pipe);
if ((events & ZMQ_POLLIN) == 0) {
code = curl_multi_wait (multi, &waitfd, 1, 1000, NULL);
assert (code == CURLM_OK);
}
events = zsock_events (pipe);
if (events & ZMQ_POLLIN) {
char* command = zstr_recv (pipe);
if (!command)
break; // Interrupted
// All actors must handle $TERM in this way
if (streq (command, "$TERM"))
terminated = true;
else if (streq (command, "GET")) {
char *url;
zlistx_t *headers;
void *userp;
int rc = zsock_recv (pipe, "slp", &url, &headers, &userp);
assert (rc == 0);
zchunk_t *data =
|
请发表评论