aboutsummaryrefslogtreecommitdiff
path: root/sip/src/transport/mod.rs
blob: b9cdaebd30fe163e1a6fd2a54e3026b2ef2f0006 (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
use crate::encoding::Message;
use anyhow::Result;
use tcp::TcpTransport;
use udp::UdpTransport;

pub mod tcp;
pub mod udp;

#[allow(async_fn_in_trait)]
pub trait Transport {
    async fn recv(&self) -> Result<Message>;
    async fn send(&self, message: Message) -> Result<()>;
}

pub enum InetTransport {
    Udp(UdpTransport),
    Tcp(TcpTransport),
}
impl Transport for InetTransport {
    async fn recv(&self) -> Result<Message> {
        match self {
            InetTransport::Udp(t) => t.recv().await,
            InetTransport::Tcp(t) => t.recv().await,
        }
    }
    async fn send(&self, message: Message) -> Result<()> {
        match self {
            InetTransport::Udp(t) => t.send(message).await,
            InetTransport::Tcp(t) => t.send(message).await,
        }
    }
}