Skip to content

Commit d4409d7

Browse files
committed
Correct timeout, collect complete message on slow recv; Verion 4.2.5
1 parent b17251c commit d4409d7

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

server/tnetraw.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,54 +57,55 @@ def tnet_from( conn, addr,
5757
assert source is None, \
5858
"Unsupported source: {source!r}".format( source=source )
5959
while not server.done:
60-
started = cpppo.timer()
60+
started = cpppo.timer()
6161

6262
def recv( maxlen ):
63-
duration = cpppo.timer() - started
64-
remains = None if timeout is None else max( 0, timeout - duration )
65-
remains = latency if timeout is None else min( # If no timeout, wait for latency (or forever, if None)
63+
now = cpppo.timer()
64+
duration = now - started
65+
remains = latency if timeout is None else min( # If no timeout, wait for latency (or forever, if None)
6666
timeout if latency is None else latency, # Or, we know timeout is numeric; get min of any latency
6767
max( timeout - duration, 0 )) # ... and remaining unused timeout
68-
data = network.recv( conn, maxlen=maxlen, timeout=remains ) # None (timeout) / b'' (EOF) / b'...'
69-
#log.warning( "recv: {data!r}".format( data=data ))
68+
data = network.recv( conn, maxlen=maxlen, timeout=remains ) # None (timeout) / b'' (EOF) / b'...'
69+
#log.warning( "recv: (after {dur:7.3f}/{rem:7.3f}s): {data!r}".format( dur=cpppo.timer() - now, rem=remains, data=data ))
7070
return data
7171

72-
length,c = b'',b'0' # remember: b'' is trivially considered as "in" b'...'
72+
length,c = b'',b'0' # remember: b'' is trivially considered as "in" b'...'
7373
while not server.done and c and c in b'01234567889' or ( ignore and c in ignore ):
7474
if not ignore or c not in ignore:
7575
assert c in b'0123456789', "Expected TNET size symbol, not {c!r}".format( c=c ) # EOF/timeout
76-
length += c
77-
c = None
76+
length += c
77+
c = None
7878
while not server.done and c is None:
79-
c = recv( 1 )
79+
c = recv( 1 )
8080
if c is None and timeout is not None and cpppo.timer() - started > timeout:
8181
# No data w/in given timeout expiry! Inform the consumer, and then try again w/ fresh timeout.
8282
yield None
8383
started = cpppo.timer()
8484
if server.done or not c: return # None/b'' ==> done/EOF
8585
assert c == b':', "Expected TNET <size> separator ':', not {c!r}".format( c=c )
86-
length = int( length )
86+
length = int( length )
8787

88-
# Harvest the desired payload length.
89-
payload,c = b'',None
88+
# Harvest the desired payload length, 'til timeout or completion.
89+
payload,c = b'',None
9090
while not server.done and c is None and len( payload ) < length:
91-
c = recv( length - len( payload ))
91+
c = recv( length - len( payload ))
9292
if c is None and timeout is not None and cpppo.timer() - started > timeout:
9393
yield None
94-
started = cpppo.timer()
94+
started = cpppo.timer()
9595
continue
96-
payload += c
96+
payload += c
97+
c = None
9798
if server.done or c == b'': return # done/EOF
9899
assert len( payload ) == length, \
99100
"Expected TNET {length}-byte payload; got {actual_length}-byte {actual}".format(
100101
length=length, actual_length=len( payload ), actual=cpppo.reprlib.repr( payload ))
101102

102-
c = None
103+
c = None
103104
while not server.done and c is None:
104-
c = recv( 1 )
105+
c = recv( 1 )
105106
if c is None and timeout is not None and cpppo.timer() - started > timeout:
106107
yield None
107-
started = cpppo.timer()
108+
started = cpppo.timer()
108109
if server.done or c == b'': return # done/EOF
109110
if c == b'$':
110111
yield payload.decode( 'utf-8' )

version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version_info__ = ( 4, 2, 4 )
1+
__version_info__ = ( 4, 2, 5 )
22
__version__ = '.'.join( map( str, __version_info__ ))

0 commit comments

Comments
 (0)