[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Query regarding bufferevent_read_buffer



On Mon, May 30, 2011 at 5:03 AM, deepak jain <deepakjain111@xxxxxxxxx> wrote:
> Hello,
>  I am a new user of libevent and new to asynchronous I/O , so the question
> may seem quite naive.
> I am building an application based on libevent on linux using epoll backend.
> Sometime clients of my application can write a huge data to my application
> say 100K bytes.
> I am using buffer_read_buffer() api for reading data from clients. In that
> case, would always  only one call of buffer_read_buffer be sufficient
> or buffer_read_buffer() will get call multiple time until i did not get
> whole 100K bytes.

You can read any amount of data from the bufferevent, from one byte up
to all the bytes that are waiting.

The is one catch here: the read callback that you provide for the
bufferevent will be called when new data arrives, and only when new
data arrives.  So if there is 50K waiting on the buffer, and your
callback handles 30K and then returns, Libevent won't invoke your
callback again until some more data is read.  So if you want to handle
all the data as it arrives, a callback like this will work fine:

void readcb(struct bufferevent *bev, void *ctx) {
   char bytes[1024];
   size_t n;
   while ((n = bufferevent_read(bev, bytes, 1024)) {
       process(bytes, n);
   }
}

but a callback like this will potentially leave data around until more
data arrives:

void readcb(struct bufferevent *bev, void *ctx) {
   char bytes[1024];
   size_t n = bufferevent_read(bev, bytes, 1024);
   if (n) process(bytes, n);
}

Also, you might want to look through the interface a little.  If you
need more finegrained control over reading and draining data, have a
look at the evbuffer interface, and use the evbuffer commands on the
input buffer returned by bufferevent_get_input().

Hoping this helps,
-- 
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.