[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [Libevent-users] pause bufferevent callback
On Tue, Nov 29, 2011 at 06:58:15AM -0800, Ivan Popovski wrote:
> Hi.
>
> Sorry on newbie question.
>
> Is there way to pause read callback, because i have fixed length buffer?
> and in one moment need to avoid overflow and stop readcb until other
> module consumes data (or other conditions are met)? The time?between
> reading and consuming is not defined.
>
Checkout bufferevent watermarks:
http://www.wangafu.net/~nickm/libevent-book/Ref6_bufferevent.html
> Can i use bufferevent_disable/bufferevent_enable?for this?
> Will EV_READ + libevent_disable() stop reading from socket completely
> or only disable?callback calling?
This will disable the read side of the underlying socket, so yes. But
note that if you still have data remaining on the underlying evbuffer,
your read callback will not be invoked until more data has been read.
> There are also deferred callbacks, but can't find any useful examples on
> their usage.
>
deferred callbacks just mean when an event happens your callback isn't
immediately called, but scheduled until all currently executing events
have returned, and libevent is back into the main loop.
The Libevent book has a high-level description of deferred callbacks
are, but nothing too technical.
Think of it this way, without deferred the following happens:
event_base_loop()
- socket is ready
- bufferevent_read()
- read(sock)
- run_read_callback
- your_read_callback()
- socket is ready
- bufferevent_read()
- read(sock)
- run_read_callback
- your_read_callback()
With deferred
event_base_loop()
- socket is ready
- bufferevent_read()
- read(sock)
- run_read_callback
- schedule_callback()
- socket is ready
- bufferevent_read()
- read(sock)
- run_read_callback
- schedule_callback()
** You now have two callbacks scheduled
- run_deferred
- for each scheduled callback
- your_read_callback()
Basically without deferred all of your callbacks are executed
immediately outside of the event_base_loop(). With deferred, your
callbacks are not executed until all events triggered have been
processed and returned back into event_base_loop().
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.