Fixed bug in select
This commit is contained in:
@ -67,17 +67,20 @@ def poll():
|
|||||||
"""
|
"""
|
||||||
return Poll()
|
return Poll()
|
||||||
|
|
||||||
def select(readlist, writelist, errlist, timeout=None):
|
def _has_data(S):
|
||||||
"""Performs a select call. Works on SAM sockets and Python
|
"""True if the given I2P socket has data waiting."""
|
||||||
sockets. See select.select() in the Python library for more
|
try:
|
||||||
information."""
|
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 = []
|
Rans = []
|
||||||
Wans = []
|
Wans = []
|
||||||
Eans = []
|
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.
|
# Check for read availability.
|
||||||
for R in readlist:
|
for R in readlist:
|
||||||
@ -87,14 +90,16 @@ def select(readlist, writelist, errlist, timeout=None):
|
|||||||
Rans.append(R)
|
Rans.append(R)
|
||||||
else:
|
else:
|
||||||
# SAM Socket
|
# SAM Socket
|
||||||
if R.type == i2p.socket.SOCK_STREAM:
|
if _has_data(R):
|
||||||
try:
|
|
||||||
R._verify_connected()
|
|
||||||
Rans.append(R)
|
Rans.append(R)
|
||||||
except:
|
# if R.type == i2p.socket.SOCK_STREAM:
|
||||||
pass
|
# try:
|
||||||
else:
|
# R._verify_connected()
|
||||||
if len(R) > 0: Rans.append(R)
|
# Rans.append(R)
|
||||||
|
# except:
|
||||||
|
# pass
|
||||||
|
# else:
|
||||||
|
# if len(R.sessobj) > 0: Rans.append(R)
|
||||||
|
|
||||||
# Check for write availability.
|
# Check for write availability.
|
||||||
for W in writelist:
|
for W in writelist:
|
||||||
@ -130,8 +135,31 @@ def select(readlist, writelist, errlist, timeout=None):
|
|||||||
Eans.append(E)
|
Eans.append(E)
|
||||||
except:
|
except:
|
||||||
pass
|
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 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)
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user