@@ -7,7 +7,7 @@ use futures::channel::{
7
7
mpsc:: { self , UnboundedSender } ,
8
8
oneshot,
9
9
} ;
10
- use std:: { collections:: HashSet , io , sync:: Arc } ;
10
+ use std:: { collections:: HashSet , sync:: Arc } ;
11
11
12
12
#[ cfg( any( target_os = "linux" , target_os = "macos" ) ) ]
13
13
use futures:: stream:: Stream ;
@@ -41,12 +41,6 @@ pub enum Error {
41
41
/// Platform specific error occurred
42
42
#[ error( "Internal route manager error" ) ]
43
43
PlatformError ( #[ from] imp:: Error ) ,
44
- /// Failed to spawn route manager future
45
- #[ error( "Failed to spawn route manager on the provided executor" ) ]
46
- FailedToSpawnManager ,
47
- /// Failed to spawn route manager runtime
48
- #[ error( "Failed to spawn route manager runtime" ) ]
49
- FailedToSpawnRuntime ( #[ from] io:: Error ) ,
50
44
/// Attempt to use route manager that has been dropped
51
45
#[ error( "Cannot send message to route manager since it is down" ) ]
52
46
RouteManagerDown ,
@@ -257,7 +251,6 @@ pub enum CallbackMessage {
257
251
/// the route will be adjusted dynamically when the default route changes.
258
252
pub struct RouteManager {
259
253
manage_tx : Option < Arc < UnboundedSender < RouteManagerCommand > > > ,
260
- runtime : tokio:: runtime:: Handle ,
261
254
}
262
255
263
256
impl RouteManager {
@@ -280,7 +273,6 @@ impl RouteManager {
280
273
tokio:: spawn ( manager. run ( manage_rx) ) ;
281
274
282
275
Ok ( Self {
283
- runtime : tokio:: runtime:: Handle :: current ( ) ,
284
276
manage_tx : Some ( manage_tx) ,
285
277
} )
286
278
}
@@ -289,77 +281,59 @@ impl RouteManager {
289
281
pub async fn stop ( & mut self ) {
290
282
if let Some ( tx) = self . manage_tx . take ( ) {
291
283
let ( wait_tx, wait_rx) = oneshot:: channel ( ) ;
292
-
293
- if tx
294
- . unbounded_send ( RouteManagerCommand :: Shutdown ( wait_tx) )
295
- . is_err ( )
296
- {
297
- log:: error!( "RouteManager already down!" ) ;
298
- return ;
299
- }
300
-
301
- if wait_rx. await . is_err ( ) {
302
- log:: error!( "{}" , Error :: ManagerChannelDown ) ;
303
- }
284
+ let _ = tx. unbounded_send ( RouteManagerCommand :: Shutdown ( wait_tx) ) ;
285
+ let _ = wait_rx. await ;
304
286
}
305
287
}
306
288
307
289
/// Applies the given routes until [`RouteManager::stop`] is called.
308
- pub async fn add_routes ( & mut self , routes : HashSet < RequiredRoute > ) -> Result < ( ) , Error > {
309
- if let Some ( tx) = & self . manage_tx {
310
- let ( result_tx, result_rx) = oneshot:: channel ( ) ;
311
- if tx
312
- . unbounded_send ( RouteManagerCommand :: AddRoutes ( routes, result_tx) )
313
- . is_err ( )
314
- {
315
- return Err ( Error :: RouteManagerDown ) ;
316
- }
317
-
318
- result_rx
319
- . await
320
- . map_err ( |_| Error :: ManagerChannelDown ) ?
321
- . map_err ( Error :: PlatformError )
322
- } else {
323
- Err ( Error :: RouteManagerDown )
324
- }
290
+ pub async fn add_routes ( & self , routes : HashSet < RequiredRoute > ) -> Result < ( ) , Error > {
291
+ let tx = self . get_command_tx ( ) ?;
292
+ let ( result_tx, result_rx) = oneshot:: channel ( ) ;
293
+ tx. unbounded_send ( RouteManagerCommand :: AddRoutes ( routes, result_tx) )
294
+ . map_err ( |_| Error :: RouteManagerDown ) ?;
295
+
296
+ result_rx
297
+ . await
298
+ . map_err ( |_| Error :: ManagerChannelDown ) ?
299
+ . map_err ( Error :: PlatformError )
325
300
}
326
301
327
302
/// Removes all routes previously applied in [`RouteManager::add_routes`].
328
- pub fn clear_routes ( & mut self ) -> Result < ( ) , Error > {
329
- if let Some ( tx) = & self . manage_tx {
330
- if tx. unbounded_send ( RouteManagerCommand :: ClearRoutes ) . is_err ( ) {
331
- return Err ( Error :: RouteManagerDown ) ;
332
- }
333
- Ok ( ( ) )
334
- } else {
335
- Err ( Error :: RouteManagerDown )
336
- }
303
+ pub fn clear_routes ( & self ) -> Result < ( ) , Error > {
304
+ let tx = self . get_command_tx ( ) ?;
305
+ tx. unbounded_send ( RouteManagerCommand :: ClearRoutes )
306
+ . map_err ( |_| Error :: RouteManagerDown )
337
307
}
338
308
339
309
/// Ensure that packets are routed using the correct tables.
340
310
#[ cfg( target_os = "linux" ) ]
341
- pub async fn create_routing_rules ( & mut self , enable_ipv6 : bool ) -> Result < ( ) , Error > {
311
+ pub async fn create_routing_rules ( & self , enable_ipv6 : bool ) -> Result < ( ) , Error > {
342
312
self . handle ( ) ?. create_routing_rules ( enable_ipv6) . await
343
313
}
344
314
345
315
/// Remove any routing rules created by [Self::create_routing_rules].
346
316
#[ cfg( target_os = "linux" ) ]
347
- pub async fn clear_routing_rules ( & mut self ) -> Result < ( ) , Error > {
317
+ pub async fn clear_routing_rules ( & self ) -> Result < ( ) , Error > {
348
318
self . handle ( ) ?. clear_routing_rules ( ) . await
349
319
}
350
320
351
321
/// Retrieve a sender directly to the command channel.
352
322
pub fn handle ( & self ) -> Result < RouteManagerHandle , Error > {
353
- if let Some ( tx) = & self . manage_tx {
354
- Ok ( RouteManagerHandle { tx : tx. clone ( ) } )
355
- } else {
356
- Err ( Error :: RouteManagerDown )
357
- }
323
+ let tx = self . get_command_tx ( ) ?;
324
+ Ok ( RouteManagerHandle { tx : tx. clone ( ) } )
325
+ }
326
+
327
+ fn get_command_tx ( & self ) -> Result < & Arc < UnboundedSender < RouteManagerCommand > > , Error > {
328
+ self . manage_tx . as_ref ( ) . ok_or ( Error :: RouteManagerDown )
358
329
}
359
330
}
360
331
361
332
impl Drop for RouteManager {
362
333
fn drop ( & mut self ) {
363
- self . runtime . clone ( ) . block_on ( self . stop ( ) ) ;
334
+ if let Some ( tx) = self . manage_tx . take ( ) {
335
+ let ( done_tx, _) = oneshot:: channel ( ) ;
336
+ let _ = tx. unbounded_send ( RouteManagerCommand :: Shutdown ( done_tx) ) ;
337
+ }
364
338
}
365
339
}
0 commit comments