libevent 和非阻塞套接字
据我了解,为了使用 libevent 监视套接字,应首先使用正确的参数调用 event_set()
。
libevent 文档指出 event_set() 的 event
参数可以是 EV_READ 或 EV_WRITE。该事件参数是需要注意的事件。
但是 EV_READ 和 EV_WRITE 对应什么套接字事件呢?我的意思是,与监视传入消息相比,我将如何监视连接状态的变化?
I understand that in order to monitor a socket using libevent, event_set()
should first be called with the correct parameters.
The libevent documentation states that the event
parameter to event_set() can be either EV_READ or EV_WRITE. And that this event parameter is the event to look out for.
But what socket events do EV_READ and EV_WRITE correspond to? I mean how would I monitor for a change in connection status, versus monitor for an incoming message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现这个网站在文档方面非常出色libevent。在处理事件的页面上,有一个关于不同事件的很好的概述事件标志的实际含义。从该链接:
因此,明确回答您的问题: EV_READ 对应于可以从套接字或 bufferevent 读取数据,据我所知,它们是 libevent 套接字的等效项。 EV_WRITE 对应于套接字/缓冲区事件已准备好将数据写入其中。 的
cb
参数设置读/写回调来实际执行数据读取和写入您可以使用
结构事件 *event_new(结构 event_base *base, evutil_socket_t fd,
简而言之,event_callback_fn cb, void *arg);
如果您使用 libevent 进行套接字 IO,那么您可能真的需要考虑使用 缓冲事件 - 它们是我在我的一个项目中使用的,snot_mon,您可以在 github 上查看。
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:
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 tostruct 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.