]> git.lizzy.rs Git - mt_net.git/blob - src/conn.rs
mt_rudp changes
[mt_net.git] / src / conn.rs
1 use super::PktInfo;
2 use delegate::delegate;
3 use mt_ser::{DefCfg, MtDeserialize, MtSerialize};
4 use std::{borrow::Cow, io};
5 use thiserror::Error;
6
7 pub trait Peer {
8     type UdpPeer: mt_rudp::UdpPeer;
9     type PktFrom: MtDeserialize;
10     type PktTo: MtSerialize + PktInfo;
11 }
12
13 #[cfg(feature = "client")]
14 pub struct RemoteSrv;
15
16 #[cfg(feature = "client")]
17 impl Peer for RemoteSrv {
18     type UdpPeer = mt_rudp::RemoteSrv;
19     type PktTo = crate::ToSrvPkt;
20     type PktFrom = crate::ToCltPkt;
21 }
22
23 #[cfg(feature = "client")]
24 pub async fn connect(addr: &str) -> io::Result<(MtSender<RemoteSrv>, MtReceiver<RemoteSrv>)> {
25     let (tx, rx) = mt_rudp::connect(addr).await?;
26     Ok((MtSender(tx), MtReceiver(rx)))
27 }
28
29 /*
30
31 #[cfg(feature = "server")]
32 pub struct RemoteClt;
33
34 #[cfg(feature = "server")]
35 impl Peer for RemoteClt {
36     type UdpPeer = mt_rudp::RemoteClt;
37     type To = crate::ToCltPkt;
38     type From = crate::ToSrvPkt;
39 }
40
41 */
42
43 pub struct MtSender<P: Peer>(pub mt_rudp::RudpSender<P::UdpPeer>);
44 pub struct MtReceiver<P: Peer>(pub mt_rudp::RudpReceiver<P::UdpPeer>);
45
46 #[derive(Error, Debug)]
47 pub enum RecvError {
48     #[error("connection error: {0}")]
49     ConnError(#[from] mt_rudp::Error),
50     #[error("deserialize error: {0}")]
51     DeserializeError(#[from] mt_ser::DeserializeError),
52 }
53
54 #[derive(Error, Debug)]
55 pub enum SendError {
56     #[error("connection error: {0}")]
57     ConnError(#[from] io::Error),
58     #[error("serialize error: {0}")]
59     SerializeError(#[from] mt_ser::SerializeError),
60 }
61
62 macro_rules! impl_delegate {
63     ($T:ident) => {
64         impl<P: Peer> $T<P> {
65             delegate! {
66                 to self.0 {
67                     pub async fn peer_id(&self) -> u16;
68                     pub async fn is_server(&self) -> bool;
69                     pub async fn close(self);
70                 }
71             }
72         }
73     };
74 }
75
76 impl_delegate!(MtSender);
77 impl_delegate!(MtReceiver);
78
79 impl<P: Peer> MtReceiver<P> {
80     pub async fn recv(&mut self) -> Option<Result<P::PktFrom, RecvError>> {
81         self.0.recv().await.map(|res| {
82             res.map_err(RecvError::from).and_then(|pkt| {
83                 // TODO: warn on trailing data
84                 P::PktFrom::mt_deserialize::<DefCfg>(&mut io::Cursor::new(pkt.data))
85                     .map_err(RecvError::from)
86             })
87         })
88     }
89 }
90
91 impl<P: Peer> MtSender<P> {
92     pub async fn send(&self, pkt: &P::PktTo) -> Result<(), SendError> {
93         let mut writer = Vec::new();
94         pkt.mt_serialize::<DefCfg>(&mut writer)?;
95
96         let (chan, unrel) = pkt.pkt_info();
97         self.0
98             .send(mt_rudp::Pkt {
99                 chan,
100                 unrel,
101                 data: Cow::Borrowed(&writer),
102             })
103             .await?;
104
105         Ok(())
106     }
107 }
108
109 // derive(Clone) adds unwanted trait bound to P
110 impl<P: Peer> Clone for MtSender<P> {
111     fn clone(&self) -> Self {
112         Self(self.0.clone())
113     }
114 }