Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying device vector during server loop #16

Closed
h7x4 opened this issue Jun 19, 2023 · 0 comments · Fixed by #18
Closed

Modifying device vector during server loop #16

h7x4 opened this issue Jun 19, 2023 · 0 comments · Fixed by #18

Comments

@h7x4
Copy link
Collaborator

h7x4 commented Jun 19, 2023

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:

  • The handler function is private, so I'm not able to create my own server loop.
  • The server function spawns threads for each socket without keeping track of them, so it's not trivial to close the threads when removing a usb from the vector. (I haven't found any way to do graceful shutdown in the protocol spec)

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant