|
| 1 | +#![unstable(feature = "windows_unix_domain_sockets", issue = "56533")] |
| 2 | + |
| 3 | +use core::mem; |
| 4 | +use core::time::Duration; |
| 5 | + |
| 6 | +use crate::io::{self, IoSlice}; |
| 7 | +use crate::net::Shutdown; |
| 8 | +use crate::os::windows::io::{ |
| 9 | + AsRawSocket, AsSocket, BorrowedSocket, FromRawSocket, IntoRawSocket, RawSocket, |
| 10 | +}; |
| 11 | +use crate::os::windows::net::{SocketAddr, sockaddr_un}; |
| 12 | +use crate::path::Path; |
| 13 | +use crate::sys::c::{SO_RCVTIMEO, SO_SNDTIMEO, connect, getpeername, getsockname}; |
| 14 | +use crate::sys::cvt; |
| 15 | +use crate::sys::net::Socket; |
| 16 | + |
| 17 | +pub struct UnixStream(pub Socket); |
| 18 | +impl UnixStream { |
| 19 | + pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> { |
| 20 | + unsafe { |
| 21 | + let inner = Socket::new_unix()?; |
| 22 | + let (addr, len) = sockaddr_un(path.as_ref())?; |
| 23 | + cvt(connect(inner.as_raw() as _, &addr as *const _ as *const _, len))?; |
| 24 | + Ok(UnixStream(inner)) |
| 25 | + } |
| 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 | + pub fn peer_addr(&self) -> io::Result<SocketAddr> { |
| 31 | + SocketAddr::new(|addr, len| unsafe { getpeername(self.0.as_raw() as _, addr, len) }) |
| 32 | + } |
| 33 | + pub fn read_timeout(&self) -> io::Result<Option<Duration>> { |
| 34 | + self.0.timeout(SO_RCVTIMEO) |
| 35 | + } |
| 36 | + pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { |
| 37 | + self.0.set_nonblocking(nonblocking) |
| 38 | + } |
| 39 | + pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> { |
| 40 | + self.0.set_timeout(dur, SO_RCVTIMEO) |
| 41 | + } |
| 42 | + pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> { |
| 43 | + self.0.set_timeout(dur, SO_SNDTIMEO) |
| 44 | + } |
| 45 | + pub fn shutdown(&self, how: Shutdown) -> io::Result<()> { |
| 46 | + self.0.shutdown(how) |
| 47 | + } |
| 48 | + pub fn take_error(&self) -> io::Result<Option<io::Error>> { |
| 49 | + self.0.take_error() |
| 50 | + } |
| 51 | + pub fn try_clone(&self) -> io::Result<UnixStream> { |
| 52 | + self.0.duplicate().map(UnixStream) |
| 53 | + } |
| 54 | + pub fn write_timeout(&self) -> io::Result<Option<Duration>> { |
| 55 | + self.0.timeout(SO_SNDTIMEO) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl io::Read for UnixStream { |
| 60 | + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 61 | + io::Read::read(&mut &*self, buf) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<'a> io::Read for &'a UnixStream { |
| 66 | + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 67 | + self.0.read(buf) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl io::Write for UnixStream { |
| 72 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 73 | + io::Write::write(&mut &*self, buf) |
| 74 | + } |
| 75 | + |
| 76 | + fn flush(&mut self) -> io::Result<()> { |
| 77 | + io::Write::flush(&mut &*self) |
| 78 | + } |
| 79 | +} |
| 80 | +impl<'a> io::Write for &'a UnixStream { |
| 81 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 82 | + self.0.write_vectored(&[IoSlice::new(buf)]) |
| 83 | + } |
| 84 | + |
| 85 | + fn flush(&mut self) -> io::Result<()> { |
| 86 | + Ok(()) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl AsSocket for UnixStream { |
| 91 | + fn as_socket(&self) -> BorrowedSocket<'_> { |
| 92 | + self.0.as_socket() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl AsRawSocket for UnixStream { |
| 97 | + fn as_raw_socket(&self) -> RawSocket { |
| 98 | + self.0.as_raw_socket() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl FromRawSocket for UnixStream { |
| 103 | + unsafe fn from_raw_socket(sock: RawSocket) -> Self { |
| 104 | + unsafe { UnixStream(Socket::from_raw_socket(sock)) } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl IntoRawSocket for UnixStream { |
| 109 | + fn into_raw_socket(self) -> RawSocket { |
| 110 | + let ret = self.0.as_raw_socket(); |
| 111 | + mem::forget(self); |
| 112 | + ret |
| 113 | + } |
| 114 | +} |
0 commit comments