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

[Libevent-users] Why write event handler was trigged only once?



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!