Use kqueue to determine hangup on the other side of the socket or exceptional state of the socket
I've read man 2 kqueue
but have not found out how I can get notified about a socket hangup or exceptional condition of the socket without register开发者_StackOverflow中文版ing it with EVFILT_READ
or EVFILT_WRITE
. Apart from this it is not fully clear how kqueue signals exceptional states of sockets altogether.
Thanks for your answer in advance.
A trick that can be used to get EOL events while ignoring all READ events is to supply a ridiculously high value to NOTE_LOWAT, thus suppressing all READ events.
Here's an example doing this in a Python REPL:
Python 2.6.5 (r265:79063, Jul 17 2010, 22:57:01)
[GCC 4.2.1 20070719 [FreeBSD]] on freebsd8
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> import socket
>>> import sys
>>> a, b = socket.socketpair()
>>> kq = select.kqueue()
>>> kq.control([select.kevent(a, select.KQ_FILTER_READ, select.KQ_EV_ADD, select.KQ_NOTE_LOWAT, sys.maxint)], 0)
[]
>>> b.send('abc')
3
>>> kq.control(None, 10) # Interrupt after some time.
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> b.close()
>>> kq.control(None, 10) # Immediate return.
[<select.kevent ident=3 filter=-1 flags=0x8000 fflags=0x0 data=0x3 udata=0x0>]
>>>
Moreover, there is no such thing as exceptional state on FreeBSD, to quote man 2 select
:
The only exceptional condition detectable is out-of-band data received on a socket.
精彩评论