1
0

adding recv methods for auth & dissoc & heartbeat

This commit is contained in:
EAimTY 2023-01-25 18:37:04 +09:00
parent f175aad8e8
commit ceebbdb200
5 changed files with 77 additions and 16 deletions

View File

@ -10,8 +10,6 @@ pub struct Tx {
header: Header, header: Header,
} }
pub struct Rx;
impl Authenticate<side::Tx> { impl Authenticate<side::Tx> {
pub(super) fn new(token: [u8; 8]) -> Self { pub(super) fn new(token: [u8; 8]) -> Self {
Self { Self {
@ -27,3 +25,21 @@ impl Authenticate<side::Tx> {
&tx.header &tx.header
} }
} }
pub struct Rx {
token: [u8; 8],
}
impl Authenticate<side::Rx> {
pub(super) fn new(token: [u8; 8]) -> Self {
Self {
inner: Side::Rx(Rx { token }),
_marker: side::Rx,
}
}
pub fn token(&self) -> &[u8; 8] {
let Side::Rx(rx) = &self.inner else { unreachable!() };
&rx.token
}
}

View File

@ -10,8 +10,6 @@ pub struct Tx {
header: Header, header: Header,
} }
pub struct Rx;
impl Dissociate<side::Tx> { impl Dissociate<side::Tx> {
pub(super) fn new(assoc_id: u16) -> Self { pub(super) fn new(assoc_id: u16) -> Self {
Self { Self {
@ -27,3 +25,21 @@ impl Dissociate<side::Tx> {
&tx.header &tx.header
} }
} }
pub struct Rx {
assoc_id: u16,
}
impl Dissociate<side::Rx> {
pub(super) fn new(assoc_id: u16) -> Self {
Self {
inner: Side::Rx(Rx { assoc_id }),
_marker: side::Rx,
}
}
pub fn assoc_id(&self) -> &u16 {
let Side::Rx(rx) = &self.inner else { unreachable!() };
&rx.assoc_id
}
}

View File

@ -10,8 +10,6 @@ pub struct Tx {
header: Header, header: Header,
} }
pub struct Rx;
impl Heartbeat<side::Tx> { impl Heartbeat<side::Tx> {
pub(super) fn new() -> Self { pub(super) fn new() -> Self {
Self { Self {
@ -27,3 +25,14 @@ impl Heartbeat<side::Tx> {
&tx.header &tx.header
} }
} }
pub struct Rx;
impl Heartbeat<side::Rx> {
pub(super) fn new() -> Self {
Self {
inner: Side::Rx(Rx),
_marker: side::Rx,
}
}
}

View File

@ -1,4 +1,7 @@
use crate::protocol::{Address, Connect as ConnectHeader, Packet as PacketHeader}; use crate::protocol::{
Address, Authenticate as AuthenticateHeader, Connect as ConnectHeader,
Dissociate as DissociateHeader, Heartbeat as HeartbeatHeader, Packet as PacketHeader,
};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -45,7 +48,12 @@ where
} }
pub fn send_authenticate(&self, token: [u8; 8]) -> Authenticate<side::Tx> { pub fn send_authenticate(&self, token: [u8; 8]) -> Authenticate<side::Tx> {
Authenticate::new(token) Authenticate::<side::Tx>::new(token)
}
pub fn recv_authenticate(&self, header: AuthenticateHeader) -> Authenticate<side::Rx> {
let (token,) = header.into();
Authenticate::<side::Rx>::new(token)
} }
pub fn send_connect(&self, addr: Address) -> Connect<side::Tx> { pub fn send_connect(&self, addr: Address) -> Connect<side::Tx> {
@ -82,11 +90,21 @@ where
} }
pub fn send_dissociate(&self, assoc_id: u16) -> Dissociate<side::Tx> { pub fn send_dissociate(&self, assoc_id: u16) -> Dissociate<side::Tx> {
self.udp_sessions.lock().dissociate(assoc_id) self.udp_sessions.lock().send_dissociate(assoc_id)
}
pub fn recv_dissociate(&self, header: DissociateHeader) -> Dissociate<side::Rx> {
let (assoc_id,) = header.into();
self.udp_sessions.lock().recv_dissociate(assoc_id)
} }
pub fn send_heartbeat(&self) -> Heartbeat<side::Tx> { pub fn send_heartbeat(&self) -> Heartbeat<side::Tx> {
Heartbeat::new() Heartbeat::<side::Tx>::new()
}
pub fn recv_heartbeat(&self, header: HeartbeatHeader) -> Heartbeat<side::Rx> {
let () = header.into();
Heartbeat::<side::Rx>::new()
} }
pub fn task_connect_count(&self) -> usize { pub fn task_connect_count(&self) -> usize {
@ -174,9 +192,14 @@ where
.recv_packet(sessions, assoc_id, pkt_id, frag_total, frag_id, size, addr) .recv_packet(sessions, assoc_id, pkt_id, frag_total, frag_id, size, addr)
} }
fn dissociate(&mut self, assoc_id: u16) -> Dissociate<side::Tx> { fn send_dissociate(&mut self, assoc_id: u16) -> Dissociate<side::Tx> {
self.sessions.remove(&assoc_id); self.sessions.remove(&assoc_id);
Dissociate::new(assoc_id) Dissociate::<side::Tx>::new(assoc_id)
}
fn recv_dissociate(&mut self, assoc_id: u16) -> Dissociate<side::Rx> {
self.sessions.remove(&assoc_id);
Dissociate::<side::Rx>::new(assoc_id)
} }
fn insert<A>( fn insert<A>(

View File

@ -18,10 +18,7 @@ pub struct Tx {
max_pkt_size: usize, max_pkt_size: usize,
} }
impl<B> Packet<side::Tx, B> impl<B> Packet<side::Tx, B> {
where
B: AsRef<[u8]>,
{
pub(super) fn new(assoc_id: u16, pkt_id: u16, addr: Address, max_pkt_size: usize) -> Self { pub(super) fn new(assoc_id: u16, pkt_id: u16, addr: Address, max_pkt_size: usize) -> Self {
Self { Self {
inner: Side::Tx(Tx { inner: Side::Tx(Tx {