mtcp_rs/
connection.rs

1/*
2 * mtcp - TcpListener/TcpStream *with* timeout/cancellation support
3 * This is free and unencumbered software released into the public domain.
4 */
5use std::io::Result as IoResult;
6use std::net::{SocketAddr, Shutdown};
7
8use mio::net::TcpStream as MioTcpStream;
9
10/// A pending incoming TCP connection, usually used to initialize a new
11/// [`mtcp_rs::TcpStream`](crate::TcpStream)
12/// 
13/// Unlike an `mtcp_rs::TcpStream` instance, the `mtcp_rs::TcpConnection`
14/// instance is **not** yet tied to a
15/// [`mtcp_rs::TcpManager`](crate::TcpManager) instance and can therefore
16/// safely be moved across the thread boundary.
17#[derive(Debug)]
18pub struct TcpConnection {
19    stream: MioTcpStream,
20}
21
22impl TcpConnection {
23    pub(crate) fn new(stream: MioTcpStream) -> Self {
24        Self {
25            stream,
26        }
27    }
28
29    pub(crate) fn stream(self) -> MioTcpStream {
30        self.stream
31    }
32
33    /// Get the *peer* socket address of this TCP connection.
34    pub fn peer_addr(&self) -> Option<SocketAddr> {
35        self.stream.peer_addr().ok()
36    }
37
38    /// Get the *local* socket address of this TCP connection.
39    pub fn local_addr(&self) -> Option<SocketAddr> {
40        self.stream.local_addr().ok()
41    }
42
43    /// Shuts down the read, write, or both halves of this TCP connection.
44    pub fn shutdown(&self, how: Shutdown) -> IoResult<()> {
45        self.stream.shutdown(how)
46    }
47}