Bufferevent is a little different from raw Event. Every bufferevent created
via function bufferevent_socket_new() has been assigned EV_READ| EV_PERSIST
and EV_WRITE | EV_PERSIST flags.

struct bufferevent * bufferevent_socket_new(......){
..............
event_assign(&bufev->ev_read, bufev->ev_base,fd, EV_READ | EV_PERSIST,
bufferevent_readcb, bufev);
event_assign(&bufev->ev_write, bufev->ev_base,fd,EV_WRITE| EV_PERSIST,
bufferevent_writecb, bufev);
............
}

so my write callback will be called by function 'bufferevent_writecb' under
some conditions.  I need know this 'conditions'.

Whatever, I really thank for you help!


2013/7/5 叶雨飞 <[email protected]>

> Event only triggers once unless you specify EV_PERSIST.  if you want
> them to repeatly trigger you can add EV_READ| EV_WRITE|EV_PERSIST
>
> but,  that's usually a bad idea, since you should only care about
> write event when server actually have something to write to the
> socket.
>
> Here's what I do,
>
> I have two event, one is EV_READ | EV_PERSIST  , the other is
> EV_WRITE, and at the end of generating a response, call event_add()
> to add the write event, during the socket writing, if you didn't
> finish the write, remember to call event_add again.
>
>
> On Thu, Jul 4, 2013 at 7:51 PM, 陈邦义 <[email protected]> wrote:
> > Hi, I'm building a server with libevent2.0.21. I create a new bufferevent
> > for every accepted connection like below:
> >
> > struct event_base* conn_evbase = event_base_new();
> >   if (NULL == conn_evbase){
> >     _error("can not create event base for client!");
> >     close_and_free_connection(client);
> >   }
> >   client->evbase = conn_evbase;
> >
> >   struct bufferevent *bev =
> > bufferevent_socket_new(client->evbase,fd,BEV_OPT_CLOSE_ON_FREE);
> >   if (!bev){
> >     _error("can not create bufferevent!");
> >     close_and_free_connection(client);
> >     return NULL;
> >   }
> >
> >
> bufferevent_setcb(bev,asp_connection_read,asp_connection_write,asp_connection_event,client);
> >   bufferevent_enable(bev,EV_READ | EV_WRITE);
> >
> >   client->buf_ev = bev;
> >
> > I attached read/write handler on this event and let EV_READ/EV_WRITE
> > enabled.
> >
> > I start this server, and use a python script client to connect it. After
> > connected, the client send a request to server and wait for response, the
> > log on server console like the below:
> >
> > .. start connection from 127.0.0.1
> > .. write back to client (means write handler was called)
> > .. writeback ended (means reached the last line of write handler method)
> > .. read request from client
> > .. appended a new response (saved in a queue)
> > .. read request from client
> > .. appended a new response (saved in a queue)
> >
> > What I want is write handler will be trigged repeatedly because there's
> no
> > data on underlying write buffer (via ss command).
> >
> > As you see, when a client was connected, the write handler was trigged
> but
> > only this time, it never be called anymore dispite the client was waiting
> > for response. It seems like the EV_WRITE was removed from event base.
> >
> > Hope for your help, thanks a lot!
> ***********************************************************************
> To unsubscribe, send an e-mail to [email protected] with
> unsubscribe libevent-users    in the body.
>

Reply via email to