Skip to content

uasyncio: StreamReader: Separate "poll socket" vs "I/O socket". #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions uasyncio/uasyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,33 @@ def wait(self, delay):

class StreamReader:

def __init__(self, s):
self.s = s
def __init__(self, polls, ios=None):
if ios is None:
ios = polls
self.polls = polls
self.ios = ios

def read(self, n=-1):
while True:
yield IORead(self.s)
res = self.s.read(n)
yield IORead(self.polls)
res = self.ios.read(n)
if res is not None:
break
log.warn("Empty read")
# This should not happen for real sockets, but can easily
# happen for stream wrappers (ssl, websockets, etc.)
#log.warn("Empty read")
if not res:
yield IOReadDone(self.s)
yield IOReadDone(self.polls)
return res

def readexactly(self, n):
buf = b""
while n:
yield IORead(self.s)
res = self.s.read(n)
yield IORead(self.polls)
res = self.ios.read(n)
assert res is not None
if not res:
yield IOReadDone(self.s)
yield IOReadDone(self.polls)
break
buf += res
n -= len(res)
Expand All @@ -115,11 +120,11 @@ def readline(self):
log.debug("StreamReader.readline()")
buf = b""
while True:
yield IORead(self.s)
res = self.s.readline()
yield IORead(self.polls)
res = self.ios.readline()
assert res is not None
if not res:
yield IOReadDone(self.s)
yield IOReadDone(self.polls)
break
buf += res
if buf[-1] == 0x0a:
Expand All @@ -129,11 +134,11 @@ def readline(self):
return buf

def aclose(self):
yield IOReadDone(self.s)
self.s.close()
yield IOReadDone(self.polls)
self.ios.close()

def __repr__(self):
return "<StreamReader %r>" % self.s
return "<StreamReader %r %r>" % (self.polls, self.ios)


class StreamWriter:
Expand Down