修复域名转换ip的问题

This commit is contained in:
lubeilin
2024-04-09 22:48:14 +08:00
parent aeebf45390
commit c4a9e79dc2
+11 -37
View File
@@ -1,7 +1,7 @@
use std::io; use std::io;
use std::net::{SocketAddr, ToSocketAddrs}; use std::net::{SocketAddr, ToSocketAddrs};
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::Duration;
use crossbeam_utils::atomic::AtomicCell; use crossbeam_utils::atomic::AtomicCell;
use mio::net::TcpStream; use mio::net::TcpStream;
@@ -30,30 +30,8 @@ pub fn idle_route<Call: VntCallback>(
log::info!("定时任务停止"); log::info!("定时任务停止");
} }
} }
pub fn idle_gateway<Call: VntCallback>( pub fn idle_gateway<Call: VntCallback>(
scheduler: &Scheduler,
context: Context,
current_device_info: Arc<AtomicCell<CurrentDeviceInfo>>,
config: BaseConfigInfo,
tcp_socket_sender: AcceptSocketSender<(TcpStream, SocketAddr, Option<Vec<u8>>)>,
call: Call,
connect_count: usize,
handshake: Handshake,
) {
let time = Instant::now();
idle_gateway_(
scheduler,
context,
current_device_info,
config,
tcp_socket_sender,
call,
connect_count,
handshake,
time,
);
}
pub fn idle_gateway_<Call: VntCallback>(
scheduler: &Scheduler, scheduler: &Scheduler,
context: Context, context: Context,
current_device_info: Arc<AtomicCell<CurrentDeviceInfo>>, current_device_info: Arc<AtomicCell<CurrentDeviceInfo>>,
@@ -62,7 +40,6 @@ pub fn idle_gateway_<Call: VntCallback>(
call: Call, call: Call,
mut connect_count: usize, mut connect_count: usize,
handshake: Handshake, handshake: Handshake,
mut time: Instant,
) { ) {
idle_gateway0( idle_gateway0(
&context, &context,
@@ -72,10 +49,9 @@ pub fn idle_gateway_<Call: VntCallback>(
&call, &call,
&mut connect_count, &mut connect_count,
&handshake, &handshake,
&mut time,
); );
let rs = scheduler.timeout(Duration::from_secs(5), move |s| { let rs = scheduler.timeout(Duration::from_secs(5), move |s| {
idle_gateway_( idle_gateway(
s, s,
context, context,
current_device_info, current_device_info,
@@ -84,13 +60,13 @@ pub fn idle_gateway_<Call: VntCallback>(
call, call,
connect_count, connect_count,
handshake, handshake,
time,
) )
}); });
if !rs { if !rs {
log::info!("定时任务停止"); log::info!("定时任务停止");
} }
} }
fn idle_gateway0<Call: VntCallback>( fn idle_gateway0<Call: VntCallback>(
context: &Context, context: &Context,
current_device: &AtomicCell<CurrentDeviceInfo>, current_device: &AtomicCell<CurrentDeviceInfo>,
@@ -99,7 +75,6 @@ fn idle_gateway0<Call: VntCallback>(
call: &Call, call: &Call,
connect_count: &mut usize, connect_count: &mut usize,
handshake: &Handshake, handshake: &Handshake,
time: &mut Instant,
) { ) {
if let Err(e) = check_gateway_channel( if let Err(e) = check_gateway_channel(
context, context,
@@ -109,7 +84,6 @@ fn idle_gateway0<Call: VntCallback>(
call, call,
connect_count, connect_count,
handshake, handshake,
time,
) { ) {
let cur = current_device.load(); let cur = current_device.load();
call.error(ErrorInfo::new_msg( call.error(ErrorInfo::new_msg(
@@ -118,6 +92,7 @@ fn idle_gateway0<Call: VntCallback>(
)); ));
} }
} }
fn idle_route0<Call: VntCallback>( fn idle_route0<Call: VntCallback>(
idle: &Idle, idle: &Idle,
context: &Context, context: &Context,
@@ -149,16 +124,12 @@ fn check_gateway_channel<Call: VntCallback>(
call: &Call, call: &Call,
count: &mut usize, count: &mut usize,
handshake: &Handshake, handshake: &Handshake,
time: &mut Instant,
) -> io::Result<()> { ) -> io::Result<()> {
let mut current_device = current_device_info.load(); let mut current_device = current_device_info.load();
if current_device.status.offline() { if current_device.status.offline() {
*count += 1; *count += 1;
if time.elapsed() < Duration::from_secs(6 * 60) { // 探测服务器地址
// 探测服务器地址 current_device = domain_request0(current_device_info, config);
current_device = domain_request0(current_device_info, config);
*time = Instant::now()
}
//需要重连 //需要重连
call.connect(ConnectInfo::new(*count, current_device.connect_server)); call.connect(ConnectInfo::new(*count, current_device.connect_server));
log::info!("发送握手请求,{:?}", config); log::info!("发送握手请求,{:?}", config);
@@ -185,6 +156,7 @@ fn check_gateway_channel<Call: VntCallback>(
} }
Ok(()) Ok(())
} }
pub fn domain_request0( pub fn domain_request0(
current_device: &AtomicCell<CurrentDeviceInfo>, current_device: &AtomicCell<CurrentDeviceInfo>,
config: &BaseConfigInfo, config: &BaseConfigInfo,
@@ -197,13 +169,15 @@ pub fn domain_request0(
let mut tmp = current_dev.clone(); let mut tmp = current_dev.clone();
tmp.connect_server = addr; tmp.connect_server = addr;
let rs = current_device.compare_exchange(current_dev, tmp); let rs = current_device.compare_exchange(current_dev, tmp);
current_dev.connect_server = addr;
log::info!( log::info!(
"服务端地址变化,旧地址:{},新地址:{},替换结果:{}", "服务端地址变化,旧地址:{},新地址:{},替换结果:{}",
current_dev.connect_server, current_dev.connect_server,
addr, addr,
rs.is_ok() rs.is_ok()
); );
if rs.is_ok() {
current_dev.connect_server = addr;
}
} }
} }
} }