cURL / Mailing Lists / curl-library / Single Mail

curl-library

Questions on using the curl multi interface(som clarification)

From: lali .cpp <lali.cpp_at_gmail.com>
Date: Fri, 3 Sep 2010 13:01:22 +0530

Hi

This post is in addition my earlier post "Questions on using the curl multi
interface"

This is to clarify what I intend to do and how presently I am doing it.

Here's what I wish to do:-

To get data from multiple urls and enforce a timeout *both on connection
establishment and data transfer*, for example enforce a connectioin
establishment timeout of 30ms and data transfer timeout of 75ms.

So here are the steps I follow:-

1) Initialize a multi handle using curl_multi_init

2) Set option i.e CURLMOPT_MAXCONNECTS on curl multi handle using the api
curl_multi_setopt.

3) Initialize curl easy handles using curl_easy_init

4) Set options CURLOPT_URL, CURLOPT_WRITEFUNCTION, CURLOPT_WRITEDATA and
CURLOPT_CONNECTTIMEOUT_MS(30ms) on each of the easy handles using
curl_easy_setopt. CURLOPT_CONNECTTIMEOUT_MS is used to enforce connection
establishment timeout. Is it correct as I would be using multi interface and
not easy interface?

5) Add all the easy handles to the multi handle using curl_multi_add_handle

6) The following code+comments+some pseudo_code(in a function) is then used
for transfer and enforce tranfer timeout:-

int still_running=0;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd;
int rc;
int retval;
struct timeval timeout;

while((retval=curl_multi_perform(multi_handle, &still_running)) ==
CURLM_CALL_MULTI_PERFORM );

if ( retval != CURLM_OK) {
    return some error i.e exit from this function
}
timeout.tv_sec = 0;
timeout.tv_usec = 75000; // I want to enforce a transfer timeout of 75
milli seconds
while(still_running) {

    FD_ZERO(&fdread);
    FD_ZERO(&fdwrite);
    FD_ZERO(&fdexcep);

    /* get file descriptors from the transfers */
    retval = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep,
&maxfd);
    if(retval != CURLM_OK){
        return some error and exit from this function
    }

    if(maxfd == -1){
        break;// which would essentially return from the function
    }
    else{
        rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); // Wait
for 75ms or till fds are ready for some "read" or "write"
        // On Linux, select() modifies timeout to reflect the amount of time
not slept
    }

    switch(rc) {
        case -1:
            /* select error */
            //
            fprintf(stderr,"ERROR select returned error
%s:%d\n",__FILE__,__LINE__);
            still_running = 0;
            break; // and return from function
        case 0:
            //Timeout case, all transfers could not complete in 75ms so
return from function and process the data we have received
            still_running = 0;
            break;

        default:
            /* one or more of curl's file descriptors say there's data to
read
                 or write */
            while((retval=curl_multi_perform(multi_handle, &still_running))
== CURLM_CALL_MULTI_PERFORM );
            if (retval != CURLM_OK) {
                return some error and exit from function
            }
    }
}
return from this function// i.e end of function

7. After that I remove all the easy handles from the multi handle and go to
step 4(this time to fetch data from some different urls)

Kindly let me know where I am wrong or which issues I need to take care of.
As you can see in the above code,* I never called the function
curl_multi_timeout as given in the example c code multi-double.c*

Sorry for the long post and thanks for your patience.

Regards
lali

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-09-03