Move varint and delim read/write to toplevel

To `libp2p.utils`.
This commit is contained in:
mhchia
2019-08-17 00:19:37 +08:00
parent 5192944724
commit a0923d202a
6 changed files with 44 additions and 21 deletions

View File

@ -1,23 +1,10 @@
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.stream_muxer.abc import IMuxedStream
from libp2p.stream_muxer.mplex.utils import decode_uvarint_from_stream, encode_uvarint
from libp2p.typing import StreamReader
from libp2p.utils import encode_delim, read_delim
from .multiselect_communicator_interface import IMultiselectCommunicator
def delim_encode(msg_str: str) -> bytes:
msg_bytes = msg_str.encode()
varint_len_msg = encode_uvarint(len(msg_bytes) + 1)
return varint_len_msg + msg_bytes + b"\n"
async def delim_read(reader: StreamReader, timeout: int = 10) -> str:
len_msg = await decode_uvarint_from_stream(reader, timeout)
msg_bytes = await reader.read(len_msg)
return msg_bytes.decode().rstrip()
class RawConnectionCommunicator(IMultiselectCommunicator):
conn: IRawConnection
@ -25,12 +12,12 @@ class RawConnectionCommunicator(IMultiselectCommunicator):
self.conn = conn
async def write(self, msg_str: str) -> None:
msg_bytes = delim_encode(msg_str)
msg_bytes = encode_delim(msg_str)
self.conn.writer.write(msg_bytes)
await self.conn.writer.drain()
async def read(self) -> str:
return await delim_read(self.conn.reader)
return await read_delim(self.conn.reader)
class StreamCommunicator(IMultiselectCommunicator):
@ -40,8 +27,8 @@ class StreamCommunicator(IMultiselectCommunicator):
self.stream = stream
async def write(self, msg_str: str) -> None:
msg_bytes = delim_encode(msg_str)
msg_bytes = encode_delim(msg_str)
await self.stream.write(msg_bytes)
async def read(self) -> str:
return await delim_read(self.stream)
return await read_delim(self.stream)