curl-library
Re: Info request about the zero copy interface (2)
Date: Mon, 05 Dec 2005 11:50:55 +0100
Daniel Stenberg ha scritto:
> On Sun, 4 Dec 2005, Legolas wrote:
>
>> 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'
>
>
> That is not the case.
>
> We know there is data available to read (but we cannot know how much).
> We must already have a known buffer area to store data in, then call
> recv() to get the data and then store it in the buffer (possibly after
> some "decoding" like SSL, chunked HTTP and similar).
>
> Therefore, we must already have been given a buffer pointer (and size)
> from the application layer where we can store the received data BEFORE
> we call the write callback.
>
> Without that, we cannot do zero copy. Unless I'm forgetting something.
>
I understand, I'm sorry but I thought you were using something similar to
ioctlsocket(yoursocket, FIONREAD, &available_data_size);
To determine it, but probably this interface is not available on all
socket layers.
This way the read operation scheme changes a little bit, but I still
suggest to let the application decide about the buffer through the 2
functions below:
/* APPLICATION-DEFINED FUNCTIONS */
void *write_buffer(void *custom_data, int *size); /* get a
valid buffer for write-purposes */
void write_callback(void *custom_data, int received_size); /* let
the application flush the buffer or something else */
...
/* READ OPERATION */
void *buffer; /* application-provviden
buffer pointer */
int buffer_size, /* initially set to the
desired buffer size, it is updated by the application */
received_size, /* return value of recv(....) */
final_size; /* final size of worked out
data */
...
MainLoop:
received_size = recv(yoursocket, internal_buffer,
internal_buffer_size, yourflags);
buffer_size = forecast_size(received_size); /*
forecast someway by libcurl */
buffer = write_buffer(custom_data, &buffer_size); /*
application may return a bigger buffer */
...(decode SSL, join chunks...) /* work
on received data putting final result data into 'buffer' */
write_callback(custom_data, final_size); /*
previous code must set 'final_size' to the size of data written to
'buffer' */
if (there_is_more_data) goto MainLoop; /*
determined someway */
Do you think that this pseudo-code is useful for a zero-copy interface?
Received on 2005-12-05