mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
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:
@ -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()
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user