epoll_wait always sets EPOLLOUT bit?
I think you got it backwards. Don't include EPOLLOUT
unless you got EAGAIN
from a write attempt, and remove it when you have successfully written bytes to a socket.
This basically means that socket is always writable as long as there's space in socket in-kernel send buffer.
Edit 0:
You have two data streams - input and output.
You are waiting for the input by including EPOLLIN
in the flags. If upon return from epoll_wait(2)
that flag is not set, then either some event happened on some other socket, or this socket had some other event. Leave the flag in the events unless you get an error (meaning you are still interested in the input on the socket).
You don't have to wait for the output (since it's your action), you just write to the socket, but if you overflow socket send buffer, you'll get EAGAIN
from send(2)
or write(2)
. In this case you start waiting for output to be possible (kernel draining socket send buffer thus making room for you to send more) by including EPOLLOUT
. Once you get that, write your pending output bytes to the socket, and if you are successful, remove EPOLLOUT
from the events.
Now EPOLLET
indicates edge-triggered wait, meaning your desired event would only be signaled once per state change (like from "no input" to "there's input"). In this mode you are supposed to read input bytes in a loop until you get EAGAIN
.
Hope this helps.