Skip to content

Minor Windows route manager cleanup #5932

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

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions talpid-routing/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,19 @@ impl RouteManager {
&self,
callback: Callback,
) -> Result<CallbackHandle> {
if let Some(tx) = &self.manage_tx {
let (result_tx, result_rx) = oneshot::channel();
if tx
.unbounded_send(RouteManagerCommand::RegisterDefaultRouteChangeCallback(
callback, result_tx,
))
.is_err()
{
return Err(Error::RouteManagerDown);
}
Ok(result_rx.await.map_err(|_| Error::ManagerChannelDown)?)
} else {
Err(Error::RouteManagerDown)
}
let tx = self.get_command_tx()?;
let (result_tx, result_rx) = oneshot::channel();
tx.unbounded_send(RouteManagerCommand::RegisterDefaultRouteChangeCallback(
callback, result_tx,
))
.map_err(|_| Error::RouteManagerDown)?;
result_rx.await.map_err(|_| Error::ManagerChannelDown)
}

/// Retrieve a sender directly to the command channel.
pub fn handle(&self) -> Result<RouteManagerHandle> {
if let Some(tx) = &self.manage_tx {
Ok(RouteManagerHandle { tx: tx.clone() })
} else {
Err(Error::RouteManagerDown)
}
let tx = self.get_command_tx()?;
Ok(RouteManagerHandle { tx: tx.clone() })
}

async fn listen(
Expand Down Expand Up @@ -262,28 +252,22 @@ impl RouteManager {

/// Applies the given routes until [`RouteManager::stop`] is called.
pub async fn add_routes(&self, routes: HashSet<RequiredRoute>) -> Result<()> {
if let Some(tx) = &self.manage_tx {
let (result_tx, result_rx) = oneshot::channel();
if tx
.unbounded_send(RouteManagerCommand::AddRoutes(routes, result_tx))
.is_err()
{
return Err(Error::RouteManagerDown);
}
result_rx.await.map_err(|_| Error::ManagerChannelDown)?
} else {
Err(Error::RouteManagerDown)
}
let tx = self.get_command_tx()?;
let (result_tx, result_rx) = oneshot::channel();
tx.unbounded_send(RouteManagerCommand::AddRoutes(routes, result_tx))
.map_err(|_| Error::RouteManagerDown)?;
result_rx.await.map_err(|_| Error::ManagerChannelDown)?
}

/// Removes all routes previously applied in [`RouteManager::add_routes`].
pub fn clear_routes(&self) -> Result<()> {
if let Some(tx) = &self.manage_tx {
tx.unbounded_send(RouteManagerCommand::ClearRoutes)
.map_err(|_| Error::RouteManagerDown)
} else {
Err(Error::RouteManagerDown)
}
let tx = self.get_command_tx()?;
tx.unbounded_send(RouteManagerCommand::ClearRoutes)
.map_err(|_| Error::RouteManagerDown)
}

fn get_command_tx(&self) -> Result<&UnboundedSender<RouteManagerCommand>> {
self.manage_tx.as_ref().ok_or(Error::RouteManagerDown)
}
}

Expand Down
Loading