Created
August 21, 2012 10:03
-
-
Save bassosimone/3414130 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def netio_core_handle_read(sess): | |
''' Handles the READ I/O event ''' | |
# | |
# Read protocol: | |
# | |
# 1. netio_recv() MUST return data on success, MUST generate a close | |
# event when the socket is closed, and MAY generate an error event | |
# when there is an exception (letting the exception propagate is | |
# acceptable as well); | |
# | |
# 2. the read callback MAY generate an exception, which is catched | |
# by the lower layer, e.g. asyncore, converted into an error event, | |
# and propagated. | |
# | |
data = sess['netio_recv'](262144) | |
if data: | |
sess['netio_rbuf'].fromstring(data) | |
sess['session_handle_read'](sess) | |
... | |
def netio_core_readall(sess): | |
''' Reads all the content of the input buffer ''' | |
data = sess['netio_rbuf'].tostring() | |
sess['netio_rbuf'] = array.array('b') | |
return data | |
def netio_core_readline(sess): | |
''' Reads one line from the input buffer ''' | |
try: | |
index = sess['netio_rbuf'].index(ord('\n')) | |
except ValueError: | |
return '' | |
else: | |
line = sess['netio_rbuf'][:index + 1].tostring() | |
sess['netio_rbuf'] = sess['netio_rbuf'][index + 1:] | |
return line | |
def netio_core_readn(sess, count): | |
''' Reads exactly N bytes from the input buffer ''' | |
if len(sess['netio_rbuf']) < count: | |
return '' | |
data = sess['netio_rbuf'][:count].tostring() | |
sess['netio_rbuf'] = sess['netio_rbuf'][count:] | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment