2019-11-19 18:04:48 +08:00
|
|
|
import logging
|
2019-12-06 17:06:37 +08:00
|
|
|
from typing import Awaitable, Callable, List, Sequence, Tuple
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-08-02 16:32:33 +08:00
|
|
|
from multiaddr import Multiaddr
|
2019-11-19 18:04:48 +08:00
|
|
|
import trio
|
2019-12-06 17:06:37 +08:00
|
|
|
from trio_typing import TaskStatus
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-12-06 17:06:37 +08:00
|
|
|
from libp2p.io.trio import TrioTCPStream
|
2019-01-10 02:38:56 +08:00
|
|
|
from libp2p.network.connection.raw_connection import RawConnection
|
2019-08-05 11:22:44 +08:00
|
|
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
2019-09-19 21:23:35 +08:00
|
|
|
from libp2p.transport.exceptions import OpenConnectionError
|
2019-08-02 16:32:33 +08:00
|
|
|
from libp2p.transport.listener_interface import IListener
|
|
|
|
from libp2p.transport.transport_interface import ITransport
|
2019-08-05 11:22:44 +08:00
|
|
|
from libp2p.transport.typing import THandler
|
2019-11-19 14:01:12 +08:00
|
|
|
|
|
|
|
logger = logging.getLogger("libp2p.transport.tcp")
|
2019-08-02 16:32:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TCPListener(IListener):
|
2019-12-17 15:50:55 +08:00
|
|
|
listeners: List[trio.SocketListener]
|
2019-08-02 16:32:33 +08:00
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
def __init__(self, handler_function: THandler) -> None:
|
2019-12-17 15:50:55 +08:00
|
|
|
self.listeners = []
|
2019-08-02 16:32:33 +08:00
|
|
|
self.handler = handler_function
|
|
|
|
|
2019-12-06 17:06:37 +08:00
|
|
|
# TODO: Get rid of `nursery`?
|
2019-12-01 16:26:16 +08:00
|
|
|
async def listen(self, maddr: Multiaddr, nursery: trio.Nursery) -> None:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
put listener in listening mode and wait for incoming connections.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-08-02 16:32:33 +08:00
|
|
|
:param maddr: maddr of peer
|
|
|
|
:return: return True if successful
|
|
|
|
"""
|
2019-11-19 14:01:12 +08:00
|
|
|
|
2019-12-06 17:06:37 +08:00
|
|
|
async def serve_tcp(
|
|
|
|
handler: Callable[[trio.SocketStream], Awaitable[None]],
|
|
|
|
port: int,
|
|
|
|
host: str,
|
|
|
|
task_status: TaskStatus[Sequence[trio.SocketListener]] = None,
|
|
|
|
) -> None:
|
2019-11-19 14:01:12 +08:00
|
|
|
logger.debug("serve_tcp %s %s", host, port)
|
|
|
|
await trio.serve_tcp(handler, port, host=host, task_status=task_status)
|
|
|
|
|
2019-12-06 17:06:37 +08:00
|
|
|
async def handler(stream: trio.SocketStream) -> None:
|
|
|
|
tcp_stream = TrioTCPStream(stream)
|
|
|
|
await self.handler(tcp_stream)
|
2019-11-19 14:01:12 +08:00
|
|
|
|
|
|
|
listeners = await nursery.start(
|
|
|
|
serve_tcp,
|
2019-11-19 18:04:48 +08:00
|
|
|
handler,
|
|
|
|
int(maddr.value_for_protocol("tcp")),
|
|
|
|
maddr.value_for_protocol("ip4"),
|
2019-08-02 16:32:33 +08:00
|
|
|
)
|
2019-12-17 15:50:55 +08:00
|
|
|
self.listeners.extend(listeners)
|
2019-08-02 16:32:33 +08:00
|
|
|
|
2019-12-06 17:06:37 +08:00
|
|
|
def get_addrs(self) -> Tuple[Multiaddr, ...]:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
retrieve list of addresses the listener is listening on.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-08-02 16:32:33 +08:00
|
|
|
:return: return list of addrs
|
|
|
|
"""
|
2019-12-17 15:50:55 +08:00
|
|
|
return tuple(
|
|
|
|
_multiaddr_from_socket(listener.socket) for listener in self.listeners
|
|
|
|
)
|
|
|
|
|
|
|
|
async def close(self) -> None:
|
|
|
|
async with trio.open_nursery() as nursery:
|
|
|
|
for listener in self.listeners:
|
|
|
|
nursery.start_soon(listener.aclose)
|
2019-08-02 16:32:33 +08:00
|
|
|
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
class TCP(ITransport):
|
2019-09-11 09:01:03 +08:00
|
|
|
async def dial(self, maddr: Multiaddr) -> IRawConnection:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
dial a transport to peer listening on multiaddr.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-07-10 19:33:38 +08:00
|
|
|
:param maddr: multiaddr of peer
|
2019-08-16 14:12:10 +08:00
|
|
|
:return: `RawConnection` if successful
|
2019-09-19 22:19:36 +08:00
|
|
|
:raise OpenConnectionError: raised when failed to open connection
|
2018-10-27 06:06:56 +08:00
|
|
|
"""
|
2019-08-25 03:02:30 +08:00
|
|
|
self.host = maddr.value_for_protocol("ip4")
|
|
|
|
self.port = int(maddr.value_for_protocol("tcp"))
|
2018-11-13 00:00:43 +08:00
|
|
|
|
2019-09-19 21:23:35 +08:00
|
|
|
try:
|
2019-12-24 02:19:43 +08:00
|
|
|
stream = await trio.open_tcp_stream(self.host, self.port)
|
|
|
|
except OSError as error:
|
|
|
|
raise OpenConnectionError from error
|
2019-12-06 17:06:37 +08:00
|
|
|
read_write_closer = TrioTCPStream(stream)
|
2018-11-13 00:00:43 +08:00
|
|
|
|
2019-11-19 14:01:12 +08:00
|
|
|
return RawConnection(read_write_closer, True)
|
2018-10-27 06:06:56 +08:00
|
|
|
|
2019-08-05 10:20:30 +08:00
|
|
|
def create_listener(self, handler_function: THandler) -> TCPListener:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
create listener on transport.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
:param handler_function: a function called when a new connection is received
|
2019-10-24 14:41:10 +08:00
|
|
|
that takes a connection as argument which implements interface-connection
|
2018-10-27 06:06:56 +08:00
|
|
|
:return: a listener object that implements listener_interface.py
|
|
|
|
"""
|
2019-08-02 16:32:33 +08:00
|
|
|
return TCPListener(handler_function)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
|
|
|
|
2019-12-06 17:06:37 +08:00
|
|
|
def _multiaddr_from_socket(socket: trio.socket.SocketType) -> Multiaddr:
|
|
|
|
ip, port = socket.getsockname() # type: ignore
|
|
|
|
return Multiaddr(f"/ip4/{ip}/tcp/{port}")
|