diff options
| author | David Lönnhager <david.l@mullvad.net> | 2024-03-09 10:56:14 +0100 |
|---|---|---|
| committer | David Lönnhager <dv.lnh.d@gmail.com> | 2024-03-11 13:03:10 +0100 |
| commit | f9e76f86d5842de93bf2fc7e66bd30a6104505c4 (patch) | |
| tree | fd66f8d0e8f830feb9da241f3ea8fa02693d4eaa | |
| parent | 1814c2ac923e46684bdb1abf95641d62b87ff24b (diff) | |
| download | mullvadvpn-f9e76f86d5842de93bf2fc7e66bd30a6104505c4.tar.xz mullvadvpn-f9e76f86d5842de93bf2fc7e66bd30a6104505c4.zip | |
Simplify Windows route monitor
| -rw-r--r-- | talpid-routing/src/windows/mod.rs | 58 |
1 files changed, 21 insertions, 37 deletions
diff --git a/talpid-routing/src/windows/mod.rs b/talpid-routing/src/windows/mod.rs index 055b4c2b68..b469fd5425 100644 --- a/talpid-routing/src/windows/mod.rs +++ b/talpid-routing/src/windows/mod.rs @@ -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( @@ -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) } } |
