Use ReadWriteCloser for conns and streams

This commit is contained in:
mhchia
2019-09-05 23:24:17 +08:00
parent eac159c527
commit 10415cb956
9 changed files with 24 additions and 88 deletions

View File

@ -44,13 +44,12 @@ class NetStream(INetStream):
"""
return await self.muxed_stream.write(data)
async def close(self) -> bool:
async def close(self) -> None:
"""
close stream
:return: true if successful
"""
await self.muxed_stream.close()
return True
async def reset(self) -> bool:
return await self.muxed_stream.reset()

View File

@ -1,10 +1,11 @@
from abc import ABC, abstractmethod
from abc import abstractmethod
from libp2p.io.abc import ReadWriteCloser
from libp2p.stream_muxer.abc import IMuxedConn
from libp2p.typing import TProtocol
class INetStream(ABC):
class INetStream(ReadWriteCloser):
mplex_conn: IMuxedConn
@ -21,28 +22,6 @@ class INetStream(ABC):
:return: true if successful
"""
@abstractmethod
async def read(self, n: int = -1) -> bytes:
"""
reads from the underlying muxed_stream
:param n: number of bytes to read
:return: bytes of input
"""
@abstractmethod
async def write(self, data: bytes) -> int:
"""
write to the underlying muxed_stream
:return: number of bytes written
"""
@abstractmethod
async def close(self) -> bool:
"""
close the underlying muxed stream
:return: true if successful
"""
@abstractmethod
async def reset(self) -> bool:
"""

View File

@ -8,7 +8,7 @@ from libp2p.peer.peerstore import PeerStoreError
from libp2p.peer.peerstore_interface import IPeerStore
from libp2p.protocol_muxer.multiselect import Multiselect
from libp2p.protocol_muxer.multiselect_client import MultiselectClient
from libp2p.protocol_muxer.multiselect_communicator import StreamCommunicator
from libp2p.protocol_muxer.multiselect_communicator import MultiselectCommunicator
from libp2p.routing.interfaces import IPeerRouting
from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream
from libp2p.transport.exceptions import MuxerUpgradeFailure, SecurityUpgradeFailure
@ -161,7 +161,7 @@ class Swarm(INetwork):
# Perform protocol muxing to determine protocol to use
selected_protocol = await self.multiselect_client.select_one_of(
list(protocol_ids), StreamCommunicator(muxed_stream)
list(protocol_ids), MultiselectCommunicator(muxed_stream)
)
# Create a net stream with the selected protocol
@ -294,7 +294,7 @@ def create_generic_protocol_handler(swarm: Swarm) -> GenericProtocolHandlerFn:
async def generic_protocol_handler(muxed_stream: IMuxedStream) -> None:
# Perform protocol muxing to determine protocol to use
protocol, handler = await multiselect.negotiate(
StreamCommunicator(muxed_stream)
MultiselectCommunicator(muxed_stream)
)
net_stream = NetStream(muxed_stream)