Fixed bug in select
This commit is contained in:
@ -67,17 +67,20 @@ def poll():
|
||||
"""
|
||||
return Poll()
|
||||
|
||||
def select(readlist, writelist, errlist, timeout=None):
|
||||
"""Performs a select call. Works on SAM sockets and Python
|
||||
sockets. See select.select() in the Python library for more
|
||||
information."""
|
||||
def _has_data(S):
|
||||
"""True if the given I2P socket has data waiting."""
|
||||
try:
|
||||
S.recv(1, i2p.socket.MSG_PEEK | i2p.socket.MSG_DONTWAIT)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def _noblock_select(readlist, writelist, errlist):
|
||||
"""Makes a single query of the given sockets, like
|
||||
select() with timeout 0.0."""
|
||||
Rans = []
|
||||
Wans = []
|
||||
Eans = []
|
||||
if timeout != None: end = time.clock() + timeout
|
||||
while True:
|
||||
# FIXME: Check performance.
|
||||
# Use pyselect.poll for Python sockets, if needed for speed.
|
||||
|
||||
# Check for read availability.
|
||||
for R in readlist:
|
||||
@ -87,14 +90,16 @@ def select(readlist, writelist, errlist, timeout=None):
|
||||
Rans.append(R)
|
||||
else:
|
||||
# SAM Socket
|
||||
if R.type == i2p.socket.SOCK_STREAM:
|
||||
try:
|
||||
R._verify_connected()
|
||||
if _has_data(R):
|
||||
Rans.append(R)
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
if len(R) > 0: Rans.append(R)
|
||||
# if R.type == i2p.socket.SOCK_STREAM:
|
||||
# try:
|
||||
# R._verify_connected()
|
||||
# Rans.append(R)
|
||||
# except:
|
||||
# pass
|
||||
# else:
|
||||
# if len(R.sessobj) > 0: Rans.append(R)
|
||||
|
||||
# Check for write availability.
|
||||
for W in writelist:
|
||||
@ -130,8 +135,31 @@ def select(readlist, writelist, errlist, timeout=None):
|
||||
Eans.append(E)
|
||||
except:
|
||||
pass
|
||||
|
||||
return (Rans, Wans, Eans)
|
||||
|
||||
|
||||
def select(readlist, writelist, errlist, timeout=None):
|
||||
"""Performs a select call. Works on SAM sockets and Python
|
||||
sockets. See select.select() in the Python library for more
|
||||
information."""
|
||||
|
||||
if timeout != None: end = time.clock() + timeout
|
||||
while True:
|
||||
# FIXME: Check performance.
|
||||
# Use pyselect.poll for Python sockets, if needed for speed.
|
||||
(Rans, Wans, Eans) = _noblock_select(readlist,writelist,errlist)
|
||||
|
||||
if timeout != None and time.clock() >= end: break
|
||||
if len(Rans) != 0 or len(Wans) != 0 or len(Eans) != 0: break
|
||||
if len(Rans) != 0 or len(Wans) != 0 or len(Eans) != 0:
|
||||
# One or more sockets are ready.
|
||||
if timeout != 0.0:
|
||||
# Check again, because sockets may have changed state while
|
||||
# we did _noblock_select (it's safer to check twice, since
|
||||
# they usually go from no data => data ready, and so forth).
|
||||
(Rans, Wans, Eans) = _noblock_select(readlist, writelist,
|
||||
errlist)
|
||||
return (Rans, Wans, Eans)
|
||||
|
||||
time.sleep(0.01)
|
||||
|
||||
|
Reference in New Issue
Block a user