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

Re: [Libevent-users] a bug in libevent IOCP



On Fri, Nov 14, 2014 at 4:04 AM, jason <xujiezhige@xxxxxxx> wrote:
> These days, I use libevent IOCP for my server programming, but I was trapped
> in a situation where some network package data sent by client first time
> will be lost. The following codes suffers from that bug.
>
>
>
>                    m_pBufferEvent = bufferevent_socket_new( pBase, fSocket,
> BUFFER_EVENT_OPTION );
>
>                    bufferevent_enable( m_pBufferEvent, EV_WRITE | EV_READ );
>
> bufferevent_setcb( m_pBufferEvent, OnRead, NULL, OnEvent, &m_qwSessionID );
>
>
>
> After some days diving into libevent source code and debugging, I find the
> reason. Bufferevent_enable() will invoke user callback if there is a data in
> input buffer, but now user callback hasnât been set. So you have to set user
> callback before you enable buffer read and write. Like this:
>
>
>
> m_pBufferEvent = bufferevent_socket_new( pBase, fSocket, BUFFER_EVENT_OPTION
> );
>
> bufferevent_setcb( m_pBufferEvent, OnRead, NULL, OnEvent, &m_qwSessionID );
>
>                    bufferevent_enable( m_pBufferEvent, EV_WRITE | EV_READ );
>
>
>
> I think bufferevent_setcb() should call user callback when there is some
> data. Or you must add relevant comments to this function to warn developers.

Hm.  The rule in the documentation is that the read callback is
invoked whenever more data arrives (subject to the watermarks).  If
more data arrives before the callback is invoked, that can happen.

So I think this is a documentation clarity issue; I'd be glad to take
documentation patches to make this more clear.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.