libp2p.io package
Submodules
libp2p.io.abc module
- class libp2p.io.abc.EncryptedMsgReadWriter(conn: Any | None = None)
Bases:
MsgReadWriteCloser,EncrypterRead/write message with encryption/decryption.
- class libp2p.io.abc.ReadWriteCloser
libp2p.io.exceptions module
- exception libp2p.io.exceptions.ConnectionClosedError(message: str, close_code: int | None = None, close_reason: str = '', transport: str = '')
Bases:
IOExceptionRaised when a connection is closed by the peer.
Carries structured close information so that upstream code can make decisions based on exception type (
except ConnectionClosedError) rather than fragile string matching on the message.
- exception libp2p.io.exceptions.DecryptionFailedException
Bases:
MsgioException
- exception libp2p.io.exceptions.IOException
Bases:
BaseLibp2pError
- exception libp2p.io.exceptions.IncompleteReadError(message: str, expected_bytes: int = 0, received_bytes: int = 0)
Bases:
IOExceptionFewer bytes were read than requested.
- exception libp2p.io.exceptions.MessageTooLarge
Bases:
MsgioException
- exception libp2p.io.exceptions.MissingLengthException
Bases:
MsgioException
- exception libp2p.io.exceptions.MissingMessageException
Bases:
MsgioException
- exception libp2p.io.exceptions.MsgioException
Bases:
IOException
libp2p.io.msgio module
msgio is an implementation of https://github.com/libp2p/go-msgio.
from that repo: “a simple package to r/w length-delimited slices.”
NOTE: currently missing the capability to indicate lengths by “varint” method.
- class libp2p.io.msgio.BaseMsgReadWriter(read_write_closer: ReadWriteCloser)
Bases:
MsgReadWriteCloser- read_write_closer: ReadWriteCloser
- class libp2p.io.msgio.FixedSizeLenMsgReadWriter(read_write_closer: ReadWriteCloser)
Bases:
BaseMsgReadWriter
- class libp2p.io.msgio.VarIntLengthMsgReadWriter(read_write_closer: ReadWriteCloser)
Bases:
BaseMsgReadWriter
libp2p.io.trio module
- class libp2p.io.trio.TrioTCPStream(stream: SocketStream)
Bases:
ReadWriteCloser- get_remote_address() tuple[str, int] | None
Return the remote address as (host, port) tuple.
This method caches the remote address on first successful retrieval to handle cases where the socket might become unavailable later (e.g., during connection teardown or in certain error states).
- Returns:
A tuple of (host, port) or None if the address cannot be determined.
- read_lock: Lock
- stream: SocketStream
- write_lock: Lock
libp2p.io.utils module
- async libp2p.io.utils.read_exactly(reader: Reader, n: int, retry_count: int = 100) bytes
Read exactly n bytes from the reader.
This function attempts to read exactly n bytes from the reader, retrying up to retry_count times if partial data is received. If the connection closes before n bytes are received, raises IncompleteReadError with enhanced error messages that include transport context (if available).
The error message includes:
The expected and actual number of bytes received
Transport type and connection duration (if reader provides conn_state())
Context about possible causes (peer closure, network issues, transport-specific problems)
- Args:
reader: The reader to read from n: Number of bytes to read retry_count: Maximum number of retries if partial data is received
- Returns:
bytes: Exactly n bytes of data
- Raises:
- IncompleteReadError: If the connection closes before n bytes are received.
The error message includes transport context if available via reader.conn_state().
Note
Relies on exceptions to break out on erroneous conditions, like EOF.