summaryrefslogtreecommitdiff
path: root/src/conn.rs
blob: ec823f641bb534e6e8cd40fd56711705d8a9994e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use super::PktInfo;
use async_trait::async_trait;
use mt_rudp::Pkt;
pub use mt_rudp::{Ack, Error as RudpError};
use mt_ser::{DefCfg, MtDeserialize, MtSerialize};
use std::{borrow::Cow, io};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RecvError {
    #[error("connection error: {0}")]
    ConnError(#[from] RudpError),
    #[error("deserialize error: {0}")]
    DeserializeError(#[from] mt_ser::DeserializeError),
}

#[derive(Error, Debug)]
pub enum SendError {
    #[error("connection error: {0}")]
    ConnError(#[from] io::Error),
    #[error("serialize error: {0}")]
    SerializeError(#[from] mt_ser::SerializeError),
}

#[async_trait]
pub trait SenderExt {
    type Pkt: MtSerialize + PktInfo + Send + Sync;

    async fn send_raw(&self, pkt: Pkt<'_>) -> io::Result<Ack>;
    async fn send(&self, pkt: &Self::Pkt) -> Result<Ack, SendError> {
        let mut writer = Vec::new();
        pkt.mt_serialize::<DefCfg>(&mut writer)?;

        let (chan, unrel) = pkt.pkt_info();
        Ok(self
            .send_raw(Pkt {
                chan,
                unrel,
                data: Cow::Borrowed(&writer),
            })
            .await?)
    }
}

#[async_trait]
pub trait ReceiverExt {
    type Pkt: MtDeserialize;

    async fn recv_raw(&mut self) -> Option<Result<Pkt<'static>, RudpError>>;
    async fn recv(&mut self) -> Option<Result<Self::Pkt, RecvError>> {
        self.recv_raw().await.map(|res| {
            res.map_err(RecvError::from).and_then(|pkt| {
                // TODO: warn on trailing data
                Self::Pkt::mt_deserialize::<DefCfg>(&mut io::Cursor::new(pkt.data))
                    .map_err(RecvError::from)
            })
        })
    }
}

#[cfg(feature = "client")]
pub use mt_rudp::{connect, CltReceiver, CltSender, CltWorker};

#[cfg(feature = "client")]
#[async_trait]
impl ReceiverExt for CltReceiver {
    type Pkt = crate::ToCltPkt;

    async fn recv_raw(&mut self) -> Option<Result<Pkt<'static>, RudpError>> {
        self.recv_rudp().await
    }
}

#[cfg(feature = "client")]
#[async_trait]
impl SenderExt for CltSender {
    type Pkt = crate::ToSrvPkt;

    async fn send_raw(&self, pkt: Pkt<'_>) -> io::Result<Ack> {
        self.send_rudp(pkt).await
    }
}