作为我正在从事的项目调查的一部分,我一直在研究用于检测和读取来自套接字的数据的不同事件循环机制/库。具体来说,我需要做的很简单:
- 检测来自客户端连接的数据
- 将文件描述符传递给工作线程进行读取和处理
Epoll 边缘触发非常适合这个目的,我喜欢边缘触发的行为,所以我只会在数据可用时收到一次通知。我尝试使用 libev 执行类似以下伪代码的操作,这似乎可行:
void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) {
1. Check for errors
2. ev_io_stop(loop, watcher) so I don't get constantly notified
3. Assign the ev_io watcher pointer into worker thread accessible data structure
3. Signal worker thread
4. Worker thread begins reading from watcher->fd
5. When worker thread get EAGAIN, start the watcher again
由于libuv
旨在用于类似目的并且是边缘触发的,因此我正在尝试做类似的事情但尚未成功。使用libuv
,我知道您可以use uv_read_start
从流中读取数据,但是使用此方法,将uv_read_cb
返回一个充满数据的缓冲区。由于我需要处理需要读取的数据量,并且为了避免将数据从这个缓冲区复制到不同的结构,我希望能够直接从套接字读取。
这种情况libuv
可以使用吗?
提前致谢!