kopia lustrzana https://github.com/micropython/micropython-lib
uasyncio: StreamReader: Separate "poll socket" vs "I/O socket".
Poll socket is what's passed to uselect.poll(), while I/O socket is what's used for .read(). This is a workaround of the issue that MicroPython doesn't support proxying poll functionality for stream wrappers (like SSL, websocket, etc.) This issue is tracked as https://github.com/micropython/micropython/issues/3394 It may be that it's more efficient to apply such a workaround on uasyncio level rather than implementing full solution of uPy side.pull/227/merge
rodzic
bd828087e1
commit
f704ac5817
|
@ -90,28 +90,33 @@ class PollEventLoop(EventLoop):
|
||||||
|
|
||||||
class StreamReader:
|
class StreamReader:
|
||||||
|
|
||||||
def __init__(self, s):
|
def __init__(self, polls, ios=None):
|
||||||
self.s = s
|
if ios is None:
|
||||||
|
ios = polls
|
||||||
|
self.polls = polls
|
||||||
|
self.ios = ios
|
||||||
|
|
||||||
def read(self, n=-1):
|
def read(self, n=-1):
|
||||||
while True:
|
while True:
|
||||||
yield IORead(self.s)
|
yield IORead(self.polls)
|
||||||
res = self.s.read(n)
|
res = self.ios.read(n)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
break
|
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:
|
if not res:
|
||||||
yield IOReadDone(self.s)
|
yield IOReadDone(self.polls)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def readexactly(self, n):
|
def readexactly(self, n):
|
||||||
buf = b""
|
buf = b""
|
||||||
while n:
|
while n:
|
||||||
yield IORead(self.s)
|
yield IORead(self.polls)
|
||||||
res = self.s.read(n)
|
res = self.ios.read(n)
|
||||||
assert res is not None
|
assert res is not None
|
||||||
if not res:
|
if not res:
|
||||||
yield IOReadDone(self.s)
|
yield IOReadDone(self.polls)
|
||||||
break
|
break
|
||||||
buf += res
|
buf += res
|
||||||
n -= len(res)
|
n -= len(res)
|
||||||
|
@ -122,11 +127,11 @@ class StreamReader:
|
||||||
log.debug("StreamReader.readline()")
|
log.debug("StreamReader.readline()")
|
||||||
buf = b""
|
buf = b""
|
||||||
while True:
|
while True:
|
||||||
yield IORead(self.s)
|
yield IORead(self.polls)
|
||||||
res = self.s.readline()
|
res = self.ios.readline()
|
||||||
assert res is not None
|
assert res is not None
|
||||||
if not res:
|
if not res:
|
||||||
yield IOReadDone(self.s)
|
yield IOReadDone(self.polls)
|
||||||
break
|
break
|
||||||
buf += res
|
buf += res
|
||||||
if buf[-1] == 0x0a:
|
if buf[-1] == 0x0a:
|
||||||
|
@ -136,11 +141,11 @@ class StreamReader:
|
||||||
return buf
|
return buf
|
||||||
|
|
||||||
def aclose(self):
|
def aclose(self):
|
||||||
yield IOReadDone(self.s)
|
yield IOReadDone(self.polls)
|
||||||
self.s.close()
|
self.ios.close()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<StreamReader %r>" % self.s
|
return "<StreamReader %r %r>" % (self.polls, self.ios)
|
||||||
|
|
||||||
|
|
||||||
class StreamWriter:
|
class StreamWriter:
|
||||||
|
|
Ładowanie…
Reference in New Issue