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
/*
 * mtcp - TcpListener/TcpStream *with* timeout/cancellation support
 * This is free and unencumbered software released into the public domain.
 */
use std::net::SocketAddr;

use mio::net::TcpStream as MioTcpStream;

/// A pending incoming TCP connection, usually used to initialize a new
/// [`mtcp_rs::TcpStream`](crate::TcpStream)
/// 
/// Unlike an `mtcp_rs::TcpStream` instance, the `mtcp_rs::TcpConnection`
/// instance is **not** yet tied to a
/// [`mtcp_rs::TcpManager`](crate::TcpManager) instance and can therefore
/// safely be moved across the thread boundary.
#[derive(Debug)]
pub struct TcpConnection {
    stream: MioTcpStream,
}

impl TcpConnection {
    pub(crate) fn new(stream: MioTcpStream) -> Self {
        Self {
            stream,
        }
    }

    pub(crate) fn stream(self) -> MioTcpStream {
        self.stream
    }

    pub fn peer_addr(&self) -> Option<SocketAddr> {
        self.stream.peer_addr().ok()
    }

    pub fn local_addr(&self) -> Option<SocketAddr> {
        self.stream.local_addr().ok()
    }
}