减少注册和探测nat的频率
This commit is contained in:
@@ -664,7 +664,7 @@ impl ChannelDataHandler {
|
|||||||
service_packet::Protocol::RegistrationResponse => {
|
service_packet::Protocol::RegistrationResponse => {
|
||||||
let response = RegistrationResponse::parse_from_bytes(net_packet.payload())?;
|
let response = RegistrationResponse::parse_from_bytes(net_packet.payload())?;
|
||||||
|
|
||||||
{
|
if self.nat_test.can_update() {
|
||||||
let context = context.clone();
|
let context = context.clone();
|
||||||
let nat_test = self.nat_test.clone();
|
let nat_test = self.nat_test.clone();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ impl Register {
|
|||||||
}
|
}
|
||||||
pub fn fast_register(&self, ip: Ipv4Addr) -> crate::Result<()> {
|
pub fn fast_register(&self, ip: Ipv4Addr) -> crate::Result<()> {
|
||||||
let last = self.time.load();
|
let last = self.time.load();
|
||||||
if last.elapsed() < Duration::from_secs(2)
|
if last.elapsed() < Duration::from_secs(3)
|
||||||
|| self.time.compare_exchange(last, Instant::now()).is_err()
|
|| self.time.compare_exchange(last, Instant::now()).is_err()
|
||||||
{
|
{
|
||||||
//短时间不重复注册
|
//短时间不重复注册
|
||||||
|
|||||||
+14
-1
@@ -1,7 +1,9 @@
|
|||||||
|
use crossbeam_utils::atomic::AtomicCell;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
@@ -54,6 +56,7 @@ pub fn local_ipv6_addr(port: u16) -> SocketAddrV6 {
|
|||||||
pub struct NatTest {
|
pub struct NatTest {
|
||||||
stun_server: Vec<String>,
|
stun_server: Vec<String>,
|
||||||
info: Arc<Mutex<NatInfo>>,
|
info: Arc<Mutex<NatInfo>>,
|
||||||
|
time: Arc<AtomicCell<Instant>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<NatType> for PunchNatType {
|
impl From<NatType> for PunchNatType {
|
||||||
@@ -93,7 +96,16 @@ impl NatTest {
|
|||||||
NatType::Cone,
|
NatType::Cone,
|
||||||
);
|
);
|
||||||
let info = Arc::new(Mutex::new(nat_info));
|
let info = Arc::new(Mutex::new(nat_info));
|
||||||
NatTest { stun_server, info }
|
NatTest {
|
||||||
|
stun_server,
|
||||||
|
info,
|
||||||
|
time: Arc::new(AtomicCell::new(Instant::now())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn can_update(&self) -> bool {
|
||||||
|
let last = self.time.load();
|
||||||
|
last.elapsed() > Duration::from_secs(10)
|
||||||
|
&& self.time.compare_exchange(last, Instant::now()).is_ok()
|
||||||
}
|
}
|
||||||
pub fn nat_info(&self) -> NatInfo {
|
pub fn nat_info(&self) -> NatInfo {
|
||||||
self.info.lock().clone()
|
self.info.lock().clone()
|
||||||
@@ -128,6 +140,7 @@ impl NatTest {
|
|||||||
ipv6_addr,
|
ipv6_addr,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
log::info!("探测nat类型={:?}", info);
|
||||||
*self.info.lock() = info.clone();
|
*self.info.lock() = info.clone();
|
||||||
info
|
info
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user