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

[Libevent-users] a bug in libevent IOCP



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.