]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/net/tcp.rs
Always call read/write from default vectored io methods
[rust.git] / src / libstd / sys / redox / net / tcp.rs
1 use crate::cmp;
2 use crate::io::{self, Error, ErrorKind, Result, IoVec, IoVecMut};
3 use crate::mem;
4 use crate::net::{SocketAddr, Shutdown};
5 use crate::path::Path;
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;
10
11 use super::{path_to_peer_addr, path_to_local_addr};
12
13 #[derive(Debug)]
14 pub struct TcpStream(File);
15
16 impl TcpStream {
17     pub fn connect(addr: Result<&SocketAddr>) -> Result<TcpStream> {
18         let path = format!("tcp:{}", addr?);
19         let mut options = OpenOptions::new();
20         options.read(true);
21         options.write(true);
22         Ok(TcpStream(File::open(Path::new(path.as_str()), &options)?))
23     }
24
25     pub fn connect_timeout(_addr: &SocketAddr, _timeout: Duration) -> Result<TcpStream> {
26         Err(Error::new(ErrorKind::Other, "TcpStream::connect_timeout not implemented"))
27     }
28
29     pub fn duplicate(&self) -> Result<TcpStream> {
30         Ok(TcpStream(self.0.dup(&[])?))
31     }
32
33     pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
34         self.0.read(buf)
35     }
36
37     pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
38         io::default_read_vectored(|b| self.read(b), bufs)
39     }
40
41     pub fn write(&self, buf: &[u8]) -> Result<usize> {
42         self.0.write(buf)
43     }
44
45     pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
46         io::default_write_vectored(|b| self.write(b), bufs)
47     }
48
49     pub fn take_error(&self) -> Result<Option<Error>> {
50         Ok(None)
51     }
52
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("")))
56     }
57
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("")))
61     }
62
63     pub fn peek(&self, _buf: &mut [u8]) -> Result<usize> {
64         Err(Error::new(ErrorKind::Other, "TcpStream::peek not implemented"))
65     }
66
67     pub fn shutdown(&self, _how: Shutdown) -> Result<()> {
68         Err(Error::new(ErrorKind::Other, "TcpStream::shutdown not implemented"))
69     }
70
71     pub fn nodelay(&self) -> Result<bool> {
72         Err(Error::new(ErrorKind::Other, "TcpStream::nodelay not implemented"))
73     }
74
75     pub fn nonblocking(&self) -> Result<bool> {
76         self.0.fd().nonblocking()
77     }
78
79     pub fn only_v6(&self) -> Result<bool> {
80         Err(Error::new(ErrorKind::Other, "TcpStream::only_v6 not implemented"))
81     }
82
83     pub fn ttl(&self) -> Result<u32> {
84         let mut ttl = [0];
85         let file = self.0.dup(b"ttl")?;
86         file.read(&mut ttl)?;
87         Ok(ttl[0] as u32)
88     }
89
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)))
95         } else {
96             Ok(None)
97         }
98     }
99
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)))
105         } else {
106             Ok(None)
107         }
108     }
109
110     pub fn set_nodelay(&self, _nodelay: bool) -> Result<()> {
111         Err(Error::new(ErrorKind::Other, "TcpStream::set_nodelay not implemented"))
112     }
113
114     pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
115         self.0.fd().set_nonblocking(nonblocking)
116     }
117
118     pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
119         Err(Error::new(ErrorKind::Other, "TcpStream::set_only_v6 not implemented"))
120     }
121
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])?;
125         Ok(())
126     }
127
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"));
134             }
135             file.write(&TimeSpec {
136                 tv_sec: duration.as_secs() as i64,
137                 tv_nsec: duration.subsec_nanos() as i32
138             })?;
139         } else {
140             file.write(&[])?;
141         }
142         Ok(())
143     }
144
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"));
151             }
152             file.write(&TimeSpec {
153                 tv_sec: duration.as_secs() as i64,
154                 tv_nsec: duration.subsec_nanos() as i32
155             })?;
156         } else {
157             file.write(&[])?;
158         }
159         Ok(())
160     }
161 }
162
163 impl AsInner<File> for TcpStream {
164     fn as_inner(&self) -> &File { &self.0 }
165 }
166
167 impl FromInner<File> for TcpStream {
168     fn from_inner(file: File) -> TcpStream {
169         TcpStream(file)
170     }
171 }
172
173 impl IntoInner<File> for TcpStream {
174     fn into_inner(self) -> File { self.0 }
175 }
176
177 #[derive(Debug)]
178 pub struct TcpListener(File);
179
180 impl TcpListener {
181     pub fn bind(addr: Result<&SocketAddr>) -> Result<TcpListener> {
182         let path = format!("tcp:/{}", addr?);
183         let mut options = OpenOptions::new();
184         options.read(true);
185         options.write(true);
186         Ok(TcpListener(File::open(Path::new(path.as_str()), &options)?))
187     }
188
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))
194     }
195
196     pub fn duplicate(&self) -> Result<TcpListener> {
197         Ok(TcpListener(self.0.dup(&[])?))
198     }
199
200     pub fn take_error(&self) -> Result<Option<Error>> {
201         Ok(None)
202     }
203
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("")))
207     }
208
209     pub fn nonblocking(&self) -> Result<bool> {
210         Err(Error::new(ErrorKind::Other, "TcpListener::nonblocking not implemented"))
211     }
212
213     pub fn only_v6(&self) -> Result<bool> {
214         Err(Error::new(ErrorKind::Other, "TcpListener::only_v6 not implemented"))
215     }
216
217     pub fn ttl(&self) -> Result<u32> {
218         let mut ttl = [0];
219         let file = self.0.dup(b"ttl")?;
220         file.read(&mut ttl)?;
221         Ok(ttl[0] as u32)
222     }
223
224     pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
225         Err(Error::new(ErrorKind::Other, "TcpListener::set_nonblocking not implemented"))
226     }
227
228     pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
229         Err(Error::new(ErrorKind::Other, "TcpListener::set_only_v6 not implemented"))
230     }
231
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])?;
235         Ok(())
236     }
237 }
238
239 impl AsInner<File> for TcpListener {
240     fn as_inner(&self) -> &File { &self.0 }
241 }
242
243 impl FromInner<File> for TcpListener {
244     fn from_inner(file: File) -> TcpListener {
245         TcpListener(file)
246     }
247 }
248
249 impl IntoInner<File> for TcpListener {
250     fn into_inner(self) -> File { self.0 }
251 }