mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
Merge pull request #263 from ralexstokes/remove-stream-from-connection
Encapsulate the concept of a stream to the stream multiplexer
This commit is contained in:
@ -4,32 +4,23 @@ from .raw_connection_interface import IRawConnection
|
|||||||
|
|
||||||
|
|
||||||
class RawConnection(IRawConnection):
|
class RawConnection(IRawConnection):
|
||||||
|
|
||||||
conn_ip: str
|
|
||||||
conn_port: str
|
|
||||||
reader: asyncio.StreamReader
|
reader: asyncio.StreamReader
|
||||||
writer: asyncio.StreamWriter
|
writer: asyncio.StreamWriter
|
||||||
initiator: bool
|
initiator: bool
|
||||||
|
|
||||||
_drain_lock: asyncio.Lock
|
_drain_lock: asyncio.Lock
|
||||||
_next_id: int
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
ip: str,
|
|
||||||
port: str,
|
|
||||||
reader: asyncio.StreamReader,
|
reader: asyncio.StreamReader,
|
||||||
writer: asyncio.StreamWriter,
|
writer: asyncio.StreamWriter,
|
||||||
initiator: bool,
|
initiator: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.conn_ip = ip
|
|
||||||
self.conn_port = port
|
|
||||||
self.reader = reader
|
self.reader = reader
|
||||||
self.writer = writer
|
self.writer = writer
|
||||||
self.initiator = initiator
|
self.initiator = initiator
|
||||||
|
|
||||||
self._drain_lock = asyncio.Lock()
|
self._drain_lock = asyncio.Lock()
|
||||||
self._next_id = 0 if initiator else 1
|
|
||||||
|
|
||||||
async def write(self, data: bytes) -> None:
|
async def write(self, data: bytes) -> None:
|
||||||
self.writer.write(data)
|
self.writer.write(data)
|
||||||
@ -48,12 +39,3 @@ class RawConnection(IRawConnection):
|
|||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
self.writer.close()
|
self.writer.close()
|
||||||
|
|
||||||
def next_stream_id(self) -> int:
|
|
||||||
"""
|
|
||||||
Get next available stream id
|
|
||||||
:return: next available stream id for the connection
|
|
||||||
"""
|
|
||||||
next_id = self._next_id
|
|
||||||
self._next_id += 2
|
|
||||||
return next_id
|
|
||||||
|
|||||||
@ -19,7 +19,3 @@ class IRawConnection(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def next_stream_id(self) -> int:
|
|
||||||
pass
|
|
||||||
|
|||||||
@ -204,13 +204,7 @@ class Swarm(INetwork):
|
|||||||
) -> None:
|
) -> None:
|
||||||
# Upgrade reader/write to a net_stream and pass \
|
# Upgrade reader/write to a net_stream and pass \
|
||||||
# to appropriate stream handler (using multiaddr)
|
# to appropriate stream handler (using multiaddr)
|
||||||
raw_conn = RawConnection(
|
raw_conn = RawConnection(reader, writer, False)
|
||||||
maddr.value_for_protocol("ip4"),
|
|
||||||
maddr.value_for_protocol("tcp"),
|
|
||||||
reader,
|
|
||||||
writer,
|
|
||||||
False,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
|
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
|
||||||
# the conn and then mux the conn
|
# the conn and then mux the conn
|
||||||
|
|||||||
@ -30,10 +30,6 @@ class BaseSession(ISecureConn):
|
|||||||
|
|
||||||
self.initiator = self.conn.initiator
|
self.initiator = self.conn.initiator
|
||||||
|
|
||||||
# TODO clean up how this is passed around?
|
|
||||||
def next_stream_id(self) -> int:
|
|
||||||
return self.conn.next_stream_id()
|
|
||||||
|
|
||||||
async def write(self, data: bytes) -> None:
|
async def write(self, data: bytes) -> None:
|
||||||
await self.conn.write(data)
|
await self.conn.write(data)
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,6 @@ from typing import Dict, Optional, Tuple
|
|||||||
|
|
||||||
from multiaddr import Multiaddr
|
from multiaddr import Multiaddr
|
||||||
|
|
||||||
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
||||||
from libp2p.network.typing import GenericProtocolHandlerFn
|
from libp2p.network.typing import GenericProtocolHandlerFn
|
||||||
from libp2p.peer.id import ID
|
from libp2p.peer.id import ID
|
||||||
from libp2p.security.secure_conn_interface import ISecureConn
|
from libp2p.security.secure_conn_interface import ISecureConn
|
||||||
@ -24,13 +23,13 @@ class Mplex(IMuxedConn):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
secured_conn: ISecureConn
|
secured_conn: ISecureConn
|
||||||
raw_conn: IRawConnection
|
|
||||||
peer_id: ID
|
peer_id: ID
|
||||||
# TODO: `dataIn` in go implementation. Should be size of 8.
|
# TODO: `dataIn` in go implementation. Should be size of 8.
|
||||||
# TODO: Also, `dataIn` is closed indicating EOF in Go. We don't have similar strategies
|
# TODO: Also, `dataIn` is closed indicating EOF in Go. We don't have similar strategies
|
||||||
# to let the `MplexStream`s know that EOF arrived (#235).
|
# to let the `MplexStream`s know that EOF arrived (#235).
|
||||||
buffers: Dict[int, "asyncio.Queue[bytes]"]
|
buffers: Dict[int, "asyncio.Queue[bytes]"]
|
||||||
stream_queue: "asyncio.Queue[int]"
|
stream_queue: "asyncio.Queue[int]"
|
||||||
|
next_stream_id: int
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -47,6 +46,11 @@ class Mplex(IMuxedConn):
|
|||||||
"""
|
"""
|
||||||
self.conn = secured_conn
|
self.conn = secured_conn
|
||||||
|
|
||||||
|
if self.conn.initiator:
|
||||||
|
self.next_stream_id = 0
|
||||||
|
else:
|
||||||
|
self.next_stream_id = 1
|
||||||
|
|
||||||
# Store generic protocol handler
|
# Store generic protocol handler
|
||||||
self.generic_protocol_handler = generic_protocol_handler
|
self.generic_protocol_handler = generic_protocol_handler
|
||||||
|
|
||||||
@ -100,6 +104,15 @@ class Mplex(IMuxedConn):
|
|||||||
return None
|
return None
|
||||||
return await self.buffers[stream_id].get()
|
return await self.buffers[stream_id].get()
|
||||||
|
|
||||||
|
def _get_next_stream_id(self) -> int:
|
||||||
|
"""
|
||||||
|
Get next available stream id
|
||||||
|
:return: next available stream id for the connection
|
||||||
|
"""
|
||||||
|
next_id = self.next_stream_id
|
||||||
|
self.next_stream_id += 2
|
||||||
|
return next_id
|
||||||
|
|
||||||
# FIXME: Remove multiaddr from being passed into muxed_conn
|
# FIXME: Remove multiaddr from being passed into muxed_conn
|
||||||
async def open_stream(
|
async def open_stream(
|
||||||
self, protocol_id: str, multi_addr: Multiaddr
|
self, protocol_id: str, multi_addr: Multiaddr
|
||||||
@ -110,7 +123,7 @@ class Mplex(IMuxedConn):
|
|||||||
:param multi_addr: multi_addr that stream connects to
|
:param multi_addr: multi_addr that stream connects to
|
||||||
:return: a new stream
|
:return: a new stream
|
||||||
"""
|
"""
|
||||||
stream_id = self.conn.next_stream_id()
|
stream_id = self._get_next_stream_id()
|
||||||
stream = MplexStream(stream_id, True, self)
|
stream = MplexStream(stream_id, True, self)
|
||||||
self.buffers[stream_id] = asyncio.Queue()
|
self.buffers[stream_id] = asyncio.Queue()
|
||||||
await self.send_message(HeaderTags.NewStream, None, stream_id)
|
await self.send_message(HeaderTags.NewStream, None, stream_id)
|
||||||
|
|||||||
@ -70,12 +70,12 @@ class TCP(ITransport):
|
|||||||
:param self_id: peer_id of the dialer (to send to receiver)
|
:param self_id: peer_id of the dialer (to send to receiver)
|
||||||
:return: `RawConnection` if successful
|
:return: `RawConnection` if successful
|
||||||
"""
|
"""
|
||||||
host = maddr.value_for_protocol("ip4")
|
self.host = maddr.value_for_protocol("ip4")
|
||||||
port = maddr.value_for_protocol("tcp")
|
self.port = int(maddr.value_for_protocol("tcp"))
|
||||||
|
|
||||||
reader, writer = await asyncio.open_connection(host, int(port))
|
reader, writer = await asyncio.open_connection(self.host, self.port)
|
||||||
|
|
||||||
return RawConnection(host, port, reader, writer, True)
|
return RawConnection(reader, writer, True)
|
||||||
|
|
||||||
def create_listener(self, handler_function: THandler) -> TCPListener:
|
def create_listener(self, handler_function: THandler) -> TCPListener:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user