Skip to content

Commit

Permalink
Merge pull request #58 from lbl8603/1.2.x
Browse files Browse the repository at this point in the history
调整添加转发路径的逻辑
  • Loading branch information
vnt-dev authored Jul 10, 2024
2 parents 59d6bfd + 27c0f38 commit dc45602
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
28 changes: 20 additions & 8 deletions vnt/src/channel/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,30 +353,38 @@ impl RouteTable {
}
Err(io::Error::new(io::ErrorKind::NotFound, "route not found"))
}
pub fn add_route_if_absent(&self, id: Ipv4Addr, route: Route) {
pub fn add_route_if_absent(&self, id: Ipv4Addr, route: Route) -> bool {
self.add_route_(id, route, true)
}
pub fn add_route(&self, id: Ipv4Addr, route: Route) {
pub fn add_route(&self, id: Ipv4Addr, route: Route) -> bool {
self.add_route_(id, route, false)
}
fn add_route_(&self, id: Ipv4Addr, route: Route, only_if_absent: bool) {
fn add_route_(&self, id: Ipv4Addr, route: Route, only_if_absent: bool) -> bool {
// 限制通道类型
match self.use_channel_type {
UseChannelType::P2p => {
if !route.is_p2p() {
return;
return false;
}
}
_ => {}
}
let key = route.route_key();
if only_if_absent {
if let Some((_, list)) = self.route_table.read().get(&id) {
let mut p2p_num = 0;
for (x, _) in list {
if x.is_p2p() {
p2p_num += 1;
}
if x.route_key() == key {
return;
return true;
}
}
if !self.first_latency && p2p_num >= self.channel_num {
// 非优先延迟的情况下,通道满了则不用再添加
return false;
}
}
}
let mut route_table = self.route_table.write();
Expand All @@ -387,11 +395,11 @@ impl RouteTable {
for (x, time) in list.iter_mut() {
if x.metric < route.metric && !self.first_latency {
//非优先延迟的情况下 不能比当前的路径更长
return;
return false;
}
if x.route_key() == key {
if only_if_absent {
return;
return true;
}
x.metric = route.metric;
x.rt = route.rt;
Expand All @@ -406,7 +414,7 @@ impl RouteTable {
//如果延迟都稳定了,则去除多余通道
for (route, _) in list.iter() {
if route.rt == DEFAULT_RT {
return;
return true;
}
}
//延迟优先模式需要更多的通道探测延迟最低的路线
Expand All @@ -422,13 +430,17 @@ impl RouteTable {
//非优先延迟的情况下 添加了直连的则排除非直连的
list.retain(|(k, _)| k.is_p2p());
}
if self.channel_num <= list.len() {
return false;
}
};
//增加路由表容量,避免波动
let limit_len = self.channel_num * 2;
list.sort_by_key(|(k, _)| k.rt);
self.truncate_(list, limit_len);
list.push((route, AtomicCell::new(Instant::now())));
}
return true;
}
fn truncate_(&self, list: &mut Vec<(Route, AtomicCell<Instant>)>, len: usize) {
if list.len() <= len {
Expand Down
18 changes: 11 additions & 7 deletions vnt/src/handle/recv_data/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,18 @@ impl<Device: DeviceWrite> ClientPacketHandler<Device> {
let source = net_packet.source();
match ControlPacket::new(net_packet.transport_protocol(), net_packet.payload())? {
ControlPacket::PingPacket(_) => {
net_packet.set_transport_protocol(control_packet::Protocol::Pong.into());
net_packet.set_source(current_device.virtual_ip);
net_packet.set_destination(source);
net_packet.first_set_ttl(MAX_TTL);
self.client_cipher.encrypt_ipv4(&mut net_packet)?;
context.send_by_key(&net_packet, route_key)?;
let route = Route::from_default_rt(route_key, metric);
context.route_table.add_route_if_absent(source, route);
if context.route_table.add_route_if_absent(source, route)
|| net_packet.source() < current_device.virtual_ip
{
//在路由表中,或者来源比自己小,就需要回复,注意不能调换顺序
net_packet.set_transport_protocol(control_packet::Protocol::Pong.into());
net_packet.set_source(current_device.virtual_ip);
net_packet.set_destination(source);
net_packet.first_set_ttl(MAX_TTL);
self.client_cipher.encrypt_ipv4(&mut net_packet)?;
context.send_by_key(&net_packet, route_key)?;
}
}
ControlPacket::PongPacket(pong_packet) => {
let current_time = crate::handle::now_time() as u16;
Expand Down

0 comments on commit dc45602

Please sign in to comment.