transport module

Transport Abstraction Layer

Defines the abstract base classes for network transport, plus in-memory implementations used for deterministic, socket-free testing.

The three ABCs — TransportConnection, TransportServer, and TransportClient — form the pluggable transport interface. Real implementations (e.g. network.websocket_transport) and test implementations (InMemoryServer / InMemoryClient) both implement these ABCs, allowing protocol and session code to be tested without real I/O.

class network.transport.InMemoryClient(server: InMemoryServer)

Bases: TransportClient

In-memory client that connects to an InMemoryServer.

Parameters:

server (InMemoryServer) – The server to connect to.

async connect() → TransportConnection

Create a paired connection and register with the server.

Returns:

The client-side connection endpoint.

Return type:

TransportConnection

class network.transport.InMemoryServer

Bases: TransportServer

In-memory server for testing. Clients connect via InMemoryClient.

async accept(server_conn: TransportConnection)

Called internally when a client connects.

Parameters:

server_conn (TransportConnection) – The server-side connection endpoint.

on_connection(handler)

Register an async handler called with each new TransportConnection.

Parameters:

handler (callable) – Coroutine function accepting a single TransportConnection argument.

async start()

Start listening for connections.

async stop()

Stop the server and close all active connections.

class network.transport.InMemoryTransportPair

Bases: object

Creates a matched pair of connections linked by queues.

Used internally by InMemoryClient to simulate a network connection within a single process.

static create()

Create two cross-linked _QueueConnection instances.

Returns:

A (conn_a, conn_b) tuple where sending on conn_a delivers to conn_b.recv() and vice versa.

Return type:

tuple[_QueueConnection, _QueueConnection]

class network.transport.TransportClient

Bases: ABC

Connects to a server, producing a TransportConnection.

abstractmethod async connect() → TransportConnection

Open a connection to the server.

Returns:

A connected transport endpoint.

Return type:

TransportConnection

class network.transport.TransportConnection

Bases: ABC

A single endpoint of a bidirectional connection.

Implementations wrap real sockets or in-memory queues.

abstractmethod async close() → None

Close this connection.

abstract property closed: bool

Whether this connection has been closed.

Return type:

bool

abstractmethod async recv() → str

Wait for and return the next message from the remote endpoint.

Returns:

The received message string.

Return type:

str

Raises:

ConnectionError – If the connection is closed or lost.

abstractmethod async send(data: str) → None

Send a message string to the remote endpoint.

Parameters:

data (str) – The message string to send (typically JSON).

Raises:

ConnectionError – If the connection is closed.

class network.transport.TransportServer

Bases: ABC

Accepts incoming connections.

Subclasses must call the registered handler for each new connection.

abstractmethod on_connection(handler) → None

Register an async handler called with each new TransportConnection.

Parameters:

handler (callable) – Coroutine function accepting a single TransportConnection argument.

abstractmethod async start() → None

Start listening for connections.

abstractmethod async stop() → None

Stop the server and close all active connections.