7

我知道为了使用 libevent 监视套接字, event_set()应该首先使用正确的参数调用。

libevent 文档指出eventevent_set() 的参数可以是 EV_READ 或 EV_WRITE。并且这个事件参数是要注意的事件。

但是 EV_READ 和 EV_WRITE 对应的是什么套接字事件呢?我的意思是我将如何监控连接状态的变化,而不是监控传入的消息?

4

1 回答 1

4

I've found this site to be excellent in terms of documentation for libevent. On the page dealing with events, there's a nice overview of what different event flags actually mean. From that link:

  • EV_READ : This flag indicates an event that becomes active when the provided file descriptor is ready for reading.

  • EV_WRITE : This flag indicates an event that becomes active when the provided file descriptor is ready for writing.

  • EV_SIGNAL : Used to implement signal detection.

  • EV_PERSIST : Indicates that the event is persistent.

  • EV_ET : Indicates that the event should be edge-triggered, if the underlying event_base backend supports edge-triggered events. This affects the semantics of EV_READ and EV_WRITE.

So to answer your question explicitly: EV_READ corresponds to having data available to be read from the socket or bufferevent, which are the libevent socket equivalents as far as I can tell. EV_WRITE corresponds to the socket/bufferevent being ready to have data written to it. You can set read / write callbacks to actually do the data reading and writing with the cb argument to

struct event *event_new(struct event_base *base, evutil_socket_t fd, short what, event_callback_fn cb, void *arg);

If you're doing socket IO with libevent, though, you may really want to consider using buffer events - they're what I use in one of my projects, snot_mon, which you can check out over on github.

于 2011-08-29T18:46:39.480 回答