py-libp2p/libp2p/transport/tcp/tcp.py

105 lines
3.4 KiB
Python
Raw Normal View History

2018-10-27 06:06:56 +08:00
import asyncio
import logging
2019-08-05 11:22:44 +08:00
from socket import socket
from typing import List
2019-08-02 16:32:33 +08:00
from multiaddr import Multiaddr
import trio
from libp2p.io.trio import TrioReadWriteCloser
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
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):
multiaddrs: List[Multiaddr]
server = None
def __init__(self, handler_function: THandler) -> None:
2019-08-02 16:32:33 +08:00
self.multiaddrs = []
self.server = None
self.handler = handler_function
2019-11-19 14:01:12 +08:00
async def listen(self, maddr: Multiaddr, nursery) -> bool:
"""
put listener in listening mode and wait for incoming connections.
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
async def serve_tcp(handler, port, host, task_status=None):
logger.debug("serve_tcp %s %s", host, port)
await trio.serve_tcp(handler, port, host=host, task_status=task_status)
async def handler(stream):
read_write_closer = TrioReadWriteCloser(stream)
await self.handler(read_write_closer)
listeners = await nursery.start(
serve_tcp,
handler,
int(maddr.value_for_protocol("tcp")),
maddr.value_for_protocol("ip4"),
2019-08-02 16:32:33 +08:00
)
2019-11-19 14:01:12 +08:00
socket = listeners[0].socket
2019-08-02 16:32:33 +08:00
self.multiaddrs.append(_multiaddr_from_socket(socket))
return True
def get_addrs(self) -> List[Multiaddr]:
"""
retrieve list of addresses the listener is listening on.
2019-08-02 16:32:33 +08:00
:return: return list of addrs
"""
# TODO check if server is listening
return self.multiaddrs
async def close(self) -> None:
"""close the listener such that no more connections can be open on this
transport instance."""
2019-08-02 16:32:33 +08:00
if self.server is None:
return
2019-08-02 16:32:33 +08:00
self.server.close()
await self.server.wait_closed()
2019-08-02 16:32:33 +08:00
self.server = None
2018-10-27 06:06:56 +08:00
class TCP(ITransport):
async def dial(self, maddr: Multiaddr) -> IRawConnection:
"""
dial a transport to peer listening on multiaddr.
:param maddr: multiaddr of peer
: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-11-19 14:01:12 +08:00
stream = await trio.open_tcp_stream(self.host, self.port)
read_write_closer = TrioReadWriteCloser(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
def create_listener(self, handler_function: THandler) -> TCPListener:
"""
create listener on transport.
:param handler_function: a function called when a new connection is received
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)
def _multiaddr_from_socket(socket: socket) -> Multiaddr:
2019-08-02 16:32:33 +08:00
return Multiaddr("/ip4/%s/tcp/%s" % socket.getsockname())