[mio] 路由为空则回收数据

This commit is contained in:
lubeilin
2024-03-03 19:23:34 +08:00
parent 590721a4da
commit f0139c68f7
2 changed files with 22 additions and 4 deletions
+21 -3
View File
@@ -256,6 +256,17 @@ impl ContextInner {
Ok(())
}
}
pub fn remove_route(&self, ip: &Ipv4Addr, route_key: RouteKey) {
if self.route_table.remove_route(ip, route_key) {
if route_key.is_tcp {
if let Some(tcp) = self.tcp_map.write().remove(&route_key.addr) {
if let Err(e) = tcp.shutdown() {
log::warn!("{:?}", e);
}
}
}
}
}
}
pub struct RouteTable {
@@ -428,11 +439,18 @@ impl RouteTable {
}
list
}
pub fn remove_route(&self, id: &Ipv4Addr, route_key: RouteKey) {
if let Some((_, routes)) = self.route_table.write().get_mut(id) {
pub fn remove_route(&self, id: &Ipv4Addr, route_key: RouteKey) -> bool {
let mut write_guard = self.route_table.write();
if let Some((_, routes)) = write_guard.get_mut(id) {
routes.retain(|(x, _)| x.route_key() != route_key);
if routes.is_empty() {
write_guard.remove(id);
true
} else {
false
}
} else {
return;
return true;
}
}
/// 更新路由入栈包的时刻,长时间没有收到数据的路由将会被剔除
+1 -1
View File
@@ -83,7 +83,7 @@ fn idle_route0<Call: VntCallback>(
let cur = current_device.load();
match idle.next_idle() {
IdleType::Timeout(ip, route) => {
context.route_table.remove_route(&ip, route);
context.remove_route(&ip, route);
if cur.is_gateway(&ip) {
//网关路由过期,则需要改变状态
let cur = context.change_status(current_device);