| 1 |
""" |
| 2 |
An RPython implementation of select.poll() based on rffi. |
| 3 |
Note that this is not a drop-in replacement: the interface is |
| 4 |
simplified - instead of a polling object there is only a poll() |
| 5 |
function that directly takes a dictionary as argument. |
| 6 |
""" |
| 7 |
|
| 8 |
from errno import EINTR |
| 9 |
from rpython.rlib import _rsocket_rffi as _c |
| 10 |
from rpython.rlib.rarithmetic import r_uint |
| 11 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
eventnames = '''POLLIN POLLPRI POLLOUT POLLERR POLLHUP POLLNVAL |
| 17 |
POLLRDNORM POLLRDBAND POLLWRNORM POLLWEBAND POLLMSG |
| 18 |
FD_SETSIZE'''.split() |
| 19 |
|
| 20 |
eventnames = [name for name in eventnames |
| 21 |
if _c.constants.get(name) is not None] |
| 22 |
|
| 23 |
for name in eventnames: |
| 24 |
globals()[name] = _c.constants[name] |
| 25 |
|
| 26 |
class PollError(Exception): |
| 27 |
def __init__(self, errno): |
| 28 |
self.errno = errno |
| 29 |
def get_msg(self): |
| 30 |
return _c.socket_strerror_str(self.errno) |
| 31 |
def get_msg_unicode(self): |
| 32 |
return _c.socket_strerror_unicode(self.errno) |
| 33 |
def get_msg_utf8(self): |
| 34 |
return _c.socket_strerror_utf8(self.errno) |
| 35 |
|
| 36 |
class SelectError(Exception): |
| 37 |
def __init__(self, errno): |
| 38 |
self.errno = errno |
| 39 |
def get_msg(self): |
| 40 |
return _c.socket_strerror_str(self.errno) |
| 41 |
def get_msg_unicode(self): |
| 42 |
return _c.socket_strerror_unicode(self.errno) |
| 43 |
def get_msg_utf8(self): |
| 44 |
return _c.socket_strerror_utf8(self.errno) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if hasattr(_c, 'poll'): |
| 50 |
|
| 51 |
def poll(fddict, timeout=-1): |
| 52 |
"""'fddict' maps file descriptors to interesting events. |
| 53 |
'timeout' is an integer in milliseconds, and NOT a float |
| 54 |
number of seconds, but it's the same in CPython. Use -1 for infinite. |
| 55 |
Returns a list [(fd, events)]. |
| 56 |
""" |
| 57 |
numfd = len(fddict) |
| 58 |
pollfds = lltype.malloc(_c.pollfdarray, numfd, flavor='raw') |
| 59 |
try: |
| 60 |
i = 0 |
| 61 |
for fd, events in fddict.iteritems(): |
| 62 |
rffi.setintfield(pollfds[i], 'c_fd', fd) |
| 63 |
rffi.setintfield(pollfds[i], 'c_events', events) |
| 64 |
i += 1 |
| 65 |
assert i == numfd |
| 66 |
|
| 67 |
ret = _c.poll(pollfds, numfd, timeout) |
| 68 |
|
| 69 |
if ret < 0: |
| 70 |
raise PollError(_c.geterrno()) |
| 71 |
|
| 72 |
retval = [] |
| 73 |
for i in range(numfd): |
| 74 |
pollfd = pollfds[i] |
| 75 |
fd = rffi.cast(lltype.Signed, pollfd.c_fd) |
| 76 |
revents = rffi.cast(lltype.Signed, pollfd.c_revents) |
| 77 |
if revents: |
| 78 |
retval.append((fd, revents)) |
| 79 |
finally: |
| 80 |
lltype.free(pollfds, flavor='raw') |
| 81 |
return retval |
| 82 |
|
| 83 |
def select(inl, outl, excl, timeout=-1.0, handle_eintr=False): |
| 84 |
nfds = 0 |
| 85 |
if inl: |
| 86 |
ll_inl = lltype.malloc(_c.fd_set.TO, flavor='raw') |
| 87 |
_c.FD_ZERO(ll_inl) |
| 88 |
for i in inl: |
| 89 |
_c.FD_SET(i, ll_inl) |
| 90 |
if i > nfds: |
| 91 |
nfds = i |
| 92 |
else: |
| 93 |
ll_inl = lltype.nullptr(_c.fd_set.TO) |
| 94 |
if outl: |
| 95 |
ll_outl = lltype.malloc(_c.fd_set.TO, flavor='raw') |
| 96 |
_c.FD_ZERO(ll_outl) |
| 97 |
for i in outl: |
| 98 |
_c.FD_SET(i, ll_outl) |
| 99 |
if i > nfds: |
| 100 |
nfds = i |
| 101 |
else: |
| 102 |
ll_outl = lltype.nullptr(_c.fd_set.TO) |
| 103 |
if excl: |
| 104 |
ll_excl = lltype.malloc(_c.fd_set.TO, flavor='raw') |
| 105 |
_c.FD_ZERO(ll_excl) |
| 106 |
for i in excl: |
| 107 |
_c.FD_SET(i, ll_excl) |
| 108 |
if i > nfds: |
| 109 |
nfds = i |
| 110 |
else: |
| 111 |
ll_excl = lltype.nullptr(_c.fd_set.TO) |
| 112 |
|
| 113 |
if timeout < 0: |
| 114 |
ll_timeval = lltype.nullptr(_c.timeval) |
| 115 |
while True: |
| 116 |
res = _c.select(nfds + 1, ll_inl, ll_outl, ll_excl, ll_timeval) |
| 117 |
if not handle_eintr or res >= 0 or _c.geterrno() != EINTR: |
| 118 |
break |
| 119 |
else: |
| 120 |
sec = int(timeout) |
| 121 |
usec = int((timeout - sec) * 10**6) |
| 122 |
ll_timeval = rffi.make(_c.timeval) |
| 123 |
rffi.setintfield(ll_timeval, 'c_tv_sec', sec) |
| 124 |
rffi.setintfield(ll_timeval, 'c_tv_usec', usec) |
| 125 |
res = _c.select(nfds + 1, ll_inl, ll_outl, ll_excl, ll_timeval) |
| 126 |
if handle_eintr and res < 0 and _c.geterrno() == EINTR: |
| 127 |
res = 0 |
| 128 |
try: |
| 129 |
if res == -1: |
| 130 |
raise SelectError(_c.geterrno()) |
| 131 |
if res == 0: |
| 132 |
return ([], [], []) |
| 133 |
else: |
| 134 |
return ( |
| 135 |
[i for i in inl if _c.FD_ISSET(i, ll_inl)], |
| 136 |
[i for i in outl if _c.FD_ISSET(i, ll_outl)], |
| 137 |
[i for i in excl if _c.FD_ISSET(i, ll_excl)]) |
| 138 |
finally: |
| 139 |
if ll_inl: |
| 140 |
lltype.free(ll_inl, flavor='raw') |
| 141 |
if ll_outl: |
| 142 |
lltype.free(ll_outl, flavor='raw') |
| 143 |
if ll_excl: |
| 144 |
lltype.free(ll_excl, flavor='raw') |
| 145 |
if ll_timeval: |
| 146 |
lltype.free(ll_timeval, flavor='raw') |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
if hasattr(_c, 'WSAEventSelect'): |
| 152 |
|
| 153 |
|
| 154 |
def _poll(fddict, timeout=-1): |
| 155 |
"""'fddict' maps file descriptors to interesting events. |
| 156 |
'timeout' is an integer in milliseconds, and NOT a float |
| 157 |
number of seconds, but it's the same in CPython. Use -1 for infinite. |
| 158 |
Returns a list [(fd, events)]. |
| 159 |
""" |
| 160 |
numfd = len(fddict) |
| 161 |
numevents = 0 |
| 162 |
socketevents = lltype.malloc(_c.WSAEVENT_ARRAY, numfd, flavor='raw') |
| 163 |
try: |
| 164 |
eventdict = {} |
| 165 |
|
| 166 |
for fd, events in fddict.iteritems(): |
| 167 |
|
| 168 |
wsaEvents = 0 |
| 169 |
if events & _c.POLLIN: |
| 170 |
wsaEvents |= _c.FD_READ | _c.FD_ACCEPT | _c.FD_CLOSE |
| 171 |
if events & _c.POLLOUT: |
| 172 |
wsaEvents |= _c.FD_WRITE | _c.FD_CONNECT | _c.FD_CLOSE |
| 173 |
|
| 174 |
|
| 175 |
if wsaEvents == 0: |
| 176 |
continue |
| 177 |
|
| 178 |
|
| 179 |
event = _c.WSACreateEvent() |
| 180 |
if _c.WSAEventSelect(fd, event, wsaEvents) != 0: |
| 181 |
raise PollError(_c.geterrno()) |
| 182 |
|
| 183 |
eventdict[fd] = event |
| 184 |
socketevents[numevents] = event |
| 185 |
numevents += 1 |
| 186 |
|
| 187 |
assert numevents <= numfd |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if timeout < 0: |
| 198 |
timeout = _c.INFINITE |
| 199 |
|
| 200 |
|
| 201 |
ret = _c.WSAWaitForMultipleEvents(numevents, socketevents, |
| 202 |
False, timeout, False) |
| 203 |
|
| 204 |
if ret == _c.WSA_WAIT_TIMEOUT: |
| 205 |
return [] |
| 206 |
|
| 207 |
if ret == r_uint(_c.WSA_WAIT_FAILED): |
| 208 |
raise PollError(_c.geterrno()) |
| 209 |
|
| 210 |
retval = [] |
| 211 |
info = rffi.make(_c.WSANETWORKEVENTS) |
| 212 |
for fd, event in eventdict.iteritems(): |
| 213 |
if _c.WSAEnumNetworkEvents(fd, event, info) < 0: |
| 214 |
continue |
| 215 |
revents = 0 |
| 216 |
if info.c_lNetworkEvents & _c.FD_READ: |
| 217 |
revents |= _c.POLLIN |
| 218 |
if info.c_lNetworkEvents & _c.FD_ACCEPT: |
| 219 |
revents |= _c.POLLIN |
| 220 |
if info.c_lNetworkEvents & _c.FD_WRITE: |
| 221 |
revents |= _c.POLLOUT |
| 222 |
if info.c_lNetworkEvents & _c.FD_CONNECT: |
| 223 |
if info.c_iErrorCode[_c.FD_CONNECT_BIT]: |
| 224 |
revents |= _c.POLLERR |
| 225 |
else: |
| 226 |
revents |= _c.POLLOUT |
| 227 |
if info.c_lNetworkEvents & _c.FD_CLOSE: |
| 228 |
if info.c_iErrorCode[_c.FD_CLOSE_BIT]: |
| 229 |
revents |= _c.POLLERR |
| 230 |
else: |
| 231 |
if fddict[fd] & _c.POLLIN: |
| 232 |
revents |= _c.POLLIN |
| 233 |
if fddict[fd] & _c.POLLOUT: |
| 234 |
revents |= _c.POLLOUT |
| 235 |
if revents: |
| 236 |
retval.append((fd, revents)) |
| 237 |
|
| 238 |
lltype.free(info, flavor='raw') |
| 239 |
|
| 240 |
finally: |
| 241 |
for fd, event in eventdict.iteritems(): |
| 242 |
_c.WSAEventSelect(fd, event, 0) |
| 243 |
_c.WSACloseEvent(event) |
| 244 |
lltype.free(socketevents, flavor='raw') |
| 245 |
|
| 246 |
return retval |