Python#

pw_transport: Asynchronous datagram-oriented transport interface

The pw_transport Python package (//pw_transport/py:pw_transport) defines synchronous, thread-safe, datagram-oriented socket and listener interfaces matching the C++ pw::transport module:

Outgoing connections are opened with plain functions (such as pw_transport.framed_tcp.connect()) returning a Socket or ReliableSocket.

Usage#

In-memory forwarding sockets#

pw_transport.forwarding_socket.socket_pair() (//pw_transport/py:forwarding_socket) returns a connected pair of in-memory ReliableSocket endpoints, mirroring ForwardingSocketPair:

from pw_transport.forwarding_socket import socket_pair

client, server = socket_pair(queue_depth=4, max_message_size=1024)
with client, server:
    client.write(b'ping')
    assert server.read() == b'ping'

Framed TCP#

pw_transport.framed_tcp (//pw_transport/py:framed_tcp) frames datagrams over TCP with a 4-byte little-endian length prefix to interoperate with pw::transport::FramedTcpListener and pw::transport::FramedTcpConnector. Like the C++ implementation, it is intended for host tests and tools in upstream Pigweed, so its Bazel target is not visible to other projects.

from pw_transport import framed_tcp
from pw_transport.framed_tcp import FramedTcpListener

with FramedTcpListener(host='127.0.0.1') as listener:
    with (
        framed_tcp.connect('127.0.0.1', listener.port) as client,
        listener.accept() as server,
    ):
        client.write(b'hello')
        assert server.read() == b'hello'

Implementing a custom transport#

Protocol implementations (such as pw_rpc2) depend only on //pw_transport/py:pw_transport. Custom host transports subclass pw_transport.ReliableSocket (or pw_transport.Socket) and optionally provide a connect(...) function or Listener subclass:

import pw_transport


class CustomReliableSocket(pw_transport.ReliableSocket):
    @property
    def max_read_message_size(self) -> int | None:
        return 1024

    @property
    def max_write_message_size(self) -> int | None:
        return 512

    @property
    def closed(self) -> bool: ...

    def _read(self, deadline: pw_transport.Deadline) -> bytes:
        # Block until the next framed datagram arrives, or raise
        # TimeoutError / SocketClosed.
        ...

    def _write(self, data: bytes, deadline: pw_transport.Deadline) -> None:
        # Transmit one whole datagram, or raise TimeoutError /
        # SocketClosed.
        ...

    def close(self) -> None:
        # Close the link and unblock pending read() and write() calls.
        ...

Callers use the public read(), write(), and accept() methods, which validate their arguments and then call the _read(), _write(), and _accept() methods that implementations override. Implementations never see an invalid timeout or an outgoing datagram larger than max_write_message_size.

Timeouts are passed to implementations as a pw_transport.Deadline, which reports whether the operation should give up and how long to wait otherwise.

API reference#

pw_transport#

Datagram-oriented transport interfaces for Pigweed.

class pw_transport.Socket#

A bidirectional datagram-oriented connection to a peer.

This is the Python counterpart of pw::transport::Socket. Every datagram is delivered whole without merging or splitting; framing is the transport’s responsibility. Unlike ReliableSocket, a basic Socket may drop, duplicate, or reorder datagrams if the underlying link is unreliable.

Zero-length datagrams (b'') are valid in both directions and are distinct from closure, which raises SocketClosed.

Only one thread may call read() at a time, while any number of threads may call write() concurrently. close() may be called from any thread and unblocks pending read() and write() calls.

Implementations override _read() and _write() rather than read() and write(), which validate their arguments before calling into the implementation.

abstract _read(deadline: Deadline) → bytes#

Implements read(), which documents the required behavior.

Parameters:

deadline – When to give up and raise TimeoutError.

abstract _write(data: bytes, deadline: Deadline) → None#

Implements write(), which documents the required behavior.

Parameters:
  • data – Outgoing datagram payload, which is no larger than max_write_message_size.

  • deadline – When to give up and raise TimeoutError.

abstract close() → None#

Closes the socket and unblocks pending operations.

Calling close() on an already-closed socket is a no-op.

abstract property closed: bool#

True if the socket is closed.

property max_read_message_size: int | None#

Largest datagram that can be read from this socket, in bytes.

Returns None if there is no limit.

property max_write_message_size: int | None#

Largest datagram that can be written to this socket, in bytes.

Returns None if there is no limit. write() rejects larger datagrams with ValueError.

final read(timeout: float | None = None) → bytes#

Blocks until one whole datagram is received and returns it.

Only one thread may call read() at a time. If timeout expires, TimeoutError is raised without closing the socket or discarding any in-flight datagram.

Parameters:

timeout – Maximum time to wait in seconds, or None (or math.inf) to wait indefinitely.

Raises:
final write(data: bytes, timeout: float | None = None) → None#

Writes one whole datagram, blocking while backpressure applies.

Parameters:
  • data – Outgoing datagram payload.

  • timeout – Maximum time to wait in seconds while backpressure applies, or None (or math.inf) to wait indefinitely.

Raises:
  • SocketClosed – The socket is closed.

  • TimeoutError – The datagram could not be sent before timeout. Stream-backed transports that have already transmitted part of the frame close the socket before raising TimeoutError.

  • ValueError – data exceeds max_write_message_size, or timeout is negative or NaN.

class pw_transport.ReliableSocket#

A reliable, ordered, datagram-oriented connection to a peer.

This is the Python counterpart of pw::transport::ReliableSocket. Unless the connection fails and raises SocketClosed, datagrams written by one peer are delivered whole, in order, and without duplication.

__init__()#
abstract _read(deadline: Deadline) → bytes#

Implements read(), which documents the required behavior.

Parameters:

deadline – When to give up and raise TimeoutError.

abstract _write(data: bytes, deadline: Deadline) → None#

Implements write(), which documents the required behavior.

Parameters:
  • data – Outgoing datagram payload, which is no larger than max_write_message_size.

  • deadline – When to give up and raise TimeoutError.

abstract close() → None#

Closes the socket and unblocks pending operations.

Calling close() on an already-closed socket is a no-op.

abstract property closed: bool#

True if the socket is closed.

property max_read_message_size: int | None#

Largest datagram that can be read from this socket, in bytes.

Returns None if there is no limit.

property max_write_message_size: int | None#

Largest datagram that can be written to this socket, in bytes.

Returns None if there is no limit. write() rejects larger datagrams with ValueError.

read(timeout: float | None = None) → bytes#

Blocks until one whole datagram is received and returns it.

Only one thread may call read() at a time. If timeout expires, TimeoutError is raised without closing the socket or discarding any in-flight datagram.

Parameters:

timeout – Maximum time to wait in seconds, or None (or math.inf) to wait indefinitely.

Raises:
write(data: bytes, timeout: float | None = None) → None#

Writes one whole datagram, blocking while backpressure applies.

Parameters:
  • data – Outgoing datagram payload.

  • timeout – Maximum time to wait in seconds while backpressure applies, or None (or math.inf) to wait indefinitely.

Raises:
  • SocketClosed – The socket is closed.

  • TimeoutError – The datagram could not be sent before timeout. Stream-backed transports that have already transmitted part of the frame close the socket before raising TimeoutError.

  • ValueError – data exceeds max_write_message_size, or timeout is negative or NaN.

class pw_transport.Listener#

Accepts incoming Socket connections from peers.

This is the Python counterpart of pw::transport::Listener. Closing a listener stops accepting new connections without closing sockets that were already accepted.

Multiple threads may call accept() concurrently; when multiple calls are pending, connections are distributed in an unspecified order. close() may be called from any thread and unblocks pending accept() calls.

Implementations override _accept() rather than accept(), which validates its arguments before calling into the implementation.

abstract _accept(deadline: Deadline) → _SocketT_co#

Implements accept(), which documents the required behavior.

Parameters:

deadline – When to give up and raise TimeoutError.

final accept(timeout: float | None = None) → _SocketT_co#

Blocks until a peer connects and returns the accepted socket.

Multiple threads may call accept() concurrently; the order in which pending calls receive connections is unspecified.

Parameters:

timeout – Maximum time to wait in seconds, or None (or math.inf) to wait indefinitely.

Raises:
abstract close() → None#

Stops listening and unblocks pending accept() calls.

Calling close() on an already-closed listener is a no-op.

abstract property closed: bool#

True if the listener is closed.

class pw_transport.ReliableListener#

Accepts incoming ReliableSocket connections from peers.

This is the Python counterpart of pw::transport::ReliableListener.

__init__()#
abstract _accept(deadline: Deadline) → _SocketT_co#

Implements accept(), which documents the required behavior.

Parameters:

deadline – When to give up and raise TimeoutError.

accept(timeout: float | None = None) → _SocketT_co#

Blocks until a peer connects and returns the accepted socket.

Multiple threads may call accept() concurrently; the order in which pending calls receive connections is unspecified.

Parameters:

timeout – Maximum time to wait in seconds, or None (or math.inf) to wait indefinitely.

Raises:
abstract close() → None#

Stops listening and unblocks pending accept() calls.

Calling close() on an already-closed listener is a no-op.

abstract property closed: bool#

True if the listener is closed.

class pw_transport.Deadline(timeout: float | None = None)#

The point in time at which a blocking operation gives up.

Socket and Listener convert each caller-provided timeout to a Deadline before calling into the implementation, so implementations never handle unvalidated timeouts or compute their own deadlines.

Pass remaining() to blocking calls and raise TimeoutError once the deadline has expired():

while not ready():
    if deadline.expired():
        raise TimeoutError('Timed out')
    condition.wait(deadline.remaining())
__init__(timeout: float | None = None) → None#

Starts a deadline that expires timeout seconds from now.

Parameters:

timeout – Maximum time to wait in seconds, or None (or math.inf) to never expire.

Raises:

ValueError – timeout is negative or NaN.

expired() → bool#

True if the deadline has passed; the operation should give up.

remaining() → float | None#

Returns the time left to wait, for use as a timeout.

Returns:

None if the deadline never expires; otherwise the seconds left until it does, which is at least 0.

exception pw_transport.SocketClosed#

The socket or listener is closed; no more operations can complete.

pw_transport.forwarding_socket#

In-memory connected pairs of ReliableSocket endpoints.

pw_transport.forwarding_socket.socket_pair(
*,
queue_depth: int | None = None,
max_message_size: int | None = None,
) → tuple[ReliableSocket, ReliableSocket]#

Returns a connected (first, second) pair of in-memory sockets.

Writes on one socket are delivered as reads on the other, mirroring pw::transport::ForwardingSocketPair. Closing one socket prevents further writes in both directions while allowing the peer to finish reading datagrams that were already written.

Parameters:
  • queue_depth – Maximum number of unread datagrams buffered in each direction before write() blocks, or None for an unbounded queue. Must be at least 1 when set.

  • max_message_size – Maximum datagram size in bytes in each direction, or None if unlimited.

Raises:

ValueError – queue_depth is less than 1 or max_message_size is negative.

pw_transport.framed_tcp#

Reliable framed datagram transport over TCP.

Each datagram is sent on the wire as a 4-byte little-endian unsigned payload length followed by the payload bytes, matching pw::transport::FramedTcpListener and pw::transport::FramedTcpConnector.

This module is intended for host tests and tools in upstream Pigweed.

pw_transport.framed_tcp.DEFAULT_MAX_MESSAGE_SIZE: Final[int] = 65536#

Default maximum datagram size in bytes, matching pw::transport::FramedTcpOptions::max_message_size_bytes.

class pw_transport.framed_tcp.FramedTcpListener(port: int = 0, *, host: str = '', backlog: int | None = None, max_message_size: int = 65536)#

Accepts incoming ReliableSocket connections.

This is the Python counterpart of pw::transport::FramedTcpListener.

__init__(port: int = 0, *, host: str = '', backlog: int | None = None, max_message_size: int = 65536) → None#

Binds and starts listening on (host, port).

Parameters:
  • port – TCP port to listen on, or 0 to let the OS pick a free port.

  • host – Interface address or hostname to bind to, or '' for all local interfaces. A hostname with both IPv4 and IPv6 addresses, such as localhost, is bound to its IPv4 address only.

  • backlog – Optional listen backlog passed to socket.create_server.

  • max_message_size – Largest datagram to read or write on accepted sockets, in bytes.

Raises:
  • OSError – Binding or listening on (host, port) failed.

  • ValueError – max_message_size is invalid.

accept(timeout: float | None = None) → _SocketT_co#

Blocks until a peer connects and returns the accepted socket.

Multiple threads may call accept() concurrently; the order in which pending calls receive connections is unspecified.

Parameters:

timeout – Maximum time to wait in seconds, or None (or math.inf) to wait indefinitely.

Raises:
close() → None#

Stops listening and unblocks pending accept() calls.

property closed: bool#

True if close() has been called on this listener.

property max_message_size: int#

Largest datagram to read or write on accepted sockets, in bytes.

property port: int#

The local TCP port this listener is bound to.

pw_transport.framed_tcp.connect(
host: str,
port: int,
*,
timeout: float | None = None,
max_message_size: int = 65536,
) → ReliableSocket#

Opens a framed TCP ReliableSocket.

Parameters:
  • host – Hostname or IPv4/IPv6 address of the peer.

  • port – TCP port of the peer.

  • timeout – Maximum time to wait in seconds when establishing the TCP connection, or None (or math.inf) to wait indefinitely. If host resolves to multiple addresses, the timeout applies to each connection attempt.

  • max_message_size – Largest datagram to read or write, in bytes.

Raises:
  • TimeoutError – Establishing the TCP connection timed out.

  • OSError – The host could not be resolved or the connection failed.

  • ValueError – timeout or max_message_size is invalid.