2 use io::{self, Error, ErrorKind, Result, IoVec, IoVecMut};
4 use net::{SocketAddr, Shutdown};
6 use sys::fs::{File, OpenOptions};
7 use sys::syscall::TimeSpec;
8 use sys_common::{AsInner, FromInner, IntoInner};
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 match bufs.iter_mut().find(|b| !b.is_empty()) {
39 Some(buf) => self.read(buf),
44 pub fn write(&self, buf: &[u8]) -> Result<usize> {
48 pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
49 match bufs.iter().find(|b| !b.is_empty()) {
50 Some(buf) => self.write(buf),
55 pub fn take_error(&self) -> Result<Option<Error>> {
59 pub fn peer_addr(&self) -> Result<SocketAddr> {
60 let path = self.0.path()?;
61 Ok(path_to_peer_addr(path.to_str().unwrap_or("")))
64 pub fn socket_addr(&self) -> Result<SocketAddr> {
65 let path = self.0.path()?;
66 Ok(path_to_local_addr(path.to_str().unwrap_or("")))
69 pub fn peek(&self, _buf: &mut [u8]) -> Result<usize> {
70 Err(Error::new(ErrorKind::Other, "TcpStream::peek not implemented"))
73 pub fn shutdown(&self, _how: Shutdown) -> Result<()> {
74 Err(Error::new(ErrorKind::Other, "TcpStream::shutdown not implemented"))
77 pub fn nodelay(&self) -> Result<bool> {
78 Err(Error::new(ErrorKind::Other, "TcpStream::nodelay not implemented"))
81 pub fn nonblocking(&self) -> Result<bool> {
82 self.0.fd().nonblocking()
85 pub fn only_v6(&self) -> Result<bool> {
86 Err(Error::new(ErrorKind::Other, "TcpStream::only_v6 not implemented"))
89 pub fn ttl(&self) -> Result<u32> {
91 let file = self.0.dup(b"ttl")?;
96 pub fn read_timeout(&self) -> Result<Option<Duration>> {
97 let mut time = TimeSpec::default();
98 let file = self.0.dup(b"read_timeout")?;
99 if file.read(&mut time)? >= mem::size_of::<TimeSpec>() {
100 Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32)))
106 pub fn write_timeout(&self) -> Result<Option<Duration>> {
107 let mut time = TimeSpec::default();
108 let file = self.0.dup(b"write_timeout")?;
109 if file.read(&mut time)? >= mem::size_of::<TimeSpec>() {
110 Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32)))
116 pub fn set_nodelay(&self, _nodelay: bool) -> Result<()> {
117 Err(Error::new(ErrorKind::Other, "TcpStream::set_nodelay not implemented"))
120 pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
121 self.0.fd().set_nonblocking(nonblocking)
124 pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
125 Err(Error::new(ErrorKind::Other, "TcpStream::set_only_v6 not implemented"))
128 pub fn set_ttl(&self, ttl: u32) -> Result<()> {
129 let file = self.0.dup(b"ttl")?;
130 file.write(&[cmp::min(ttl, 255) as u8])?;
134 pub fn set_read_timeout(&self, duration_option: Option<Duration>) -> Result<()> {
135 let file = self.0.dup(b"read_timeout")?;
136 if let Some(duration) = duration_option {
137 if duration.as_secs() == 0 && duration.subsec_nanos() == 0 {
138 return Err(io::Error::new(io::ErrorKind::InvalidInput,
139 "cannot set a 0 duration timeout"));
141 file.write(&TimeSpec {
142 tv_sec: duration.as_secs() as i64,
143 tv_nsec: duration.subsec_nanos() as i32
151 pub fn set_write_timeout(&self, duration_option: Option<Duration>) -> Result<()> {
152 let file = self.0.dup(b"write_timeout")?;
153 if let Some(duration) = duration_option {
154 if duration.as_secs() == 0 && duration.subsec_nanos() == 0 {
155 return Err(io::Error::new(io::ErrorKind::InvalidInput,
156 "cannot set a 0 duration timeout"));
158 file.write(&TimeSpec {
159 tv_sec: duration.as_secs() as i64,
160 tv_nsec: duration.subsec_nanos() as i32
169 impl AsInner<File> for TcpStream {
170 fn as_inner(&self) -> &File { &self.0 }
173 impl FromInner<File> for TcpStream {
174 fn from_inner(file: File) -> TcpStream {
179 impl IntoInner<File> for TcpStream {
180 fn into_inner(self) -> File { self.0 }
184 pub struct TcpListener(File);
187 pub fn bind(addr: Result<&SocketAddr>) -> Result<TcpListener> {
188 let path = format!("tcp:/{}", addr?);
189 let mut options = OpenOptions::new();
192 Ok(TcpListener(File::open(Path::new(path.as_str()), &options)?))
195 pub fn accept(&self) -> Result<(TcpStream, SocketAddr)> {
196 let file = self.0.dup(b"listen")?;
197 let path = file.path()?;
198 let peer_addr = path_to_peer_addr(path.to_str().unwrap_or(""));
199 Ok((TcpStream(file), peer_addr))
202 pub fn duplicate(&self) -> Result<TcpListener> {
203 Ok(TcpListener(self.0.dup(&[])?))
206 pub fn take_error(&self) -> Result<Option<Error>> {
210 pub fn socket_addr(&self) -> Result<SocketAddr> {
211 let path = self.0.path()?;
212 Ok(path_to_local_addr(path.to_str().unwrap_or("")))
215 pub fn nonblocking(&self) -> Result<bool> {
216 Err(Error::new(ErrorKind::Other, "TcpListener::nonblocking not implemented"))
219 pub fn only_v6(&self) -> Result<bool> {
220 Err(Error::new(ErrorKind::Other, "TcpListener::only_v6 not implemented"))
223 pub fn ttl(&self) -> Result<u32> {
225 let file = self.0.dup(b"ttl")?;
226 file.read(&mut ttl)?;
230 pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
231 Err(Error::new(ErrorKind::Other, "TcpListener::set_nonblocking not implemented"))
234 pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
235 Err(Error::new(ErrorKind::Other, "TcpListener::set_only_v6 not implemented"))
238 pub fn set_ttl(&self, ttl: u32) -> Result<()> {
239 let file = self.0.dup(b"ttl")?;
240 file.write(&[cmp::min(ttl, 255) as u8])?;
245 impl AsInner<File> for TcpListener {
246 fn as_inner(&self) -> &File { &self.0 }
249 impl FromInner<File> for TcpListener {
250 fn from_inner(file: File) -> TcpListener {
255 impl IntoInner<File> for TcpListener {
256 fn into_inner(self) -> File { self.0 }