Skip to main content

Module handle

Module handle 

Source
Expand description

Thread-safe handle over a background-task crate::tcp::adapter::Adapter.

AdapterHandle spawns the adapter’s I/O loop onto whatever async executor is current (tokio on native, wasm_bindgen_futures::spawn_local on wasm32-unknown-unknown) and communicates with it through tokio mpsc channels. The resulting StreamHandle is Send + Sync + 'static, enabling use cases that crate::tcp::stream::AdapterStream cannot support:

  • Multiple concurrent connections on the same adapter.
  • Passing a stream to a spawn call or storing it in an Arc<Mutex<…>>.
  • Storing the stream in a struct without a lifetime parameter.
  • FFI or any context that requires 'static bounds.

The trade-off versus crate::tcp::stream::AdapterStream is a small channel round-trip on every write, and the adapter runs in a dedicated background task rather than being driven directly by the caller.

§Background task

AdapterHandle::new immediately spawns a task. The spawned task runs a select! loop that races three branches:

  1. Incoming messages: connect/send/pcap/close requests from callers.
  2. Incoming packets: reads the next frame from the transport and updates connection state.
  3. 1 ms tick: calls write_buffer_flush, which drains pending writes and checks for retransmission timeouts.

The task exits when the last AdapterHandle is dropped or when AdapterHandle::close is called.

Structs§

AdapterHandle
A cloneable, thread-safe handle to a background crate::tcp::adapter::Adapter task.
Reset
The reset of a stream marked with StreamHandle::reset_on_drop.
StreamHandle
A Send + Sync + 'static stream produced by AdapterHandle::connect.
UdpSocketHandle
A bound UDP port on the userspace stack. Connectionless: [recv] yields whole datagrams and [send_to] sends to a peer port. Dropping it unbinds the port.