2019-08-15 23:31:26 +08:00
|
|
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
|
|
from libp2p.stream_muxer.abc import IMuxedStream
|
2019-08-17 00:19:37 +08:00
|
|
|
from libp2p.utils import encode_delim, read_delim
|
2019-08-07 18:02:30 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
from .multiselect_communicator_interface import IMultiselectCommunicator
|
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2019-08-15 23:31:26 +08:00
|
|
|
class RawConnectionCommunicator(IMultiselectCommunicator):
|
|
|
|
conn: IRawConnection
|
|
|
|
|
|
|
|
def __init__(self, conn: IRawConnection) -> None:
|
|
|
|
self.conn = conn
|
|
|
|
|
|
|
|
async def write(self, msg_str: str) -> None:
|
2019-08-17 00:19:37 +08:00
|
|
|
msg_bytes = encode_delim(msg_str)
|
2019-08-15 23:31:26 +08:00
|
|
|
self.conn.writer.write(msg_bytes)
|
|
|
|
await self.conn.writer.drain()
|
|
|
|
|
|
|
|
async def read(self) -> str:
|
2019-08-17 00:19:37 +08:00
|
|
|
return await read_delim(self.conn.reader)
|
2019-08-15 23:31:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
class StreamCommunicator(IMultiselectCommunicator):
|
|
|
|
stream: IMuxedStream
|
|
|
|
|
|
|
|
def __init__(self, stream: IMuxedStream) -> None:
|
|
|
|
self.stream = stream
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-08-07 18:02:30 +08:00
|
|
|
async def write(self, msg_str: str) -> None:
|
2019-08-17 00:19:37 +08:00
|
|
|
msg_bytes = encode_delim(msg_str)
|
2019-08-15 23:31:26 +08:00
|
|
|
await self.stream.write(msg_bytes)
|
|
|
|
|
|
|
|
async def read(self) -> str:
|
2019-08-17 00:19:37 +08:00
|
|
|
return await read_delim(self.stream)
|