LaKabane / libtuntap

The portable Tun/Tap devices configuration utility
193 stars 63 forks source link

Non blocking read write #43

Closed cozis closed 9 months ago

cozis commented 11 months ago

resolves #42

tleguern commented 10 months ago

Hello, I did a small clean up by removing extra blanks at end of lines and a redundant removal then adding back of FILE_FLAG_OVERLAPPED.

chipot commented 9 months ago

Thanks for the change. I find that timeout support on IO functions does not promote good code in general. A good practice for async IO is to use a multiplexer (select, poll, epoll, kqueue, ...). Supporting timeouts encourage code using them, and it make sense only for toy projects with one or two file descriptors. The languages successfully using timeout on calls are languages providing some sort of async/await controls, so the IO are still multiplexed across all the existing tasks. C does not do that and blocking until a timeout blocks the whole process. In the linked issue you mention using this timeout to multiplex between the tap device and a queue. I don't have the full picture but in this situation I would use the time of the most imminent event of the queue as timeout to a central select call. In this fd_set, the tap device is registered along with the fd of other peers. This way you ensure your app wakes up on time for any async IO.

A timerfd but it could also work, if portability is not an issue. But it has its quirks, so I don't recommend. Sorry for the late review. I'm not blocking this PR as I understand it could be useful for toy projects and for educational purposes.

cozis commented 9 months ago

@chipot Thank you for the feedback! I agree with you that this interface works best when there are few descriptors. Before I opened the issue, I considered using select on the descriptor from my code. However, this approach didn't work on Windows because it would require changes to the ReadFile and WriteFile calls to achieve similar behavior. The difference between Windows and Unix in handling asynchronous operations is quite troublesome when writing cross-platform code!

I refactored the functions to share common code. Let me know if this works for you!