libp2p.network.connection package

Submodules

libp2p.network.connection.exceptions module

exception libp2p.network.connection.exceptions.RawConnError

Bases: IOException

libp2p.network.connection.raw_connection module

class libp2p.network.connection.raw_connection.RawConnection(stream: ReadWriteCloser, initiator: bool, connection_type: ConnectionType = ConnectionType.DIRECT, addresses: list[Multiaddr] | None = None)

Bases: IRawConnection

async close() None
get_connection_type() ConnectionType

Get the type of connection (direct, relayed, etc.)

get_remote_address() tuple[str, int] | None

Delegate to the underlying stream’s get_remote_address method.

get_transport_addresses() list[Multiaddr]

Get the actual transport addresses used by this connection.

Returns the real IP/port addresses, not peerstore addresses. For relayed connections, should include /p2p-circuit in the path.

is_initiator: bool
async read(n: int | None = None) bytes

Read up to n bytes from the underlying stream. This call is delegated directly to the underlying self.reader.

Raise RawConnError if the underlying connection breaks. ConnectionClosedError is allowed to propagate directly.

stream: ReadWriteCloser
async write(data: bytes) None

Raise RawConnError if the underlying connection breaks.

ConnectionClosedError (a subclass of IOException) is allowed to propagate directly so that upstream code (e.g. yamux) can catch it by type instead of doing string matching.

libp2p.network.connection.swarm_connection module

class libp2p.network.connection.swarm_connection.SwarmConn(muxed_conn: IMuxedConn, swarm: Swarm, direction: Direction | str = Direction.UNKNOWN)

Bases: INetConn

async close() None
property direction: Direction

Get the connection direction.

Returns

Direction

INBOUND if we accepted the connection, OUTBOUND if we initiated it.

event_closed: Event
get_connection_type() ConnectionType

Get the type of connection (direct, relayed, etc.)

get_streams() tuple[NetStream, ...]

Retrieve all active streams associated with this connection.

Returns:

A tuple containing instances of INetStream.

get_transport_addresses() list[Multiaddr]

Retrieve the actual transport addresses used by this connection.

Returns the real IP/port addresses, not peerstore addresses. For relayed connections, should include /p2p-circuit in the path.

Returns

list[Multiaddr]

A list of multiaddresses used by the transport.

property is_closed: bool

Check if the connection is fully closed.

Returns:

True if the connection is closed, otherwise False.

muxed_conn: IMuxedConn
async new_stream() NetStream

Create a new network stream over the connection.

Returns:

A new instance of INetStream.

remove_stream(stream: NetStream) None
set_resource_scope(scope: Any) None

Set the resource scope for this connection.

set_transport_info(addresses: list[Multiaddr], conn_type: ConnectionType) None

Set the actual transport addresses and connection type.

This should be called during connection establishment with the real transport information.

async start() None
streams: set[NetStream]
swarm: Swarm

Module contents