diff --git a/tuic/src/prototype/authenticate.rs b/tuic/src/prototype/authenticate.rs index fa02069..be03ee8 100644 --- a/tuic/src/prototype/authenticate.rs +++ b/tuic/src/prototype/authenticate.rs @@ -1,7 +1,4 @@ -use super::{ - side::{self, Side, SideMarker}, - TaskRegister, -}; +use super::side::{self, Side, SideMarker}; use crate::protocol::{Authenticate as AuthenticateHeader, Header}; pub struct Authenticate @@ -14,17 +11,15 @@ where pub struct Tx { header: Header, - _task_reg: TaskRegister, } pub struct Rx; impl Authenticate { - pub(super) fn new(task_reg: TaskRegister, token: [u8; 8]) -> Self { + pub(super) fn new(token: [u8; 8]) -> Self { Self { inner: Side::Tx(Tx { header: Header::Authenticate(AuthenticateHeader::new(token)), - _task_reg: task_reg, }), _marker: side::Tx, } diff --git a/tuic/src/prototype/connect.rs b/tuic/src/prototype/connect.rs index 646ea9b..a3643a1 100644 --- a/tuic/src/prototype/connect.rs +++ b/tuic/src/prototype/connect.rs @@ -36,13 +36,17 @@ impl Connect { struct Rx { addr: Address, + _task_reg: TaskRegister, } impl Connect { - pub(super) fn new(header: ConnectHeader) -> Self { + pub(super) fn new(task_reg: TaskRegister, header: ConnectHeader) -> Self { let (addr,) = header.into(); Self { - inner: Side::Rx(Rx { addr }), + inner: Side::Rx(Rx { + addr, + _task_reg: task_reg, + }), _marker: side::Rx, } } diff --git a/tuic/src/prototype/mod.rs b/tuic/src/prototype/mod.rs index 2c0b509..86ca1d7 100644 --- a/tuic/src/prototype/mod.rs +++ b/tuic/src/prototype/mod.rs @@ -24,29 +24,31 @@ pub use self::{ pub struct Connection { udp_sessions: Mutex, - local_active_task_count: ActiveTaskCount, + task_connect_count: TaskCount, + task_associate_count: TaskCount, } impl Connection { pub fn new() -> Self { - let local_active_task_count = ActiveTaskCount::new(); + let task_associate_count = TaskCount::new(); Self { - udp_sessions: Mutex::new(UdpSessions::new(local_active_task_count.clone())), - local_active_task_count, + udp_sessions: Mutex::new(UdpSessions::new(task_associate_count.clone())), + task_connect_count: TaskCount::new(), + task_associate_count, } } pub fn send_authenticate(&self, token: [u8; 8]) -> Authenticate { - Authenticate::new(self.local_active_task_count.reg(), token) + Authenticate::new(token) } pub fn send_connect(&self, addr: Address) -> Connect { - Connect::::new(self.local_active_task_count.reg(), addr) + Connect::::new(self.task_connect_count.reg(), addr) } pub fn recv_connect(&self, header: ConnectHeader) -> Connect { - Connect::::new(header) + Connect::::new(self.task_connect_count.reg(), header) } pub fn send_packet( @@ -66,16 +68,20 @@ impl Connection { Heartbeat::new() } - pub fn local_active_task_count(&self) -> usize { - self.local_active_task_count.get() + pub fn task_connect_count(&self) -> usize { + self.task_connect_count.get() + } + + pub fn task_associate_count(&self) -> usize { + self.task_associate_count.get() } } #[derive(Clone)] -struct ActiveTaskCount(Arc<()>); +struct TaskCount(Arc<()>); struct TaskRegister(Weak<()>); -impl ActiveTaskCount { +impl TaskCount { fn new() -> Self { Self(Arc::new(())) } @@ -91,21 +97,21 @@ impl ActiveTaskCount { struct UdpSessions { sessions: HashMap, - local_active_task_count: ActiveTaskCount, + task_associate_count: TaskCount, } impl UdpSessions { - fn new(local_active_task_count: ActiveTaskCount) -> Self { + fn new(task_associate_count: TaskCount) -> Self { Self { sessions: HashMap::new(), - local_active_task_count, + task_associate_count, } } fn send<'a>(&mut self, assoc_id: u16, addr: Address, max_pkt_size: usize) -> Packet { self.sessions .entry(assoc_id) - .or_insert_with(|| UdpSession::new(self.local_active_task_count.reg())) + .or_insert_with(|| UdpSession::new(self.task_associate_count.reg())) .send(assoc_id, addr, max_pkt_size) }