SDK-2880: Read in chunks

This commit is contained in:
Romuald Juchnowicz-Bierbasz
2019-06-25 17:53:16 +02:00
parent be03c83d45
commit 58b17d94fa
16 changed files with 104 additions and 71 deletions

View File

@@ -73,6 +73,7 @@ class Server():
self._methods = {}
self._notifications = {}
self._eof_listeners = []
self._input_buffer = bytes()
def register_method(self, name, callback, internal, sensitive_params=False):
"""
@@ -104,7 +105,7 @@ class Server():
async def run(self):
while self._active:
try:
data = await self._reader.readline()
data = await self._readline()
if not data:
self._eof()
continue
@@ -115,6 +116,21 @@ class Server():
logging.debug("Received %d bytes of data", len(data))
self._handle_input(data)
async def _readline(self):
"""Like StreamReader.readline but without limit"""
while True:
chunk = await self._reader.read(1024)
if not chunk:
return chunk
previous_size = len(self._input_buffer)
self._input_buffer += chunk
it = self._input_buffer.find(b"\n", previous_size)
if it < 0:
continue
line = self._input_buffer[:it]
self._input_buffer = self._input_buffer[it+1:]
return line
def stop(self):
self._active = False