2 use crate::io::{self, Error, ErrorKind, Result, IoVec, IoVecMut};
4 use crate::net::{SocketAddr, Shutdown};
6 use crate::sys::fs::{File, OpenOptions};
7 use crate::sys::syscall::TimeSpec;
8 use crate::sys_common::{AsInner, FromInner, IntoInner};
9 use crate::time::Duration;
11 use super::{path_to_peer_addr, path_to_local_addr};
14 pub struct TcpStream(File);
17 pub fn connect(addr: Result<&SocketAddr>) -> Result<TcpStream> {
18 let path = format!("tcp:{}", addr?);
19 let mut options = OpenOptions::new();
22 Ok(TcpStream(File::open(Path::new(path.as_str()), &options)?))
25 pub fn connect_timeout(_addr: &SocketAddr, _timeout: Duration) -> Result<TcpStream> {
26 Err(Error::new(ErrorKind::Other, "TcpStream::connect_timeout not implemented"))
29 pub fn duplicate(&self) -> Result<TcpStream> {
30 Ok(TcpStream(self.0.dup(&[])?))
33 pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
37 pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
38 io::default_read_vectored(|b| self.read(b), bufs)
41 pub fn write(&self, buf: &[u8]) -> Result<usize> {
45 pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
46 io::default_write_vectored(|b| self.write(b), bufs)
49 pub fn take_error(&self) -> Result<Option<Error>> {
53 pub fn peer_addr(&self) -> Result<SocketAddr> {
54 let path = self.0.path()?;
55 Ok(path_to_peer_addr(path.to_str().unwrap_or("")))
58 pub fn socket_addr(&self) -> Result<SocketAddr> {
59 let path = self.0.path()?;
60 Ok(path_to_local_addr(path.to_str().unwrap_or("")))
63 pub fn peek(&self, _buf: &mut [u8]) -> Result<usize> {
64 Err(Error::new(ErrorKind::Other, "TcpStream::peek not implemented"))
67 pub fn shutdown(&self, _how: Shutdown) -> Result<()> {
68 Err(Error::new(ErrorKind::Other, "TcpStream::shutdown not implemented"))
71 pub fn nodelay(&self) -> Result<bool> {
72 Err(Error::new(ErrorKind::Other, "TcpStream::nodelay not implemented"))
75 pub fn nonblocking(&self) -> Result<bool> {
76 self.0.fd().nonblocking()
79 pub fn only_v6(&self) -> Result<bool> {
80 Err(Error::new(ErrorKind::Other, "TcpStream::only_v6 not implemented"))
83 pub fn ttl(&self) -> Result<u32> {
85 let file = self.0.dup(b"ttl")?;
90 pub fn read_timeout(&self) -> Result<Option<Duration>> {
91 let mut time = TimeSpec::default();
92 let file = self.0.dup(b"read_timeout")?;
93 if file.read(&mut time)? >= mem::size_of::<TimeSpec>() {
94 Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32)))
100 pub fn write_timeout(&self) -> Result<Option<Duration>> {
101 let mut time = TimeSpec::default();
102 let file = self.0.dup(b"write_timeout")?;
103 if file.read(&mut time)? >= mem::size_of::<TimeSpec>() {
104 Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32)))
110 pub fn set_nodelay(&self, _nodelay: bool) -> Result<()> {
111 Err(Error::new(ErrorKind::Other, "TcpStream::set_nodelay not implemented"))
114 pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
115 self.0.fd().set_nonblocking(nonblocking)
118 pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
119 Err(Error::new(ErrorKind::Other, "TcpStream::set_only_v6 not implemented"))
122 pub fn set_ttl(&self, ttl: u32) -> Result<()> {
123 let file = self.0.dup(b"ttl")?;
124 file.write(&[cmp::min(ttl, 255) as u8])?;
128 pub fn set_read_timeout(&self, duration_option: Option<Duration>) -> Result<()> {
129 let file = self.0.dup(b"read_timeout")?;
130 if let Some(duration) = duration_option {
131 if duration.as_secs() == 0 && duration.subsec_nanos() == 0 {
132 return Err(io::Error::new(io::ErrorKind::InvalidInput,
133 "cannot set a 0 duration timeout"));
135 file.write(&TimeSpec {
136 tv_sec: duration.as_secs() as i64,
137 tv_nsec: duration.subsec_nanos() as i32
145 pub fn set_write_timeout(&self, duration_option: Option<Duration>) -> Result<()> {
146 let file = self.0.dup(b"write_timeout")?;
147 if let Some(duration) = duration_option {
148 if duration.as_secs() == 0 && duration.subsec_nanos() == 0 {
149 return Err(io::Error::new(io::ErrorKind::InvalidInput,
150 "cannot set a 0 duration timeout"));
152 file.write(&TimeSpec {
153 tv_sec: duration.as_secs() as i64,
154 tv_nsec: duration.subsec_nanos() as i32
163 impl AsInner<File> for TcpStream {
164 fn as_inner(&self) -> &File { &self.0 }
167 impl FromInner<File> for TcpStream {
168 fn from_inner(file: File) -> TcpStream {
173 impl IntoInner<File> for TcpStream {
174 fn into_inner(self) -> File { self.0 }
178 pub struct TcpListener(File);
181 pub fn bind(addr: Result<&SocketAddr>) -> Result<TcpListener> {
182 let path = format!("tcp:/{}", addr?);
183 let mut options = OpenOptions::new();
186 Ok(TcpListener(File::open(Path::new(path.as_str()), &options)?))
189 pub fn accept(&self) -> Result<(TcpStream, SocketAddr)> {
190 let file = self.0.dup(b"listen")?;
191 let path = file.path()?;
192 let peer_addr = path_to_peer_addr(path.to_str().unwrap_or(""));
193 Ok((TcpStream(file), peer_addr))
196 pub fn duplicate(&self) -> Result<TcpListener> {
197 Ok(TcpListener(self.0.dup(&[])?))
200 pub fn take_error(&self) -> Result<Option<Error>> {
204 pub fn socket_addr(&self) -> Result<SocketAddr> {
205 let path = self.0.path()?;
206 Ok(path_to_local_addr(path.to_str().unwrap_or("")))
209 pub fn nonblocking(&self) -> Result<bool> {
210 Err(Error::new(ErrorKind::Other, "TcpListener::nonblocking not implemented"))
213 pub fn only_v6(&self) -> Result<bool> {
214 Err(Error::new(ErrorKind::Other, "TcpListener::only_v6 not implemented"))
217 pub fn ttl(&self) -> Result<u32> {
219 let file = self.0.dup(b"ttl")?;
220 file.read(&mut ttl)?;
224 pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
225 Err(Error::new(ErrorKind::Other, "TcpListener::set_nonblocking not implemented"))
228 pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
229 Err(Error::new(ErrorKind::Other, "TcpListener::set_only_v6 not implemented"))
232 pub fn set_ttl(&self, ttl: u32) -> Result<()> {
233 let file = self.0.dup(b"ttl")?;
234 file.write(&[cmp::min(ttl, 255) as u8])?;
239 impl AsInner<File> for TcpListener {
240 fn as_inner(&self) -> &File { &self.0 }
243 impl FromInner<File> for TcpListener {
244 fn from_inner(file: File) -> TcpListener {
249 impl IntoInner<File> for TcpListener {
250 fn into_inner(self) -> File { self.0 }