0

I'm trying to send a buffer to my socket client when the file descriptor is available for writing.

EV_SET is set to: EVFILT_WRITE, EV_ADD | EV_DISABLE | EV_CLEAR

then when changed to EVFILT_WRITE, EV_ENABLE then EVFILT_WRITE get triggered once which is great!

but if i use the function write or send when i get EVFILT_WRITE like this:

if (e->filter == EVFILT_WRITE)
send(socket, buff, strlen(buff), 0);

then i get again another EVFILT_WRITE event. It seems like the send function trigger another EVFILT_WRITE event. is that expected behaviour? I thought EVFILT_WRITE triggers only when the file descriptor is available for writing.

I searched for the issue, but it looks like nobody mention that. Can someone please confirm if that is expected behaviour and why?

1 Answers1

0

This is how I understand it:

Since you used EV_CLEAR, the kevent facility starts to return state transitions, not the current state. So, whenever you touch the socket descriptor with kevent() or send() calls, you get an EVFILT_WRITE event back.

Another way to look at this:

When send() is called the descriptor becomes unavailable for writing for a moment and then again becomes available, which is why you get an event.

I'll try to loop in some knowledgeable people to this question.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • I tried to call send() 10 times in a loop, but it result in 2 EVFILT_WRITE... according to what you said it should return 11 EVFILT_WRITE? – hanerik jansen Dec 04 '22 at 10:24
  • This is probably because consecutive `send()` calls get aggregated. Try waiting for a `EVFILT_WRITE` event before making the next `send()` call. – arrowd Dec 05 '22 at 07:51