|
| 1 | +#![stable(feature = "rust1", since = "1.0.0")] |
| 2 | + |
| 3 | +use core::time::Duration; |
| 4 | + |
| 5 | +use crate::io; |
| 6 | +use crate::net::Shutdown; |
| 7 | +use crate::os::windows::net::{SocketAddr, sockaddr_un}; |
| 8 | +use crate::path::Path; |
| 9 | +use crate::sys::c::{SO_RCVTIMEO, SO_SNDTIMEO, connect, getpeername, getsockname}; |
| 10 | +use crate::sys::cvt; |
| 11 | +use crate::sys::net::Socket; |
| 12 | + |
| 13 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 14 | +pub struct UnixStream(pub Socket); |
| 15 | +impl UnixStream { |
| 16 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 17 | + pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> { |
| 18 | + unsafe { |
| 19 | + let inner = Socket::new_unix()?; |
| 20 | + let (addr, len) = sockaddr_un(path.as_ref())?; |
| 21 | + cvt(connect(inner.as_raw() as _, &addr as *const _ as *const _, len))?; |
| 22 | + Ok(UnixStream(inner)) |
| 23 | + } |
| 24 | + } |
| 25 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 26 | + |
| 27 | + pub fn local_addr(&self) -> io::Result<SocketAddr> { |
| 28 | + SocketAddr::new(|addr, len| unsafe { getsockname(self.0.as_raw() as _, addr, len) }) |
| 29 | + } |
| 30 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 31 | + pub fn pair() -> io::Result<(Self, Self)> { |
| 32 | + unimplemented!() |
| 33 | + } |
| 34 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 35 | + pub fn peer_addr(&self) -> io::Result<SocketAddr> { |
| 36 | + SocketAddr::new(|addr, len| unsafe { getpeername(self.0.as_raw() as _, addr, len) }) |
| 37 | + } |
| 38 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 39 | + pub fn read_timeout(&self) -> io::Result<Option<Duration>> { |
| 40 | + self.0.timeout(SO_RCVTIMEO) |
| 41 | + } |
| 42 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 43 | + pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { |
| 44 | + self.0.set_nonblocking(nonblocking) |
| 45 | + } |
| 46 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 47 | + pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> { |
| 48 | + self.0.set_timeout(dur, SO_RCVTIMEO) |
| 49 | + } |
| 50 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 51 | + pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> { |
| 52 | + self.0.set_timeout(dur, SO_SNDTIMEO) |
| 53 | + } |
| 54 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 55 | + pub fn shutdown(&self, how: Shutdown) -> io::Result<()> { |
| 56 | + self.0.shutdown(how) |
| 57 | + } |
| 58 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 59 | + pub fn take_error(&self) -> io::Result<Option<io::Error>> { |
| 60 | + self.0.take_error() |
| 61 | + } |
| 62 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 63 | + pub fn try_clone(&self) -> io::Result<UnixStream> { |
| 64 | + self.0.duplicate().map(UnixStream) |
| 65 | + } |
| 66 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 67 | + pub fn write_timeout(&self) -> io::Result<Option<Duration>> { |
| 68 | + self.0.timeout(SO_SNDTIMEO) |
| 69 | + } |
| 70 | +} |
0 commit comments