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
/*
* mtcp - TcpListener/TcpStream *with* timeout/cancellation support
* This is free and unencumbered software released into the public domain.
*/
use std::io::Result as IoResult;
use std::net::{SocketAddr, Shutdown};
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
}
/// Get the *peer* socket address of this TCP connection.
pub fn peer_addr(&self) -> Option<SocketAddr> {
self.stream.peer_addr().ok()
}
/// Get the *local* socket address of this TCP connection.
pub fn local_addr(&self) -> Option<SocketAddr> {
self.stream.local_addr().ok()
}
/// Shuts down the read, write, or both halves of this TCP connection.
pub fn shutdown(&self, how: Shutdown) -> IoResult<()> {
self.stream.shutdown(how)
}
}