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

[Libevent-users] Some questions. 1- Releasing bufferevents. 2 - Timeouts on bufferevents.



Hi All,

I'm using the latest stable version (2.0.21).
I've been reading the whole libevent book (Great book!).

I have some questions which I could not get straight/clear answers for from the book (or perhaps I just didn't fully understand).

1. When using bufferevents I pass a class/struct as a "ctx/opaque"  for each bufferevent.
From the book:
"Bufferevents are internally reference-counted, so if the bufferevent has pending deferred callbacks when you free it, it won’t be deleted until the callbacks are done."

In my code I do the following:
bufferevent_free(bev);
delete ctx;

What worries my is the following scenario:
I call bufferevent_free and release the ctx as well. But since the bufferevent has some pending events a callback will be called and the ctx will be invalid (since it was already released when I invoked the delete command).

I was considering doing the following
bufferevent_setcb(bev, NULL, NULL, NULL, NULL);
bufferevent_free(bev);
delete ctx;

Will it work?

Please note that I'm not sure I even have an issue here. I do not use the option "BEV_OPT_DEFER_CALLBACKS" so perhaps the scenario I described is impossible.

2. So I know timeout mechanism is a bit broken in 2.0.21. If I set a timeout it cant be disabled, but with a bit of work (as I've done with the SSL shutdown and bufferevent_ssl problem) it can be resolved. 

A) On which bufferevent types is it broken?

In my program I need to change the bufferevent timeout values numerous times.

e.g.
At first I want to the timeout to be set to 10 seconds.
After some events I want it to be set 60 seconds.
After some more events I want it to be set 20 seconds.

B) Can it be done?

e.g.
bufferevent_set_timeouts(bev, {10, 0 }, {10 ,0})
.
.
.
bufferevent_set_timeouts(bev, {60, 0 }, {60 ,0})
.
.
.
bufferevent_set_timeouts(bev, {20, 0 }, {20 ,0})

C) What happens if I do the following:
bufferevent_set_timeouts(bev, NULL, {20, 0})
Is it valid? The timeout will only be set for write?

D) From the book:
"When a read or write timeout occurs, the corresponding read or write operation becomes disabled on the bufferevent. The event callback is then invoked with either BEV_EVENT_TIMEOUT|BEV_EVENT_READING or BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING."

Is it ok to re-enable the buffevent. 
E.g.  EV_EVENT_TIMEOUT|BEV_EVENT_READING event was invoked.
So I execute:
 bufferevent_enable(bev, EV_READ) It is okay?

Please note that my questions apply for both bufferevent and bufferevent_ssl.


Sorry for all these questions... I just don't want to do mistakes/bad design decisions without fully understanding how libevent works.

Thanks in advance,
Tomer.