优化重连逻辑和路径探测
This commit is contained in:
@@ -88,7 +88,7 @@ fn start_heartbeat_(
|
|||||||
let peer_list = device_list.lock().1.clone();
|
let peer_list = device_list.lock().1.clone();
|
||||||
for peer in peer_list {
|
for peer in peer_list {
|
||||||
set_now_time(&mut net_packet)?;
|
set_now_time(&mut net_packet)?;
|
||||||
net_packet.first_set_ttl(MAX_TTL);
|
net_packet.first_set_ttl(2);
|
||||||
net_packet.set_destination(peer.virtual_ip);
|
net_packet.set_destination(peer.virtual_ip);
|
||||||
if sender
|
if sender
|
||||||
.send_to_id(net_packet.buffer(), &peer.virtual_ip)
|
.send_to_id(net_packet.buffer(), &peer.virtual_ip)
|
||||||
@@ -103,6 +103,7 @@ fn start_heartbeat_(
|
|||||||
l
|
l
|
||||||
});
|
});
|
||||||
let mut num = 0;
|
let mut num = 0;
|
||||||
|
//只寻找两跳以内能到的目标
|
||||||
net_packet.first_set_ttl(2);
|
net_packet.first_set_ttl(2);
|
||||||
for (peer_ip, route) in route_list.iter() {
|
for (peer_ip, route) in route_list.iter() {
|
||||||
if peer_ip != &peer.virtual_ip && route.metric == 1 {
|
if peer_ip != &peer.virtual_ip && route.metric == 1 {
|
||||||
|
|||||||
@@ -25,37 +25,64 @@ pub fn registration(
|
|||||||
registration_request_packet(token.clone(), device_id.clone(), name.clone(), false)?;
|
registration_request_packet(token.clone(), device_id.clone(), name.clone(), false)?;
|
||||||
let buf = request_packet.buffer();
|
let buf = request_packet.buffer();
|
||||||
let mut recv_buf = [0u8; 10240];
|
let mut recv_buf = [0u8; 10240];
|
||||||
channel.send_to_addr(buf, server_address)?;
|
let mut count = 0;
|
||||||
let (len, route) = channel.recv_from(&mut recv_buf, Some(Duration::from_millis(300)))?;
|
let len = loop {
|
||||||
if server_address != route.addr {
|
match channel.send_to_addr(buf, server_address) {
|
||||||
return Err(Error::Warn(format!("数据来源错误:{:?}", route.addr)));
|
Ok(_) => {
|
||||||
}
|
match channel.recv_from(&mut recv_buf, Some(Duration::from_millis(300))) {
|
||||||
let net_packet = NetPacket::new(&recv_buf[..len])?;
|
Ok((len, route)) => {
|
||||||
return match net_packet.protocol() {
|
if server_address == route.addr {
|
||||||
Protocol::Service => {
|
let net_packet = NetPacket::new(&recv_buf[..len])?;
|
||||||
match service_packet::Protocol::from(net_packet.transport_protocol()) {
|
match net_packet.protocol() {
|
||||||
service_packet::Protocol::RegistrationResponse => {
|
Protocol::Service => {
|
||||||
let response = RegistrationResponse::parse_from_bytes(net_packet.payload())?;
|
match service_packet::Protocol::from(net_packet.transport_protocol()) {
|
||||||
Ok(response)
|
service_packet::Protocol::RegistrationResponse => {
|
||||||
|
let response = RegistrationResponse::parse_from_bytes(net_packet.payload())?;
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
_ => println!("响应数据错误"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Protocol::Error => {
|
||||||
|
match InErrorPacket::new(net_packet.transport_protocol(), net_packet.payload()) {
|
||||||
|
Ok(e) => match e {
|
||||||
|
InErrorPacket::TokenError => return Err(Error::Stop("token错误".to_string())),
|
||||||
|
InErrorPacket::Disconnect => {
|
||||||
|
println!("断开连接");
|
||||||
|
}
|
||||||
|
InErrorPacket::AddressExhausted => {
|
||||||
|
println!("地址用尽");
|
||||||
|
log::warn!("地址用尽");
|
||||||
|
}
|
||||||
|
InErrorPacket::OtherError(e) => match e.message() {
|
||||||
|
Ok(str) => {
|
||||||
|
println!("其他异常:{:?}", str);
|
||||||
|
log::warn!("其他异常{:?}",str);
|
||||||
|
}
|
||||||
|
Err(e) => println!("其他异常:{:?}", e),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Err(e) => println!("数据解析异常:{:?}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => println!("响应数据错误"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("接收服务器数据失败:{:?}", e);
|
||||||
|
log::warn!("接收服务器数据失败:{:?}",e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => Err(Error::Warn(format!("数据错误:{:?}", net_packet))),
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("发送数据到服务器失败:{:?}", e);
|
||||||
|
log::warn!("发送数据到服务器失败:{:?}",e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Protocol::Error => {
|
count += 1;
|
||||||
match InErrorPacket::new(net_packet.transport_protocol(), net_packet.payload()) {
|
println!("重试中(retrying)...");
|
||||||
Ok(e) => match e {
|
std::thread::sleep(Duration::from_secs(count % 10 + 1));
|
||||||
InErrorPacket::TokenError => Err(Error::Stop("token错误".to_string())),
|
|
||||||
InErrorPacket::Disconnect => Err(Error::Warn("断开连接".to_string())),
|
|
||||||
InErrorPacket::AddressExhausted => Err(Error::Stop("地址用尽".to_string())),
|
|
||||||
InErrorPacket::OtherError(e) => match e.message() {
|
|
||||||
Ok(str) => Err(Error::Warn(str)),
|
|
||||||
Err(e) => Err(Error::Warn(format!("{:?}", e))),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Err(e) => Err(Error::Warn(format!("{:?}", e))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => Err(Error::Warn(format!("数据错误:{:?}", net_packet))),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,9 +139,9 @@ impl Register {
|
|||||||
let new = Local::now().timestamp_millis();
|
let new = Local::now().timestamp_millis();
|
||||||
if new - last < 1000
|
if new - last < 1000
|
||||||
|| self
|
|| self
|
||||||
.time
|
.time
|
||||||
.compare_exchange(last, new, Ordering::Relaxed, Ordering::Relaxed)
|
.compare_exchange(last, new, Ordering::Relaxed, Ordering::Relaxed)
|
||||||
.is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
//短时间不重复注册
|
//短时间不重复注册
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -126,7 +153,7 @@ impl Register {
|
|||||||
self.name.clone(),
|
self.name.clone(),
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let buf = request_packet.buffer();
|
let buf = request_packet.buffer();
|
||||||
self.sender.send_to_addr(buf, self.server_address)?;
|
self.sender.send_to_addr(buf, self.server_address)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user