From 2c2fc7924b2e2de9be954ac2021d6be78d11fc4a Mon Sep 17 00:00:00 2001 From: EAimTY Date: Wed, 25 Jan 2023 00:12:34 +0900 Subject: [PATCH] adding header destructors --- tuic/src/protocol/authenticate.rs | 6 ++++++ tuic/src/protocol/connect.rs | 6 ++++++ tuic/src/protocol/dissociate.rs | 10 ++++++++-- tuic/src/protocol/heartbeat.rs | 6 ++++++ tuic/src/protocol/packet.rs | 33 +++++++++++++++++++++---------- tuic/src/prototype/connect.rs | 16 +++++++++++++-- tuic/src/prototype/mod.rs | 6 +++++- 7 files changed, 68 insertions(+), 15 deletions(-) diff --git a/tuic/src/protocol/authenticate.rs b/tuic/src/protocol/authenticate.rs index ba016da..eebbafd 100644 --- a/tuic/src/protocol/authenticate.rs +++ b/tuic/src/protocol/authenticate.rs @@ -31,3 +31,9 @@ impl Command for Authenticate { 8 } } + +impl From for ([u8; 8],) { + fn from(auth: Authenticate) -> Self { + (auth.token,) + } +} diff --git a/tuic/src/protocol/connect.rs b/tuic/src/protocol/connect.rs index f32c6e7..1814558 100644 --- a/tuic/src/protocol/connect.rs +++ b/tuic/src/protocol/connect.rs @@ -31,3 +31,9 @@ impl Command for Connect { self.addr.len() } } + +impl From for (Address,) { + fn from(connect: Connect) -> Self { + (connect.addr,) + } +} diff --git a/tuic/src/protocol/dissociate.rs b/tuic/src/protocol/dissociate.rs index dc4dcf3..94734f5 100644 --- a/tuic/src/protocol/dissociate.rs +++ b/tuic/src/protocol/dissociate.rs @@ -17,8 +17,8 @@ impl Dissociate { Self { assoc_id } } - pub fn assoc_id(&self) -> u16 { - self.assoc_id + pub fn assoc_id(&self) -> &u16 { + &self.assoc_id } } @@ -31,3 +31,9 @@ impl Command for Dissociate { 2 } } + +impl From for (u16,) { + fn from(dissoc: Dissociate) -> Self { + (dissoc.assoc_id,) + } +} diff --git a/tuic/src/protocol/heartbeat.rs b/tuic/src/protocol/heartbeat.rs index 7b03ad1..4694444 100644 --- a/tuic/src/protocol/heartbeat.rs +++ b/tuic/src/protocol/heartbeat.rs @@ -25,3 +25,9 @@ impl Command for Heartbeat { 0 } } + +impl From for () { + fn from(hb: Heartbeat) -> Self { + () + } +} diff --git a/tuic/src/protocol/packet.rs b/tuic/src/protocol/packet.rs index c4d2a2b..71c79e5 100644 --- a/tuic/src/protocol/packet.rs +++ b/tuic/src/protocol/packet.rs @@ -36,24 +36,24 @@ impl Packet { } } - pub fn assoc_id(&self) -> u16 { - self.assoc_id + pub fn assoc_id(&self) -> &u16 { + &self.assoc_id } - pub fn pkt_id(&self) -> u16 { - self.pkt_id + pub fn pkt_id(&self) -> &u16 { + &self.pkt_id } - pub fn frag_total(&self) -> u8 { - self.frag_total + pub fn frag_total(&self) -> &u8 { + &self.frag_total } - pub fn frag_id(&self) -> u8 { - self.frag_id + pub fn frag_id(&self) -> &u8 { + &self.frag_id } - pub fn size(&self) -> u16 { - self.size + pub fn size(&self) -> &u16 { + &self.size } pub fn addr(&self) -> &Address { @@ -74,3 +74,16 @@ impl Command for Packet { 2 + 2 + 1 + 1 + 2 + self.addr.len() } } + +impl From for (u16, u16, u8, u8, u16, Address) { + fn from(pkt: Packet) -> Self { + ( + pkt.assoc_id, + pkt.pkt_id, + pkt.frag_total, + pkt.frag_id, + pkt.size, + pkt.addr, + ) + } +} diff --git a/tuic/src/prototype/connect.rs b/tuic/src/prototype/connect.rs index 391c2d0..646ea9b 100644 --- a/tuic/src/prototype/connect.rs +++ b/tuic/src/prototype/connect.rs @@ -17,8 +17,6 @@ struct Tx { _task_reg: TaskRegister, } -struct Rx; - impl Connect { pub(super) fn new(task_reg: TaskRegister, addr: Address) -> Self { Self { @@ -35,3 +33,17 @@ impl Connect { &tx.header } } + +struct Rx { + addr: Address, +} + +impl Connect { + pub(super) fn new(header: ConnectHeader) -> Self { + let (addr,) = header.into(); + Self { + inner: Side::Rx(Rx { addr }), + _marker: side::Rx, + } + } +} diff --git a/tuic/src/prototype/mod.rs b/tuic/src/prototype/mod.rs index 4f8c5e9..2c0b509 100644 --- a/tuic/src/prototype/mod.rs +++ b/tuic/src/prototype/mod.rs @@ -1,4 +1,4 @@ -use crate::protocol::Address; +use crate::protocol::{Address, Connect as ConnectHeader}; use parking_lot::Mutex; use std::{ collections::HashMap, @@ -45,6 +45,10 @@ impl Connection { Connect::::new(self.local_active_task_count.reg(), addr) } + pub fn recv_connect(&self, header: ConnectHeader) -> Connect { + Connect::::new(header) + } + pub fn send_packet( &self, assoc_id: u16,