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:
pw_transport.Socketandpw_transport.ReliableSocketread and write whole datagrams over an established connection.pw_transport.Listenerandpw_transport.ReliableListeneraccept incoming connections.
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. UnlikeReliableSocket, a basicSocketmay 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 raisesSocketClosed.Only one thread may call
read()at a time, while any number of threads may callwrite()concurrently.close()may be called from any thread and unblocks pendingread()andwrite()calls.Implementations override
_read()and_write()rather thanread()andwrite(), 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.
- property max_read_message_size: int | None#
Largest datagram that can be read from this socket, in bytes.
Returns
Noneif there is no limit.
- property max_write_message_size: int | None#
Largest datagram that can be written to this socket, in bytes.
Returns
Noneif there is no limit.write()rejects larger datagrams withValueError.
- 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. Iftimeoutexpires,TimeoutErroris raised without closing the socket or discarding any in-flight datagram.- Parameters:
timeout – Maximum time to wait in seconds, or
None(ormath.inf) to wait indefinitely.- Raises:
SocketClosed – The socket is closed.
TimeoutError – No complete datagram arrived before
timeout.RuntimeError – Another
read()is already in progress.ValueError –
timeoutis negative or NaN.
- 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(ormath.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 raisingTimeoutError.ValueError –
dataexceedsmax_write_message_size, ortimeoutis 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 raisesSocketClosed, 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.
- property max_read_message_size: int | None#
Largest datagram that can be read from this socket, in bytes.
Returns
Noneif there is no limit.
- property max_write_message_size: int | None#
Largest datagram that can be written to this socket, in bytes.
Returns
Noneif there is no limit.write()rejects larger datagrams withValueError.
- 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. Iftimeoutexpires,TimeoutErroris raised without closing the socket or discarding any in-flight datagram.- Parameters:
timeout – Maximum time to wait in seconds, or
None(ormath.inf) to wait indefinitely.- Raises:
SocketClosed – The socket is closed.
TimeoutError – No complete datagram arrived before
timeout.RuntimeError – Another
read()is already in progress.ValueError –
timeoutis negative or NaN.
- 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(ormath.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 raisingTimeoutError.ValueError –
dataexceedsmax_write_message_size, ortimeoutis negative or NaN.
- class pw_transport.Listener#
Accepts incoming
Socketconnections 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 pendingaccept()calls.Implementations override
_accept()rather thanaccept(), 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(ormath.inf) to wait indefinitely.- Raises:
SocketClosed – The listener is closed.
TimeoutError – No connection arrived before
timeout.ValueError –
timeoutis negative or NaN.
- class pw_transport.ReliableListener#
Accepts incoming
ReliableSocketconnections 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(ormath.inf) to wait indefinitely.- Raises:
SocketClosed – The listener is closed.
TimeoutError – No connection arrived before
timeout.ValueError –
timeoutis negative or NaN.
- class pw_transport.Deadline(timeout: float | None = None)#
The point in time at which a blocking operation gives up.
SocketandListenerconvert each caller-provided timeout to aDeadlinebefore calling into the implementation, so implementations never handle unvalidated timeouts or compute their own deadlines.Pass
remaining()to blocking calls and raiseTimeoutErroronce the deadline hasexpired():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
timeoutseconds from now.- Parameters:
timeout – Maximum time to wait in seconds, or
None(ormath.inf) to never expire.- Raises:
ValueError –
timeoutis negative or NaN.
- 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( ) 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, orNonefor an unbounded queue. Must be at least 1 when set.max_message_size – Maximum datagram size in bytes in each direction, or
Noneif unlimited.
- Raises:
ValueError –
queue_depthis less than 1 ormax_message_sizeis 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
ReliableSocketconnections.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
0to 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 aslocalhost, 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_sizeis 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(ormath.inf) to wait indefinitely.- Raises:
SocketClosed – The listener is closed.
TimeoutError – No connection arrived before
timeout.ValueError –
timeoutis negative or NaN.
- pw_transport.framed_tcp.connect( ) 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(ormath.inf) to wait indefinitely. Ifhostresolves 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 –
timeoutormax_message_sizeis invalid.