jiegec / usbip

A Rust library to run a USB/IP server
MIT License
255 stars 25 forks source link

Modifying device vector during server loop #16

Closed h7x4 closed 10 months ago

h7x4 commented 1 year ago

Hello!

I'm trying to create a program where I can add and remove devices during execution. But I find this to be difficult to do in a safe manner for several reasons:

I had imagined to create something like the following:

/// Spawn a USB/IP server at `addr` using [TcpListener]
pub async fn customServer(addr: SocketAddr, server: UsbIpServer) {
    let listener = TcpListener::bind(addr).await.expect("bind to addr");

    let server = async move {
        let usbip_server = Arc::new(server);
        loop {
            tokio::select! {
        val = listener.accept() => {
                    match val {
                        Ok((mut socket, _addr)) => {
                            ...
                        }
                        Err(err) => {
                            ...
                        }
                    }
        }

        val = listen_for_devicelist_changes() => {
            // add or remove device
            ...
        }
        }
        }
    };

    server.await
}

Is this something you would consider to be in the scope of the library?