I am new to libev and I am having hard time understanding it. I have used select(), poll() and epoll() before and they were fairly easy to understand and implement. I now want to switch from epoll to libev. Here is what I am currently doing with epoll -
short int state[10000]; // stores the events for all fd
state[fd] |= current_state // Update state for the fd, current_state could be either EPOLLIN or EPOLLOUT but not both
/* attempt to add fd to epoll for monitoring. If fd already exists, then just modify the events */
ev.events = state[fd];
ev.data.fd = fd;
epollctl = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
if(epollctl == -1 && errno == EEXIST)
epollctl = epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &ev);
while(1)
{
num = epoll_wait (epollfd, events, 100000 , -1);
for(i = 0, i < num, ++i)
{
/* process the read and tell epoll to not notify anymore read events for given fd */
if(events[i].events & EPOLLIN)
{
process_read (m, m->read, events[i].data.fd );
state[events[i].data.fd] &= ~(EPOLLIN)
ev.events = state[events[i].data.fd]
ev.data.fd = events[i].data.fd;
epollctl = epoll_ctl(epollfd, EPOLL_CTL_MOD, events[i].data.fd, &ev);
}
/* process the write and tell epoll to not notify anymore write events for given fd */
if(events[i].events & EPOLLOUT)
{
process_write (m, m->write, events[i].data.fd );
state[events[i].data.fd] &= ~(EPOLLOUT)
ev.events = state[events[i].data.fd]
ev.data.fd = events[i].data.fd;
epollctl = epoll_ctl(epollfd, EPOLL_CTL_MOD, events[i].data.fd, &ev);
}
/* remove fd from epoll on error */
if(events[i].events & EPOLLERR)
epollctl = epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, &ev);
}
}
This is not the complete code, have purposely omitted the error checks and other non-relevant stuff while posting here so you can simply concentrate on logic. I am looking for libev equivalent method to achieve the following -
- Adding an fd for monitoring
- Removing an fd from monitoring
- Modify an event (READ / WRITE) for an fd that is already being monitored.
Can someone provide me a rough libev equivalent template for the above epoll code, that would be highly appreciated.