From 69da6de1edd0819a31d02a00ea240064c3342602 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Sun, 24 Mar 2024 09:18:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A6=BB=E7=BA=BF=E6=97=B6=E6=89=8D=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/handle/maintain/addr_request.rs | 49 +++------------- vnt/src/handle/maintain/idle.rs | 78 ++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 50 deletions(-) diff --git a/vnt/src/handle/maintain/addr_request.rs b/vnt/src/handle/maintain/addr_request.rs index f519234..f27ecf7 100644 --- a/vnt/src/handle/maintain/addr_request.rs +++ b/vnt/src/handle/maintain/addr_request.rs @@ -1,4 +1,3 @@ -use std::net::ToSocketAddrs; use std::sync::Arc; use std::time::Duration; @@ -16,10 +15,14 @@ pub fn addr_request( context: Context, current_device_info: Arc>, server_cipher: Cipher, - config: BaseConfigInfo, + _config: BaseConfigInfo, ) { - pub_address_request(scheduler, context, current_device_info.clone(), server_cipher); - domain_request(scheduler,current_device_info,config); + pub_address_request( + scheduler, + context, + current_device_info.clone(), + server_cipher, + ); } pub fn pub_address_request( scheduler: &Scheduler, @@ -36,43 +39,7 @@ pub fn pub_address_request( log::info!("定时任务停止"); } } -pub fn domain_request( - scheduler: &Scheduler, - current_device_info: Arc>, - config: BaseConfigInfo, -) { - domain_request0(¤t_device_info, &config); - // 120秒发送一次 - let rs = scheduler.timeout(Duration::from_secs(120), |s| { - domain_request(s, current_device_info, config) - }); - if !rs { - log::info!("定时任务停止"); - } -} -pub fn domain_request0( - current_device: &AtomicCell, - config: &BaseConfigInfo, -) { - let mut current_dev = current_device.load(); - // 探测服务端地址变化 - if let Ok(mut addr) = config.server_addr.to_socket_addrs() { - if let Some(addr) = addr.next() { - if addr != current_dev.connect_server { - 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() - ); - } - } - } -} + pub fn addr_request0( context: &Context, current_device: &AtomicCell, diff --git a/vnt/src/handle/maintain/idle.rs b/vnt/src/handle/maintain/idle.rs index 1697880..7838582 100644 --- a/vnt/src/handle/maintain/idle.rs +++ b/vnt/src/handle/maintain/idle.rs @@ -1,3 +1,11 @@ +use std::io; +use std::net::{SocketAddr, ToSocketAddrs}; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use crossbeam_utils::atomic::AtomicCell; +use mio::net::TcpStream; + use crate::channel::context::Context; use crate::channel::idle::{Idle, IdleType}; use crate::channel::sender::AcceptSocketSender; @@ -6,12 +14,6 @@ use crate::handle::handshaker::Handshake; use crate::handle::{handshaker, BaseConfigInfo, ConnectStatus, CurrentDeviceInfo}; use crate::util::Scheduler; use crate::{ErrorInfo, VntCallback}; -use crossbeam_utils::atomic::AtomicCell; -use mio::net::TcpStream; -use std::io; -use std::net::SocketAddr; -use std::sync::Arc; -use std::time::Duration; pub fn idle_route( scheduler: &Scheduler, @@ -29,6 +31,29 @@ pub fn idle_route( } } 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>, @@ -37,6 +62,7 @@ pub fn idle_gateway( call: Call, mut connect_count: usize, handshake: Handshake, + mut time: Instant, ) { idle_gateway0( &context, @@ -46,9 +72,10 @@ 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, @@ -57,6 +84,7 @@ pub fn idle_gateway( call, connect_count, handshake, + time, ) }); if !rs { @@ -71,6 +99,7 @@ fn idle_gateway0( call: &Call, connect_count: &mut usize, handshake: &Handshake, + time: &mut Instant, ) { if let Err(e) = check_gateway_channel( context, @@ -80,6 +109,7 @@ fn idle_gateway0( call, connect_count, handshake, + time, ) { let cur = current_device.load(); call.error(ErrorInfo::new_msg( @@ -113,16 +143,22 @@ fn idle_route0( fn check_gateway_channel( context: &Context, - current_device: &AtomicCell, + current_device_info: &AtomicCell, config: &BaseConfigInfo, tcp_socket_sender: &AcceptSocketSender<(TcpStream, SocketAddr, Option>)>, call: &Call, count: &mut usize, handshake: &Handshake, + time: &mut Instant, ) -> io::Result<()> { - let current_device = current_device.load(); + 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() + } //需要重连 call.connect(ConnectInfo::new(*count, current_device.connect_server)); log::info!("发送握手请求,{:?}", config); @@ -149,3 +185,27 @@ fn check_gateway_channel( } Ok(()) } +pub fn domain_request0( + current_device: &AtomicCell, + config: &BaseConfigInfo, +) -> CurrentDeviceInfo { + let mut current_dev = current_device.load(); + // 探测服务端地址变化 + if let Ok(mut addr) = config.server_addr.to_socket_addrs() { + if let Some(addr) = addr.next() { + if addr != current_dev.connect_server { + 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() + ); + } + } + } + current_dev +}