解决掉线问题

This commit is contained in:
lubeilin
2023-02-06 22:12:04 +08:00
parent af58c3990d
commit 5cd9179ce7
7 changed files with 44 additions and 45 deletions
+2 -1
View File
@@ -52,7 +52,8 @@ async fn handle_loop(
ping.set_epoch(epoch);
}
let _ = udp.send_to(net_packet.buffer(), server_addr);
for x in DIRECT_ROUTE_TABLE.iter() {
// 不clone会死锁?
for x in DIRECT_ROUTE_TABLE.clone().iter() {
let virtual_ip = x.key().clone();
let route = x.value().clone();
drop(x);
+5 -9
View File
@@ -1,20 +1,20 @@
use std::{io, thread};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
use std::time::Duration;
use std::{io, thread};
use protobuf::Message;
use rand::prelude::SliceRandom;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::mpsc::error::TrySendError;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::watch;
use crate::{CurrentDeviceInfo, DEVICE_LIST, handle::NAT_INFO, handle::NatInfo};
use crate::error::*;
use crate::handle::{ApplicationStatus, DIRECT_ROUTE_TABLE};
use crate::proto::message::{NatType, Punch};
use crate::protocol::{control_packet, NetPacket, Protocol, turn_packet, Version};
use crate::protocol::control_packet::PunchRequestPacket;
use crate::protocol::turn_packet::TurnPacket;
use crate::protocol::{control_packet, turn_packet, NetPacket, Protocol, Version};
use crate::{handle::NatInfo, handle::NAT_INFO, CurrentDeviceInfo, DEVICE_LIST};
/// 每一种类型一个通道,减少相互干扰
pub fn bounded() -> (
@@ -381,11 +381,7 @@ fn send_punch(udp: &UdpSocket, cur_info: &CurrentDeviceInfo, nat_info: NatInfo)
Ok(())
}
fn punch_packet(
virtual_ip: Ipv4Addr,
nat_info: NatInfo,
dest: Ipv4Addr,
) -> Result<Vec<u8>> {
fn punch_packet(virtual_ip: Ipv4Addr, nat_info: NatInfo, dest: Ipv4Addr) -> Result<Vec<u8>> {
let mut punch_reply = Punch::new();
punch_reply.reply = false;
punch_reply.virtual_ip = u32::from_be_bytes(virtual_ip.octets());
+19 -18
View File
@@ -1,8 +1,8 @@
use std::io;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, ToSocketAddrs, UdpSocket};
use std::sync::atomic::{Ordering};
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use crossbeam::atomic::AtomicCell;
use crossbeam::sync::WaitGroup;
@@ -11,11 +11,11 @@ use tokio::sync::watch;
use error::*;
use crate::handle::registration_handler::CONNECTION_STATUS;
use crate::handle::{
ApplicationStatus, ConnectStatus, CurrentDeviceInfo, PeerDeviceInfo, Route, RouteType,
DEVICE_LIST, DIRECT_ROUTE_TABLE, SERVER_RT,
ApplicationStatus, ConnectStatus, CurrentDeviceInfo, DEVICE_LIST, DIRECT_ROUTE_TABLE, PeerDeviceInfo,
Route, RouteType, SERVER_RT,
};
use crate::handle::registration_handler::CONNECTION_STATUS;
pub mod error;
pub mod handle;
@@ -39,8 +39,8 @@ impl<F> Config<F> {
name: Option<String>,
abnormal_call: F,
) -> Result<Self>
where
F: FnOnce() + Send + 'static,
where
F: FnOnce() + Send + 'static,
{
if token.is_empty() || token.len() > 64 {
return Err(Error::Stop("token invalid".to_string()));
@@ -84,8 +84,8 @@ pub struct Switch {
impl Switch {
pub fn start<F>(config: Config<F>) -> Result<Self>
where
F: FnOnce() + Send + 'static,
where
F: FnOnce() + Send + 'static,
{
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
@@ -141,8 +141,8 @@ impl Switch {
return status == ApplicationStatus::Starting;
}
pub async fn start_<F>(config: Config<F>) -> Result<Self>
where
F: FnOnce() + Send + 'static,
where
F: FnOnce() + Send + 'static,
{
// let server_address = "nat1.wherewego.top:29876"
let server_address = "127.0.0.1:29876".to_socket_addrs().unwrap().next().unwrap();
@@ -162,6 +162,7 @@ impl Switch {
}
}
};
udp.set_write_timeout(Some(Duration::from_millis(2000)))?;
//注册
let response = handle::registration_handler::registration(
&udp,
@@ -214,7 +215,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
}
//初始化nat数据
handle::init_nat_info(response.public_ip, response.public_port as u16);
@@ -248,7 +249,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
let udp1 = udp.try_clone()?;
let wait_group1 = wait_group.clone();
let status_sender1 = status_sender.clone();
@@ -268,7 +269,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
}
//打洞处理
{
@@ -290,7 +291,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
let udp1 = udp.try_clone()?;
let wait_group1 = wait_group.clone();
let status_sender1 = status_sender.clone();
@@ -309,7 +310,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
let udp1 = udp.try_clone()?;
let wait_group1 = wait_group.clone();
let status_sender1 = status_sender.clone();
@@ -328,7 +329,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
}
//tun数据处理
{
@@ -349,7 +350,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
}
Ok(Switch {
current_device,