cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Info request about the zero copy interface (2)

From: Legolas <legolas558_at_email.it>
Date: Sun, 04 Dec 2005 14:16:40 +0100

Daniel Stenberg ha scritto:

> On Thu, 1 Dec 2005, Legolas wrote:
>
>> A file-type object has DIFFERENT read and write callbacks from those
>> owned by a memory-type object. The object I was talking about in the
>> previous replies SETS them.
>
>
> Can you please write up a tiny client in a pseudo code using this new
> suggested API of yours? I still don't get how you get zero copy out of
> what you've explained.
>
I am very rusty in socket programming, I suppose you have the following
scenario when handling incoming data from the socket layer:

    - you have knowledge of a certain amount of pending data available
to be read through a
        function like 'recv'

    - libcurl currently handles an internal buffer to copy this data
into and caches several
        pending chunks into it to improve performance, calling the
associated write function
         callback on the internal buffer (but I am probably wrong about
this since such a
         scheme perhaps should not be the best)

Please correct my suppositions.

The basics of my previous explanations are these:

    - receive data into a temporary buffer if the client does not need to
        order chunks into a stream before using them (e.g. it may write
        them to file or do something else)

    - receive data into an assigned buffer if the client needs an ordered
       stream of data

Both cases can be handled by libcurl simply calling a write callback of
the following type:

void *write_buffer(void *custom_data, int desired_size);

Which returns the buffer pointer if *it is asserted* that the buffer is
_at least_ of the desired size, else NULL if something goes wrong
(CURLE_WRITE_ERROR).

My idea is to have the application handling the memory reallocation and
I was presenting you a possible object which assigns its read/write
methods and that automatically reallocates its buffers.

(2)

Another function is of course needed:

void write_callback(void *custom_data, int received_size);

'custom_data' is a parameter specified by the application through
curl_easy_setopt(...) and 'received_size' is the size of data written
to the provviden buffer (obtained calling 'write_buffer')

Read operation scheme:

        1) call to 'write_buffer' to get the receive buffer
        2) ... (receive data)
        3) call to 'write_callback' to let the application work
           on the received data
        4) loop to step (1) till there is available data

This scheme preserves the chance for the application to reuse
a temporary buffer for file writing operations and allows it
to reallocate a dynamic one if needed.

Other readers' opinions are also welcome.

--
    Giuseppe
Received on 2005-12-04