qrpc 0.1.2

qrpc is a small QUIC + mTLS messaging library
Documentation
use std::time::Duration;

use qrpc::{Ctx, QrpcInstance, QrpcResult, State};

#[path = "common/demo_support.rs"]
mod demo_support;

use demo_support::DemoEnvelope;

#[tokio::main]
async fn main() -> QrpcResult<()> {
    let instance =
        QrpcInstance::<(), DemoEnvelope, _>::builder(
            |_state: State<()>,
             _ctx: Ctx<DemoEnvelope>,
             source_peer_id: String,
             msg: DemoEnvelope| async move {
                println!("[20001] recv from={} msg={msg:?}", source_peer_id);
                Ok(())
            },
        )
            .with_id("node-20001")
            .with_ca_cert("tests/certs/ca.crt")
            .with_identity("tests/certs/server.crt", "tests/certs/server.key")
            .with_port(20001)
            .build()?;

    instance.start().await;
    println!("[20001] started, listening on 20001");

    let start = tokio::time::Instant::now();
    loop {
        let peers = instance.peer_ids().await;
        if peers.iter().any(|p| p == "node-20002") {
            break;
        }
        if start.elapsed() > Duration::from_secs(60) {
            println!("[20001] waiting for node-20002 timeout, keep serving...");
            break;
        }
        tokio::time::sleep(Duration::from_millis(200)).await;
    }

    let _ = instance
        .send_to("node-20002", &DemoEnvelope::pong("pong from 20001"))
        .await;

    println!("[20001] ready; press Ctrl+C to stop");
    tokio::signal::ctrl_c()
        .await
        .map_err(|e| qrpc::QrpcError::MessageDecode(format!("ctrl_c error: {e}")))?;

    instance.shutdown().await;
    Ok(())
}