Fix MplexStream.read

This commit is contained in:
mhchia
2019-09-06 17:26:40 +08:00
parent 95926b7376
commit 649a230776
8 changed files with 116 additions and 143 deletions

View File

@ -11,11 +11,12 @@ from libp2p.peer.peerinfo import info_from_p2p_addr
from libp2p.typing import TProtocol
PROTOCOL_ID = TProtocol("/chat/1.0.0")
MAX_READ_LEN = 2 ** 32 - 1
async def read_data(stream: INetStream) -> None:
while True:
read_bytes = await stream.read()
read_bytes = await stream.read(MAX_READ_LEN)
if read_bytes is not None:
read_string = read_bytes.decode()
if read_string != "\n":
@ -24,7 +25,6 @@ async def read_data(stream: INetStream) -> None:
print("\x1b[32m %s\x1b[0m " % read_string, end="")
# FIXME(mhchia): Reconsider whether we should use a thread pool here.
async def write_data(stream: INetStream) -> None:
loop = asyncio.get_event_loop()
while True:

View File

@ -14,6 +14,7 @@ PROTOCOL_ID = TProtocol("/echo/1.0.0")
async def _echo_stream_handler(stream: INetStream) -> None:
# Wait until EOF
msg = await stream.read()
await stream.write(msg)
await stream.close()
@ -72,13 +73,13 @@ async def run(port: int, destination: str, localhost: bool, seed: int = None) ->
msg = b"hi, there!\n"
await stream.write(msg)
# Notify the other side about EOF
await stream.close()
response = await stream.read()
print(f"Sent: {msg}")
print(f"Got: {response}")
await stream.close()
def main() -> None:
description = """