]> git.lizzy.rs Git - mt_rudp.git/blob - src/common.rs
Don't spawn tasks
[mt_rudp.git] / src / common.rs
1 use async_trait::async_trait;
2 use num_enum::TryFromPrimitive;
3 use std::{borrow::Cow, fmt::Debug, io};
4
5 pub const PROTO_ID: u32 = 0x4f457403;
6 pub const UDP_PKT_SIZE: usize = 512;
7 pub const NUM_CHANS: usize = 3;
8 pub const REL_BUFFER: usize = 0x8000;
9 pub const INIT_SEQNUM: u16 = 65500;
10 pub const TIMEOUT: u64 = 30;
11 pub const PING_TIMEOUT: u64 = 5;
12
13 #[async_trait]
14 pub trait UdpSender: Send + Sync {
15     async fn send(&self, data: &[u8]) -> io::Result<()>;
16 }
17
18 #[async_trait]
19 pub trait UdpReceiver: Send {
20     async fn recv(&mut self) -> io::Result<Vec<u8>>;
21 }
22
23 pub trait UdpPeer {
24     type Sender: UdpSender;
25     type Receiver: UdpReceiver;
26 }
27
28 #[derive(Debug, Copy, Clone, PartialEq)]
29 #[repr(u16)]
30 pub enum PeerID {
31     Nil = 0,
32     Srv,
33     CltMin,
34 }
35
36 #[derive(Debug, Copy, Clone, PartialEq, TryFromPrimitive)]
37 #[repr(u8)]
38 pub enum PktType {
39     Ctl = 0,
40     Orig,
41     Split,
42     Rel,
43 }
44
45 #[derive(Debug, Copy, Clone, PartialEq, TryFromPrimitive)]
46 #[repr(u8)]
47 pub enum CtlType {
48     Ack = 0,
49     SetPeerID,
50     Ping,
51     Disco,
52 }
53
54 #[derive(Debug)]
55 pub struct Pkt<'a> {
56     pub unrel: bool,
57     pub chan: u8,
58     pub data: Cow<'a, [u8]>,
59 }