cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Asynchronous

From: Ingo Krabbe <ikrabbe.ask_at_web.de>
Date: Mon, 10 Mar 2008 12:11:36 +0100

Am Montag, 10. März 2008 10:53:31 schrieb Ramprakash Jelari thinakaran:
> Hi Ingo,
>
> Thanks for the information. Two cases i am considering here.
>
> 1. We can send as a chunk wise data.
> 2. Non chunk wise where 'size' of the data to be provided.
>
> Let's say i have a data with its size. In that case also, is there any way
> of
> posting the data with single http post. ?
>
> the present implementation what i have done does follow the following
> logic.
>
> SendTo(data)
> {
> 1. set http data to be posted
> 2. set the size of the data to be posted!
> 3. do curl_easy_perform ()
>
> }
>
> client
> --------
>
> SendTo(beginning data)
> for(Multiple Times)
> sendTo(data);
> sendTo(End data);
>
>
>
> The only thing which i don't want to repeat is the (3) step[ ie multiple
> times posting through curl_easy_perform ] . That means, Just get a
> connection from handler, server is now listening to the port continuiously,
> Start doing the post, and keep on sending the data, and send the data from
> which server knows the end of the transfer.

You should try to set a CURLOPT_READFUNCTION (per easy_setopt) and send the
data through the registered READ callback. This read callback will
automatically get called after (or better from somewhere within)
easy_perform.

size_t read_callback( void *ptr, size_t size, size_t nmemb, void *stream)
{
        /* fill the ptr with a maximum of size*nmemb bytes and return the
         * actual number of bytes. */
}

When you are ready with filling in data you can return 0 from the read
callback, but you should take care that the total sent size matches the
previously set size, for the sanity of the webserver, which may hang
otherwise. To control the data chunks you can register a
CURLOPT_READDATA record, that carries your current state. For example:

struct read_state { unsigned content_length; unsigned pos; };
struct read_state *rs_p;

curl_easy_setopt(easy_handle, CURLOPT_READDATA, rs_p );

This registered data will be passed to the read callback in the "stream"
parameter. So in your read_callback you can read and modify the state.

> Any way of implemention this logic with existing facilities in libcurl?

When you are doing multiple chunked requests you can also reuse the easy
handle by not calling easy_cleanup but easy_reset before filling the options
for the next request step. This will utilize a keep alive connection, if the
server provides this.
Received on 2008-03-10