aboutsummaryrefslogtreecommitdiff
path: root/net/src/transports/tcp.rs
blob: 5ff7b28ca2e4b70d0bea192ec893bb785e31be33 (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 async_trait::async_trait;

use smol::{
    io::{split, AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf},
    lock::Mutex,
    net::{TcpListener, TcpStream},
};

use crate::{
    connection::Connection,
    endpoint::{Addr, Endpoint, Port},
    listener::Listener,
    Result,
};

/// TCP network connection implementations of the `Connection` trait.
pub struct TcpConn {
    inner: TcpStream,
    read: Mutex<ReadHalf<TcpStream>>,
    write: Mutex<WriteHalf<TcpStream>>,
}

impl TcpConn {
    /// Creates a new TcpConn
    pub fn new(conn: TcpStream) -> Self {
        let (read, write) = split(conn.clone());
        Self {
            inner: conn,
            read: Mutex::new(read),
            write: Mutex::new(write),
        }
    }
}

#[async_trait]
impl Connection for TcpConn {
    fn peer_endpoint(&self) -> Result<Endpoint> {
        Ok(Endpoint::new_tcp_addr(&self.inner.peer_addr()?))
    }

    fn local_endpoint(&self) -> Result<Endpoint> {
        Ok(Endpoint::new_tcp_addr(&self.inner.local_addr()?))
    }

    async fn recv(&self, buf: &mut [u8]) -> Result<usize> {
        self.read.lock().await.read_exact(buf).await?;
        Ok(buf.len())
    }

    async fn send(&self, buf: &[u8]) -> Result<usize> {
        self.write.lock().await.write_all(buf).await?;
        Ok(buf.len())
    }
}

#[async_trait]
impl Listener for TcpListener {
    fn local_endpoint(&self) -> Result<Endpoint> {
        Ok(Endpoint::new_tcp_addr(&self.local_addr()?))
    }

    async fn accept(&self) -> Result<Box<dyn Connection>> {
        let (conn, _) = self.accept().await?;
        conn.set_nodelay(true)?;
        Ok(Box::new(TcpConn::new(conn)))
    }
}

/// Connects to the given TCP address and port.
pub async fn dial_tcp(addr: &Addr, port: &Port) -> Result<TcpConn> {
    let address = format!("{}:{}", addr, port);
    let conn = TcpStream::connect(address).await?;
    conn.set_nodelay(true)?;
    Ok(TcpConn::new(conn))
}

/// Listens on the given TCP address and port.
pub async fn listen_tcp(addr: &Addr, port: &Port) -> Result<TcpListener> {
    let address = format!("{}:{}", addr, port);
    let listener = TcpListener::bind(address).await?;
    Ok(listener)
}