Skip to content

Commit

Permalink
修改结构体名称
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Apr 30, 2024
1 parent fcbc52d commit f78f8de
Show file tree
Hide file tree
Showing 25 changed files with 106 additions and 98 deletions.
6 changes: 3 additions & 3 deletions vnt/src/channel/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use crate::channel::{Route, RouteKey, UseChannelType, DEFAULT_RT};

/// 传输通道上下文,持有udp socket、tcp socket和路由信息
#[derive(Clone)]
pub struct Context {
pub struct ChannelContext {
inner: Arc<ContextInner>,
}

impl Context {
impl ChannelContext {
pub fn new(
main_udp_socket: Vec<UdpSocket>,
use_channel_type: UseChannelType,
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Context {
}
}

impl Deref for Context {
impl Deref for ChannelContext {
type Target = ContextInner;

fn deref(&self) -> &Self::Target {
Expand Down
4 changes: 2 additions & 2 deletions vnt/src/channel/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::channel::RouteKey;

pub trait RecvChannelHandler: Clone + Send + 'static {
fn handle(&mut self, buf: &mut [u8], route_key: RouteKey, context: &Context);
fn handle(&mut self, buf: &mut [u8], route_key: RouteKey, context: &ChannelContext);
}
6 changes: 3 additions & 3 deletions vnt/src/channel/idle.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::net::Ipv4Addr;
use std::time::Duration;

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::channel::Route;

pub struct Idle {
read_idle: Duration,
context: Context,
context: ChannelContext,
}

impl Idle {
pub fn new(read_idle: Duration, context: Context) -> Self {
pub fn new(read_idle: Duration, context: ChannelContext) -> Self {
Self { read_idle, context }
}
}
Expand Down
8 changes: 4 additions & 4 deletions vnt/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;
use std::net::{SocketAddr, UdpSocket};
use std::str::FromStr;

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::channel::handler::RecvChannelHandler;
use crate::channel::sender::AcceptSocketSender;
use crate::channel::tcp_channel::tcp_listen;
Expand Down Expand Up @@ -151,7 +151,7 @@ pub fn init_context(
is_tcp: bool,
packet_loss_rate: Option<f64>,
packet_delay: u32,
) -> io::Result<(Context, mio::net::TcpListener)> {
) -> io::Result<(ChannelContext, mio::net::TcpListener)> {
assert!(!ports.is_empty(), "not channel");
let mut udps = Vec::with_capacity(ports.len());
//检查系统是否支持ipv6
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn init_context(
let main_channel: UdpSocket = socket.into();
udps.push(main_channel);
}
let context = Context::new(
let context = ChannelContext::new(
udps,
use_channel_type,
first_latency,
Expand Down Expand Up @@ -242,7 +242,7 @@ pub fn init_context(

pub fn init_channel<H>(
tcp_listener: mio::net::TcpListener,
context: Context,
context: ChannelContext,
stop_manager: StopManager,
recv_handler: H,
) -> io::Result<(
Expand Down
6 changes: 3 additions & 3 deletions vnt/src/channel/punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use mio::net::TcpStream;
use rand::prelude::SliceRandom;
use rand::Rng;

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::channel::sender::AcceptSocketSender;
use crate::external_route::ExternalRoute;
use crate::nat::NatTest;
Expand Down Expand Up @@ -181,7 +181,7 @@ impl NatInfo {

#[derive(Clone)]
pub struct Punch {
context: Context,
context: ChannelContext,
port_vec: Vec<u16>,
port_index: HashMap<Ipv4Addr, usize>,
punch_model: PunchModel,
Expand All @@ -193,7 +193,7 @@ pub struct Punch {

impl Punch {
pub fn new(
context: Context,
context: ChannelContext,
punch_model: PunchModel,
is_tcp: bool,
tcp_socket_sender: AcceptSocketSender<(TcpStream, SocketAddr, Option<Vec<u8>>)>,
Expand Down
8 changes: 4 additions & 4 deletions vnt/src/channel/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use std::sync::Arc;

use mio::Token;

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::channel::notify::{AcceptNotify, WritableNotify};

#[derive(Clone)]
pub struct ChannelSender {
context: Context,
context: ChannelContext,
}

impl ChannelSender {
pub fn new(context: Context) -> Self {
pub fn new(context: ChannelContext) -> Self {
Self { context }
}
}

impl Deref for ChannelSender {
type Target = Context;
type Target = ChannelContext;

fn deref(&self) -> &Self::Target {
&self.context
Expand Down
14 changes: 7 additions & 7 deletions vnt/src/channel/tcp_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{io, thread};
use mio::net::{TcpListener, TcpStream};
use mio::{Events, Interest, Poll, Registry, Token, Waker};

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::channel::handler::RecvChannelHandler;
use crate::channel::notify::{AcceptNotify, WritableNotify};
use crate::channel::sender::{AcceptSocketSender, PacketSender};
Expand All @@ -30,7 +30,7 @@ pub fn tcp_listen<H>(
tcp_server: TcpListener,
stop_manager: StopManager,
recv_handler: H,
context: Context,
context: ChannelContext,
) -> io::Result<AcceptSocketSender<(TcpStream, SocketAddr, Option<Vec<u8>>)>>
where
H: RecvChannelHandler,
Expand Down Expand Up @@ -74,7 +74,7 @@ fn tcp_listen0<H>(
accept_notify: AcceptNotify,
accept_tcp_receiver: Receiver<(TcpStream, SocketAddr, Option<Vec<u8>>)>,
mut recv_handler: H,
context: Context,
context: ChannelContext,
) -> io::Result<()>
where
H: RecvChannelHandler,
Expand Down Expand Up @@ -158,7 +158,7 @@ where
fn init_writable_handler(
receiver: Receiver<(TcpStream, Token, SocketAddr, Option<Vec<u8>>)>,
stop_manager: StopManager,
context: Context,
context: ChannelContext,
) -> io::Result<WritableNotify> {
let poll = Poll::new()?;
let writable_notify = WritableNotify::new(Waker::new(poll.registry(), NOTIFY)?);
Expand Down Expand Up @@ -190,7 +190,7 @@ fn tcp_writable_listen(
receiver: Receiver<(TcpStream, Token, SocketAddr, Option<Vec<u8>>)>,
mut poll: Poll,
writable_notify: WritableNotify,
context: &Context,
context: &ChannelContext,
) -> io::Result<()> {
let mut events = Events::with_capacity(1024);
let mut write_map: HashMap<
Expand Down Expand Up @@ -338,7 +338,7 @@ fn readable_handle<H>(
token: &Token,
map: &mut HashMap<Token, (RouteKey, TcpStream, Box<[u8; BUFFER_SIZE]>, usize)>,
recv_handler: &mut H,
context: &Context,
context: &ChannelContext,
) -> io::Result<()>
where
H: RecvChannelHandler,
Expand Down Expand Up @@ -447,7 +447,7 @@ fn closed_handle_w(
Option<(Vec<u8>, usize)>,
),
>,
context: &Context,
context: &ChannelContext,
) {
if let Some((tcp, addr, _, _)) = map.remove(token) {
context.tcp_map.write().remove(&addr);
Expand Down
16 changes: 10 additions & 6 deletions vnt/src/channel/udp_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use mio::event::Source;
use mio::net::UdpSocket;
use mio::{Events, Interest, Poll, Token, Waker};

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::channel::handler::RecvChannelHandler;
use crate::channel::notify::AcceptNotify;
use crate::channel::sender::AcceptSocketSender;
Expand All @@ -16,7 +16,7 @@ use crate::util::StopManager;
pub fn udp_listen<H>(
stop_manager: StopManager,
recv_handler: H,
context: Context,
context: ChannelContext,
) -> io::Result<AcceptSocketSender<Option<Vec<UdpSocket>>>>
where
H: RecvChannelHandler,
Expand All @@ -30,7 +30,7 @@ const NOTIFY: Token = Token(0);
fn sub_udp_listen<H>(
stop_manager: StopManager,
recv_handler: H,
context: Context,
context: ChannelContext,
) -> io::Result<AcceptSocketSender<Option<Vec<UdpSocket>>>>
where
H: RecvChannelHandler,
Expand Down Expand Up @@ -61,7 +61,7 @@ where
fn sub_udp_listen0<H>(
mut poll: Poll,
mut recv_handler: H,
context: Context,
context: ChannelContext,
accept_notify: AcceptNotify,
accept_receiver: Receiver<Option<Vec<UdpSocket>>>,
) -> io::Result<()>
Expand Down Expand Up @@ -205,7 +205,7 @@ where
fn main_udp_listen<H>(
stop_manager: StopManager,
recv_handler: H,
context: Context,
context: ChannelContext,
) -> io::Result<()>
where
H: RecvChannelHandler,
Expand All @@ -231,7 +231,11 @@ where
Ok(())
}

pub fn main_udp_listen0<H>(mut poll: Poll, mut recv_handler: H, context: Context) -> io::Result<()>
pub fn main_udp_listen0<H>(
mut poll: Poll,
mut recv_handler: H,
context: ChannelContext,
) -> io::Result<()>
where
H: RecvChannelHandler,
{
Expand Down
6 changes: 3 additions & 3 deletions vnt/src/core/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rsa::signature::digest::Digest;
#[cfg(not(target_os = "android"))]
use tun::device::IFace;

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::channel::idle::Idle;
use crate::channel::punch::{NatInfo, Punch};
use crate::channel::{init_channel, init_context, Route, RouteKey};
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct Vnt {
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
nat_test: NatTest,
device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>,
context: Context,
context: ChannelContext,
peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>,
down_count_watcher: WatchU64Adder,
up_count_watcher: WatchSingleU64Adder,
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Vnt {

pub fn start<Call: VntCallback>(
scheduler: &Scheduler,
context: Context,
context: ChannelContext,
nat_test: NatTest,
device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
Expand Down
4 changes: 2 additions & 2 deletions vnt/src/handle/handshaker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crossbeam_utils::atomic::AtomicCell;
use parking_lot::Mutex;
use protobuf::Message;

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
#[cfg(feature = "server_encrypt")]
use crate::cipher::RsaCipher;
use crate::handle::{GATEWAY_IP, SELF_IP};
Expand Down Expand Up @@ -37,7 +37,7 @@ impl Handshake {
rsa_cipher,
}
}
pub fn send(&self, context: &Context, secret: bool, addr: SocketAddr) -> io::Result<()> {
pub fn send(&self, context: &ChannelContext, secret: bool, addr: SocketAddr) -> io::Result<()> {
let last = self.time.load();
//短时间不重复发送
if last.elapsed() < Duration::from_secs(3) {
Expand Down
8 changes: 4 additions & 4 deletions vnt/src/handle/maintain/addr_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;

use crossbeam_utils::atomic::AtomicCell;

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::cipher::Cipher;
use crate::handle::{BaseConfigInfo, CurrentDeviceInfo};
use crate::protocol::body::ENCRYPTION_RESERVED;
Expand All @@ -12,7 +12,7 @@ use crate::util::Scheduler;

pub fn addr_request(
scheduler: &Scheduler,
context: Context,
context: ChannelContext,
current_device_info: Arc<AtomicCell<CurrentDeviceInfo>>,
server_cipher: Cipher,
_config: BaseConfigInfo,
Expand All @@ -26,7 +26,7 @@ pub fn addr_request(
}
pub fn pub_address_request(
scheduler: &Scheduler,
context: Context,
context: ChannelContext,
current_device_info: Arc<AtomicCell<CurrentDeviceInfo>>,
server_cipher: Cipher,
) {
Expand All @@ -41,7 +41,7 @@ pub fn pub_address_request(
}

pub fn addr_request0(
context: &Context,
context: &ChannelContext,
current_device: &AtomicCell<CurrentDeviceInfo>,
server_cipher: &Cipher,
) {
Expand Down
12 changes: 6 additions & 6 deletions vnt/src/handle/maintain/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crossbeam_utils::atomic::AtomicCell;
use parking_lot::Mutex;
use rand::prelude::SliceRandom;

use crate::channel::context::Context;
use crate::channel::context::ChannelContext;
use crate::cipher::Cipher;
use crate::handle::{CurrentDeviceInfo, PeerDeviceInfo};
use crate::protocol::body::ENCRYPTION_RESERVED;
Expand All @@ -18,7 +18,7 @@ use crate::util::Scheduler;
/// 定时发送心跳包
pub fn heartbeat(
scheduler: &Scheduler,
context: Context,
context: ChannelContext,
current_device_info: Arc<AtomicCell<CurrentDeviceInfo>>,
device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>,
client_cipher: Cipher,
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn heartbeat(
}

fn heartbeat0(
context: &Context,
context: &ChannelContext,
current_device: &CurrentDeviceInfo,
device_list: &Mutex<(u16, Vec<PeerDeviceInfo>)>,
client_cipher: &Cipher,
Expand Down Expand Up @@ -125,7 +125,7 @@ fn heartbeat0(
/// 客户端中继路径探测,延迟启动
pub fn client_relay(
scheduler: &Scheduler,
context: Context,
context: ChannelContext,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>,
client_cipher: Cipher,
Expand All @@ -141,7 +141,7 @@ pub fn client_relay(
/// 客户端中继路径探测,每30秒探测一次
fn client_relay_(
scheduler: &Scheduler,
context: Context,
context: ChannelContext,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>,
client_cipher: Cipher,
Expand All @@ -163,7 +163,7 @@ fn client_relay_(
}

fn client_relay0(
context: &Context,
context: &ChannelContext,
current_device: &CurrentDeviceInfo,
device_list: &Mutex<(u16, Vec<PeerDeviceInfo>)>,
client_cipher: &Cipher,
Expand Down
Loading

0 comments on commit f78f8de

Please sign in to comment.