cURL / Mailing Lists / curl-library / Single Mail

curl-library

RE: Using libcurl for async operations: Cannot use callback write function

From: Shaul Eizikovich <Shaul_at_checkpoint.com>
Date: Wed, 18 Aug 2004 14:15:53 +0200

 

> -----Original Message-----
> From: curl-library-bounces_at_cool.haxx.se
> [mailto:curl-library-bounces_at_cool.haxx.se] On Behalf Of
> Daniel Stenberg
> Sent: Wednesday, August 18, 2004 12:50 PM
> To: libcurl development
> Subject: Re: Using libcurl for async operations: Cannot use
> callback write function
>
> On Wed, 18 Aug 2004, Shaul Eizikovich wrote:
>
> > 2. I encapsulate this function with a multi interface. This fails,
> > since the callback function is never called.
>
> Can you show us a simple application that repeats this behavior?

I put the culprit function (http_client_get_async() ) at the end of this
message. I expected this function to call function WriteMemoryCallback() as
in the 'easy' case, but it did not.

>
> > Also, all the examples do not use CURLOPT_WRITEFUNCTION
>
> That's just because they are very simple examples. I'll
> welcome new contributions with better examples.

Once this one works, I promise to send it over.

>
> However, when libcurl has no CURLOPT_WRITEFUNCTION set, it
> calls its own internal callback instead, so the functionality
> is the same.

So I thought. However, I'd like to force it use my own function, because I
want to detect the last transfer (how?) and then send a message to the
calling application. Remember, I'm writing an API library.

>
> > and always use 'select()'. Is it essential to use it? why?
>
> It isn't essential, but it is stupid not to. Because if you
> don't, you'll be busy-looping a whole lot when there's no
> data to transfer - without any additional benefit.

Well, I want to return ASAP from this function, letting the transfer work in
the background. I probably miss something.

>
> --
> Daniel Stenberg -- http://curl.haxx.se -- http://daniel.haxx.se
> Dedicated custom curl help for hire: http://haxx.se/curl.html
>

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
http_client_get_async(HttpClient client,const char *url,const char
*user_name,const char *password)
{
    char * cur_url = NULL;/* NOTE: libcurl demands that this string is
available until no longer used */
        CURLcode curlRet;
        CURL* curlEZHandle;
        CURLM *multi_handle;
        int still_running; /* keep number of running handles */
        int err_code;

        curlEZHandle = curl_easy_init();
        multi_handle = curl_multi_init();

          cur_url = strdup(url);
        curlRet = curl_easy_setopt(curlEZHandle, CURLOPT_URL, cur_url);

     /* pass memory holder struct to the callback function */
    free_memory_struct(client->reply);
    client->reply = (MemoryStruct) calloc(1, sizeof(struct _MemoryStruct));
        client->reply->data = NULL; /* we expect realloc(NULL, size) to work
*/
        client->reply->size = 0; /* no data at this point */

        /* Write callback stuff */
    curlRet = curl_easy_setopt(curlEZHandle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);
        curlRet = curl_easy_setopt(curlEZHandle, CURLOPT_WRITEDATA, (void
*)client);
 
    /* Apply all options set with http_client_set_opts */
    curlRet = http_client_apply_options(client);
    
        /* Creating a multi and executing the transfer */
        curl_multi_add_handle(multi_handle, curlEZHandle);
        do
        {
                err_code = curl_multi_perform(multi_handle, &still_running);
        } while(CURLM_CALL_MULTI_PERFORM == err_code);

        return HTCERR_OK;
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Received on 2004-08-18