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

Re: [Libevent-users] Closing bufferevents sockets from the server side



Well water marks seem to be the way to go.. But before your reply came I saw that in the library there is an interface 
struct evbuffer_cb_info {
        size_t orig_size;
        size_t n_added;
        size_t n_deleted;
};

typedef void (*evbuffer_cb_func)(struct evbuffer *buffer,
    const struct evbuffer_cb_info *info, void *arg);
described http://www.wangafu.net/~nickm/libevent-book/Ref7_evbuffer.html

I think this is the right way to do this stuff... 

On Wed, Nov 24, 2010 at 6:23 AM, Kelly Brock <Kerby@xxxxxxxxxxx> wrote:
Hi Sid,

> Sorry that got posted by mistake in the wrong thread... here it is
> again...
>
>
>       Hi,
>       I am using libevent 2.03 alpha 1 to create an app
> <https://gist.github.com/712976> . The app structure is as follows:
>
>       A C++ app wraps lib event to listen on a domain socket. I use
> buffered events. As you can see in the post, the post above is a simple
> server that loops over a set of docs and creates many connections to this
> server.
>
>       I am new to this so please spare my flaky concepts, I want to
> clarify my doubt
>       1) I am using a buffered event that uses sockets as its underlying
> transport means. Everytime a connection is received a connection handler
> forms an eventbuffer, attaches a write handler and a eventcb handler.
>
>       2) I recieve some data from the client, I read a line process it and
> write it onto the output buffer of the bufferevent object.
>
>       The problem: Now I want to just flush this buffer and close the
> underlying transport(socket) after data has been flushed. I will call the
> evbuffer_add only once for each line recieved and after that no more data
> will be written to that specific o/p buffer.
>       So at this point i know exactly how much data to write. I need to
> reliably close this connection after all these bytes are out. How to
> achieve this?

       I just went through this a couple weeks ago myself.  So the trick
here is using bufferevent_setwatermark correctly.  After you add the last of
the data to the outgoing evbuffer you want to call the
bufferevent_setwatermark with 1 and 0 on the write direction.  In this way,
the next time the write callback is hit you know all the data has been
written and calling shutdown is safe.  I typically change the watermarks and
the callback at the same time and it has worked like a charm so far.

       If that does not make complete sense, I'm going to be coding up a
quick example for Nick to post with a little howto I wrote up which should
demonstrate this fairly clearly.

KB

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