libevent库源码学习-devpoll(/dev/poll)( linux)

来源:互联网 发布:2010年东决詹姆斯数据 编辑:程序博客网 时间:2024/06/04 17:53

1.初始化
使用
fd = open(“/dev/poll”, flags, (mode_t)mode);获得文件描述符

2.增加事件
struct pollfd *events;
pwrite(fd, events,sizeof(struct pollfd) * eventsnum, 0) 直接往里边写

3.删除事件
使用 POLLREMOVE 事件,然后把这个时间用增加时间的方法写入/dev/poll,系统会自动将我们要删除的描述符时间删除


4.监听事件

struct dvpoll {        struct pollfd *dp_fds;        int dp_nfds;        int dp_timeout;    }
struct dvpoll dvp;
res = ioctl(devpollop->dpfd, DP_POLL, &dvp);返回事件发生的个数

用个for遍历一遍即可
for (i = 0; i < res; i++) {
int which = 0;
int what = dvp.dp_fds[i].revents;

if (what & POLLHUP)
what |= POLLIN | POLLOUT;
else if (what & POLLERR)
what |= POLLIN | POLLOUT;

if (what & POLLIN)
which |= EV_READ;
if (what & POLLOUT)
which |= EV_WRITE;


if (!which)
continue;
。。。
}

原创粉丝点击