I'm using epoll
to get notifications about incoming data. It's not hard because all events returned by epoll_wait()
indicates, that I can read data from epoll_event.data.fd
(socket descriptor).
But now I want get both types of notification: receiving and sending (socket is available for send). But I can't to do it because:
epoll_event.events
which is returned by epoll_wait()
is the same as I pass in epoll_ctl()
. So it contains both EPOLLIN
and EPOLLOUT
in my case.epoll
(as EPOLLIN and as EPOLLOUT event) I'll get a EEXIST
.How can I solve this problem without manually calling select()
every time I get notification?
man epoll_wait
clearly states that "the events member will contain the returned event bit field.". Therefore, if you are getting EPOLLIN | EPOLLOUT
in epoll_event.events
, then your socket must be ready for both reading and writing.
If you only want to be notified when the socket changes state, use EPOLLET
for edge-triggered operation.
When you add a descriptor using epoll_ctl
, set the events
mask to be EPOLLIN | EPOLLOUT
.
When you get notifications via epoll_wait
then you'd loop through the returned notifications checking for EPOLLIN
and EPOLLOUT
.
Pseudo code:
int index, count;
count = epoll_wait(epfd, epoll_event, MAX_EVENTS, -1);
for (index = 0; index < count; ++index) {
if (epoll_event[index].events & EPOLLIN) {
// Ready for read
}
if (epoll_event[index].events & EPOLLOUT) {
// Ready for write
}
}
Some people only set the EPOLLOUT
bit when they have data present in their send buffer. I did not include any error checking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With