2 use async_trait::async_trait;
3 use delegate::delegate;
4 use num_enum::TryFromPrimitive;
5 use std::{io, sync::Arc};
8 pub const PROTO_ID: u32 = 0x4f457403;
9 pub const UDP_PKT_SIZE: usize = 512;
10 pub const NUM_CHANS: usize = 3;
11 pub const REL_BUFFER: usize = 0x8000;
12 pub const INIT_SEQNUM: u16 = 65500;
13 pub const TIMEOUT: u64 = 30;
14 pub const PING_TIMEOUT: u64 = 5;
17 pub trait UdpSender: Send + Sync + 'static {
18 async fn send(&self, data: &[u8]) -> io::Result<()>;
22 pub trait UdpReceiver: Send + Sync + 'static {
23 async fn recv(&self) -> io::Result<Vec<u8>>;
26 #[derive(Debug, Copy, Clone, PartialEq)]
34 #[derive(Debug, Copy, Clone, PartialEq, TryFromPrimitive)]
43 #[derive(Debug, Copy, Clone, PartialEq, TryFromPrimitive)]
59 pub type InPkt = Result<Pkt<Vec<u8>>, error::Error>;
62 pub struct RudpReceiver<S: UdpSender> {
63 pub(crate) share: Arc<RudpShare<S>>,
64 pub(crate) pkt_rx: mpsc::UnboundedReceiver<InPkt>,
68 pub struct RudpSender<S: UdpSender> {
69 pub(crate) share: Arc<RudpShare<S>>,
72 macro_rules! impl_share {
74 impl<S: UdpSender> $T<S> {
75 pub async fn peer_id(&self) -> u16 {
79 pub async fn is_server(&self) -> bool {
80 self.share.id == PeerID::Srv as u16
83 pub async fn close(self) {
84 self.share.close_tx.send(true).ok();
86 let mut tasks = self.share.tasks.lock().await;
87 while let Some(res) = tasks.join_next().await {
88 res.ok(); // TODO: handle error (?)
95 impl_share!(RudpReceiver);
96 impl_share!(RudpSender);
98 impl<S: UdpSender> RudpReceiver<S> {
101 pub async fn recv(&mut self) -> Option<InPkt>;