From 718e8618544c4cdde78138655305eee7c08058ee Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Sat, 18 Feb 2023 03:03:40 +0100 Subject: Don't spawn tasks --- src/common.rs | 73 ++++++++--------------------------------------------------- 1 file changed, 9 insertions(+), 64 deletions(-) (limited to 'src/common.rs') diff --git a/src/common.rs b/src/common.rs index 4c0bc08..0ed08f3 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,9 +1,6 @@ -use super::*; use async_trait::async_trait; -use delegate::delegate; use num_enum::TryFromPrimitive; -use std::{borrow::Cow, io, sync::Arc}; -use tokio::sync::mpsc; +use std::{borrow::Cow, fmt::Debug, io}; pub const PROTO_ID: u32 = 0x4f457403; pub const UDP_PKT_SIZE: usize = 512; @@ -14,13 +11,18 @@ pub const TIMEOUT: u64 = 30; pub const PING_TIMEOUT: u64 = 5; #[async_trait] -pub trait UdpSender: Send + Sync + 'static { +pub trait UdpSender: Send + Sync { async fn send(&self, data: &[u8]) -> io::Result<()>; } #[async_trait] -pub trait UdpReceiver: Send + Sync + 'static { - async fn recv(&self) -> io::Result>; +pub trait UdpReceiver: Send { + async fn recv(&mut self) -> io::Result>; +} + +pub trait UdpPeer { + type Sender: UdpSender; + type Receiver: UdpReceiver; } #[derive(Debug, Copy, Clone, PartialEq)] @@ -55,60 +57,3 @@ pub struct Pkt<'a> { pub chan: u8, pub data: Cow<'a, [u8]>, } - -pub type InPkt = Result, Error>; - -#[derive(Debug)] -pub struct RudpReceiver { - pub(crate) share: Arc>, - pub(crate) pkt_rx: mpsc::UnboundedReceiver, -} - -#[derive(Debug)] -pub struct RudpSender { - pub(crate) share: Arc>, -} - -// derive(Clone) adds unwanted Clone trait bound to S parameter -impl Clone for RudpSender { - fn clone(&self) -> Self { - Self { - share: Arc::clone(&self.share), - } - } -} - -macro_rules! impl_share { - ($T:ident) => { - impl $T { - pub async fn peer_id(&self) -> u16 { - self.share.id - } - - pub async fn is_server(&self) -> bool { - self.share.id == PeerID::Srv as u16 - } - - pub async fn close(self) { - self.share.bomb.lock().await.defuse(); - self.share.close_tx.send(true).ok(); - - let mut tasks = self.share.tasks.lock().await; - while let Some(res) = tasks.join_next().await { - res.ok(); // TODO: handle error (?) - } - } - } - }; -} - -impl_share!(RudpReceiver); -impl_share!(RudpSender); - -impl RudpReceiver { - delegate! { - to self.pkt_rx { - pub async fn recv(&mut self) -> Option; - } - } -} -- cgit v1.2.3