Use RawConnection.read

Instead of accessing its reader and writer directly.

TODO: considering add `ReaderWriterCloser` interface and let connection
and stream inherit from it.
This commit is contained in:
mhchia
2019-08-20 18:09:36 +08:00
parent 0b466ddc86
commit ef476e555b
9 changed files with 22 additions and 31 deletions

View File

@ -39,9 +39,12 @@ class RawConnection(IRawConnection):
async with self._drain_lock:
await self.writer.drain()
async def read(self) -> bytes:
line = await self.reader.readline()
return line.rstrip(b"\n")
async def read(self, n: int = -1) -> bytes:
"""
Read up to ``n`` bytes from the underlying stream.
This call is delegated directly to the underlying ``self.reader``.
"""
return await self.reader.read(n)
def close(self) -> None:
self.writer.close()

View File

@ -1,5 +1,4 @@
from abc import ABC, abstractmethod
import asyncio
class IRawConnection(ABC):
@ -9,17 +8,12 @@ class IRawConnection(ABC):
initiator: bool
# TODO: reader and writer shouldn't be exposed.
# Need better API for the consumers
reader: asyncio.StreamReader
writer: asyncio.StreamWriter
@abstractmethod
async def write(self, data: bytes) -> None:
pass
@abstractmethod
async def read(self) -> bytes:
async def read(self, n: int = -1) -> bytes:
pass
@abstractmethod