I've just started working with libevent as of v1.3b, and it appears to still have a bug with high watermarks for bufferevents. Chris Maxwell discovered and mentioned this on the list just over a year ago:

http://monkeymail.org/archives/libevent-users/2006-February/ 000101.html

His solution had not been implemented in any subsequent releases of libevent, and the bug remains.

Although his solution (removing the return in the high-watermark check) does work in preventing lost callbacks, I don't think it is the best solution (something he also wondered). The callback can still be called with more data buffered than the highwater mark. Exactly how much more depends on the arbitrary decisions made in evbuffer_read.

The simple change I made to fix the problem was changing:
    if (bufev->wm_read.high != 0)
        howmuch = bufev->wm_read.high;
to:
    if (bufev->wm_read.high != 0)
        howmuch = bufev->wm_read.high - EVBUFFER_LENGTH(bufev->input);

The original problem was that it was using the high watermark as the max *additional* bytes to read, instead of the max *total* bytes post- read.

It might depend on your idea of what the high watermark behaviour should be (it's not really documented), but to me this makes more sense as the caller decides how much buffering should be done.

While the above change works for my needs, it's not complete either, as there are situations where howmuch will go zero or negative. (eg. Not draining the entire buffer in the read-callback). I haven't dug deeper to figure out the best thing to do in this case, but the socket read should be skipped since there's already more buffered than the high watermark. The next read-callback would be with EVBUFFER_LENGTH(bufev->input) > bufev->wm_read.high, but if you're not completely draining the buffer in the callback, then you should probably expect that.

Comments?

Scott.
--
Scott Kevill
GameRanger Technologies
http://www.GameRanger.com
multiplayer online gaming services

_______________________________________________
Libevent-users mailing list
[email protected]
http://monkey.org/mailman/listinfo/libevent-users

Reply via email to