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

[Libevent-users] Re: libevent & evbuffer_add_reference



On Sat, Feb 27, 2010 at 7:48 AM, Roman Puls <puls@xxxxxxxxxxxx> wrote:
> Hi Nick,
>
> a short one about events: I was assuming that
>
>     evbuffer_add_reference(evb, tx.buffer.ptr, tx.buffer.slen, onsent, ref);
>
> would call the "onsent" callback asynchroneously (having a bufferevent with
> the BEV_OPT_DEFER_CALLBACKS option set on instantiation).
>
> Apparently, this is not right, as the onsent-callback appears instantly (as
> long as the bev is not on its high watermark).
>
> My idea was to use that kind of callback to fetch the next piece of data to
> send, but in the current implementation, this leads to a recursion (on the
> stack).
>
> So my question: is that intended behaviour? If yes, should I queue an event
> (e.g. timer with timeval = { 0, 0 }) to unwind the stack before actually
> calling my onsent handler?

The "cleanupfn" callback is meant for resource management, so you can
free the chunk of data (or remove a reference to it, or whatever) once
it's no longer needed by the evbuffer.

The usual way to do what you're proposing is to have the bufferevent's
write callback add more chunks of data as needed.  This avoids the
recursion problem you're reporting, and has the advantage that it
works with all data, not just data that you've added to the evbuffer
by reference.

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