跳到主要内容
本页尚未翻译

下面显示的是英文原文。

Network

Network is the link the runner connects over: it opens a stream to a host and a port, and it sleeps on the clock of the runtime the runner runs on.

The stream is anything that reads and writes through embedded-io-async, and a read of it must be cancel-safe: the runner stops waiting for the next packet of the server when a handle sends it work, and picks the read up again afterwards. A device uses EmbassyNetwork (feature embassy): a TCP socket on an embassy-net stack, over a receive buffer and a send buffer that the task of the runner owns. The runner reads a packet from the socket straight into a receive buffer of the broker's memory, exactly as long as the fixed header of the packet says, so a PUBLISH stays where it arrived and the delivery borrows from it. A host name in the address is resolved by the DNS client of the stack, and an IPv4 literal is used as it is. A failure is an EmbassyConnectError: the name did not resolve, or the TCP connection failed.

A compio host, on the heap variant, uses CompioNetwork (feature compio), and MqttBroker::compio starts the runner over it. io_uring reads into a buffer it owns until the read completes, so a task of the connection reads the stream into owned buffers and the client reads out of them. That costs one copy per read, and a read the client stops waiting for loses nothing.

A board with another link, such as a TLS session or the socket of a modem, implements Network for that link; the rustdoc of the trait shows a board's network. Whatever the network, the runner takes it as an argument of MqttRunner::run.

Example​

The runner of a device, over embassy-net.

/// The broker's memory: four deliveries of up to 1 KiB, and eight subscriptions.
static MQTT: ConstStaticCell<MqttState> = ConstStaticCell::new(MqttState::new());

/// The runner owns the connection, and the app starts only while it runs.
#[embassy_executor::task]
async fn mqtt(runner: MqttRunner, stack: Stack<'static>) -> ! {
let mut rx = [0; 1024];
let mut tx = [0; 1024];
runner
.run(EmbassyNetwork::new(stack, &mut rx, &mut tx))
.await
}

/// What the board's `main` does once its network stack is up.
fn start(spawner: Spawner, stack: Stack<'static>) {
let config = MqttConfig::new("192.168.1.10:1883").client_id("valve-1");
let (broker, runner) = MqttBroker::new(MQTT.take(), config);
spawner.spawn(mqtt(runner, stack).expect("the pool holds the runner's task"));
spawner.spawn(app::task(broker).expect("the pool holds the app's task"));
}

A compio host​

The broker starts its runner on the core that connects it.

/// Takes every reading the sensors publish.
#[subscriber(MqttTopic::new("sensors/temp"))]
async fn store(reading: &Reading) -> HandlerOutcome {
let _ = reading.celsius;
HandlerOutcome::ack()
}

#[ruststream::app(runtime = compio, cores = 1)]
fn app() -> impl App {
let config = MqttConfig::new("mqtt://localhost:1883").client_id("collector");
RustStream::new(AppInfo::new("collector", "0.1.0"))
.with_broker(MqttBroker::compio(config), |b| b.include(store))
}