From c4a9e79dc27f97f6a1c77ff4a02ea9538dfe01a8 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Tue, 9 Apr 2024 22:48:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9F=9F=E5=90=8D=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2ip=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/handle/maintain/idle.rs | 48 ++++++++------------------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/vnt/src/handle/maintain/idle.rs b/vnt/src/handle/maintain/idle.rs index 7838582..0b3e760 100644 --- a/vnt/src/handle/maintain/idle.rs +++ b/vnt/src/handle/maintain/idle.rs @@ -1,7 +1,7 @@ use std::io; use std::net::{SocketAddr, ToSocketAddrs}; use std::sync::Arc; -use std::time::{Duration, Instant}; +use std::time::Duration; use crossbeam_utils::atomic::AtomicCell; use mio::net::TcpStream; @@ -30,30 +30,8 @@ pub fn idle_route( log::info!("定时任务停止"); } } + pub fn idle_gateway( - scheduler: &Scheduler, - context: Context, - current_device_info: Arc>, - config: BaseConfigInfo, - tcp_socket_sender: AcceptSocketSender<(TcpStream, SocketAddr, Option>)>, - 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_( scheduler: &Scheduler, context: Context, current_device_info: Arc>, @@ -62,7 +40,6 @@ pub fn idle_gateway_( call: Call, mut connect_count: usize, handshake: Handshake, - mut time: Instant, ) { idle_gateway0( &context, @@ -72,10 +49,9 @@ pub fn idle_gateway_( &call, &mut connect_count, &handshake, - &mut time, ); let rs = scheduler.timeout(Duration::from_secs(5), move |s| { - idle_gateway_( + idle_gateway( s, context, current_device_info, @@ -84,13 +60,13 @@ pub fn idle_gateway_( call, connect_count, handshake, - time, ) }); if !rs { log::info!("定时任务停止"); } } + fn idle_gateway0( context: &Context, current_device: &AtomicCell, @@ -99,7 +75,6 @@ fn idle_gateway0( call: &Call, connect_count: &mut usize, handshake: &Handshake, - time: &mut Instant, ) { if let Err(e) = check_gateway_channel( context, @@ -109,7 +84,6 @@ fn idle_gateway0( call, connect_count, handshake, - time, ) { let cur = current_device.load(); call.error(ErrorInfo::new_msg( @@ -118,6 +92,7 @@ fn idle_gateway0( )); } } + fn idle_route0( idle: &Idle, context: &Context, @@ -149,16 +124,12 @@ fn check_gateway_channel( call: &Call, count: &mut usize, handshake: &Handshake, - time: &mut Instant, ) -> io::Result<()> { let mut current_device = current_device_info.load(); if current_device.status.offline() { *count += 1; - if time.elapsed() < Duration::from_secs(6 * 60) { - // 探测服务器地址 - current_device = domain_request0(current_device_info, config); - *time = Instant::now() - } + // 探测服务器地址 + current_device = domain_request0(current_device_info, config); //需要重连 call.connect(ConnectInfo::new(*count, current_device.connect_server)); log::info!("发送握手请求,{:?}", config); @@ -185,6 +156,7 @@ fn check_gateway_channel( } Ok(()) } + pub fn domain_request0( current_device: &AtomicCell, config: &BaseConfigInfo, @@ -197,13 +169,15 @@ pub fn domain_request0( let mut tmp = current_dev.clone(); tmp.connect_server = addr; let rs = current_device.compare_exchange(current_dev, tmp); - current_dev.connect_server = addr; log::info!( "服务端地址变化,旧地址:{},新地址:{},替换结果:{}", current_dev.connect_server, addr, rs.is_ok() ); + if rs.is_ok() { + current_dev.connect_server = addr; + } } } }