2019-12-06 17:06:37 +08:00
|
|
|
from typing import TYPE_CHECKING, Set, Tuple
|
2019-09-12 00:38:12 +08:00
|
|
|
|
2019-11-26 19:24:30 +08:00
|
|
|
from async_service import Service
|
2019-12-01 16:26:16 +08:00
|
|
|
import trio
|
2019-11-26 19:24:30 +08:00
|
|
|
|
2019-09-12 00:38:12 +08:00
|
|
|
from libp2p.network.connection.net_connection_interface import INetConn
|
|
|
|
from libp2p.network.stream.net_stream import NetStream
|
|
|
|
from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream
|
2019-09-13 15:29:24 +08:00
|
|
|
from libp2p.stream_muxer.exceptions import MuxedConnUnavailable
|
2019-09-12 00:38:12 +08:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from libp2p.network.swarm import Swarm # noqa: F401
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
Reference: https://github.com/libp2p/go-libp2p-swarm/blob/04c86bbdafd390651cb2ee14e334f7caeedad722/swarm_conn.go # noqa: E501
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-11-26 19:24:30 +08:00
|
|
|
class SwarmConn(INetConn, Service):
|
2019-09-23 15:46:50 +08:00
|
|
|
muxed_conn: IMuxedConn
|
2019-09-12 00:38:12 +08:00
|
|
|
swarm: "Swarm"
|
|
|
|
streams: Set[NetStream]
|
2019-11-26 19:24:30 +08:00
|
|
|
event_closed: trio.Event
|
2019-09-12 00:38:12 +08:00
|
|
|
|
2019-09-23 15:46:50 +08:00
|
|
|
def __init__(self, muxed_conn: IMuxedConn, swarm: "Swarm") -> None:
|
|
|
|
self.muxed_conn = muxed_conn
|
2019-09-12 00:38:12 +08:00
|
|
|
self.swarm = swarm
|
|
|
|
self.streams = set()
|
2019-11-26 19:24:30 +08:00
|
|
|
self.event_closed = trio.Event()
|
2019-09-12 00:38:12 +08:00
|
|
|
|
2019-12-17 15:50:55 +08:00
|
|
|
@property
|
|
|
|
def is_closed(self) -> bool:
|
|
|
|
return self.event_closed.is_set()
|
|
|
|
|
2019-09-12 00:38:12 +08:00
|
|
|
async def close(self) -> None:
|
|
|
|
if self.event_closed.is_set():
|
|
|
|
return
|
|
|
|
self.event_closed.set()
|
2019-12-17 15:50:55 +08:00
|
|
|
await self._cleanup()
|
|
|
|
# Cancel service
|
|
|
|
await self.manager.stop()
|
|
|
|
|
|
|
|
async def _cleanup(self) -> None:
|
2019-09-13 15:29:24 +08:00
|
|
|
self.swarm.remove_conn(self)
|
|
|
|
|
2019-09-23 15:46:50 +08:00
|
|
|
await self.muxed_conn.close()
|
2019-09-13 15:29:24 +08:00
|
|
|
|
|
|
|
# This is just for cleaning up state. The connection has already been closed.
|
|
|
|
# We *could* optimize this but it really isn't worth it.
|
|
|
|
for stream in self.streams:
|
|
|
|
await stream.reset()
|
2019-11-04 18:27:31 +08:00
|
|
|
# Force context switch for stream handlers to process the stream reset event we just emit
|
|
|
|
# before we cancel the stream handler tasks.
|
2019-11-26 19:24:30 +08:00
|
|
|
await trio.sleep(0.1)
|
2019-09-13 15:29:24 +08:00
|
|
|
|
2019-11-26 19:24:30 +08:00
|
|
|
await self._notify_disconnected()
|
2019-09-12 00:38:12 +08:00
|
|
|
|
|
|
|
async def _handle_new_streams(self) -> None:
|
2019-11-29 19:09:56 +08:00
|
|
|
while self.manager.is_running:
|
2019-09-13 15:29:24 +08:00
|
|
|
try:
|
2019-09-23 15:46:50 +08:00
|
|
|
stream = await self.muxed_conn.accept_stream()
|
2019-12-17 15:50:55 +08:00
|
|
|
# Asynchronously handle the accepted stream, to avoid blocking the next stream.
|
2019-09-13 15:29:24 +08:00
|
|
|
except MuxedConnUnavailable:
|
|
|
|
break
|
2019-11-26 19:24:30 +08:00
|
|
|
self.manager.run_task(self._handle_muxed_stream, stream)
|
2019-09-12 17:06:10 +08:00
|
|
|
|
2019-09-12 00:38:12 +08:00
|
|
|
await self.close()
|
|
|
|
|
2019-09-12 17:06:10 +08:00
|
|
|
async def _handle_muxed_stream(self, muxed_stream: IMuxedStream) -> None:
|
2019-11-26 19:24:30 +08:00
|
|
|
net_stream = await self._add_stream(muxed_stream)
|
2019-09-12 17:06:10 +08:00
|
|
|
if self.swarm.common_stream_handler is not None:
|
2019-12-17 15:50:55 +08:00
|
|
|
try:
|
|
|
|
await self.swarm.common_stream_handler(net_stream)
|
|
|
|
# TODO: More exact exceptions
|
|
|
|
except Exception:
|
|
|
|
# TODO: Emit logs.
|
|
|
|
# TODO: Clean up and remove the stream from SwarmConn if there is anything wrong.
|
|
|
|
self.remove_stream(net_stream)
|
2019-09-12 17:06:10 +08:00
|
|
|
|
2019-11-26 19:24:30 +08:00
|
|
|
async def _add_stream(self, muxed_stream: IMuxedStream) -> NetStream:
|
2019-09-12 00:38:12 +08:00
|
|
|
net_stream = NetStream(muxed_stream)
|
2019-09-13 15:29:24 +08:00
|
|
|
self.streams.add(net_stream)
|
2019-11-26 19:24:30 +08:00
|
|
|
await self.swarm.notify_opened_stream(net_stream)
|
2019-09-12 00:38:12 +08:00
|
|
|
return net_stream
|
|
|
|
|
2019-11-26 19:24:30 +08:00
|
|
|
async def _notify_disconnected(self) -> None:
|
|
|
|
await self.swarm.notify_disconnected(self)
|
2019-09-12 00:38:12 +08:00
|
|
|
|
2019-11-26 19:24:30 +08:00
|
|
|
async def run(self) -> None:
|
2019-12-17 15:50:55 +08:00
|
|
|
self.manager.run_task(self._handle_new_streams)
|
|
|
|
await self.manager.wait_finished()
|
2019-09-12 00:38:12 +08:00
|
|
|
|
|
|
|
async def new_stream(self) -> NetStream:
|
2019-09-23 15:46:50 +08:00
|
|
|
muxed_stream = await self.muxed_conn.open_stream()
|
2019-11-26 19:24:30 +08:00
|
|
|
return await self._add_stream(muxed_stream)
|
2019-09-12 00:38:12 +08:00
|
|
|
|
|
|
|
async def get_streams(self) -> Tuple[NetStream, ...]:
|
|
|
|
return tuple(self.streams)
|
2019-09-17 23:38:11 +08:00
|
|
|
|
|
|
|
def remove_stream(self, stream: NetStream) -> None:
|
2019-09-18 15:44:45 +08:00
|
|
|
if stream not in self.streams:
|
|
|
|
return
|
2019-09-17 23:38:11 +08:00
|
|
|
self.streams.remove(stream)
|