interprocess_docfix/local_socket/
stream.rs

1use {
2    super::ToLocalSocketName,
3    std::{
4        fmt::{self, Debug, Formatter},
5        io::{self, prelude::*, IoSlice, IoSliceMut},
6    },
7};
8
9impmod! {local_socket,
10    LocalSocketStream as LocalSocketStreamImpl
11}
12
13/// A local socket byte stream, obtained eiter from [`LocalSocketListener`] or by connecting to an existing local socket.
14///
15/// # Examples
16/// - [Basic client](https://github.com/kotauskas/interprocess/blob/main/examples/local_socket/client.rs)
17///
18/// [`LocalSocketListener`]: struct.LocalSocketListener.html " "
19pub struct LocalSocketStream {
20    pub(super) inner: LocalSocketStreamImpl,
21}
22impl LocalSocketStream {
23    /// Connects to a remote local socket server.
24    pub fn connect<'a>(name: impl ToLocalSocketName<'a>) -> io::Result<Self> {
25        Ok(Self {
26            inner: LocalSocketStreamImpl::connect(name)?,
27        })
28    }
29    /// Retrieves the identifier of the process on the opposite end of the local socket connection.
30    ///
31    /// # Platform-specific behavior
32    /// ## macOS and iOS
33    /// Not supported by the OS, will always generate an error at runtime.
34    pub fn peer_pid(&self) -> io::Result<u32> {
35        self.inner.peer_pid()
36    }
37    /// Enables or disables the nonblocking mode for the stream. By default, it is disabled.
38    ///
39    /// In nonblocking mode, reading and writing will immediately return with the [`WouldBlock`] error in situations when they would normally block for an uncontrolled amount of time. The specific situations are:
40    /// - When reading is attempted and there is no new data available;
41    /// - When writing is attempted and the buffer is full due to the other side not yet having read previously sent data.
42    ///
43    /// [`WouldBlock`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.WouldBlock " "
44    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
45        self.inner.set_nonblocking(nonblocking)
46    }
47}
48// TODO panic on read-to-end and read-to-string
49// TODO vectored I/O on Unix
50impl Read for LocalSocketStream {
51    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
52        self.inner.read(buf)
53    }
54    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
55        self.inner.read_vectored(bufs)
56    }
57}
58impl Write for LocalSocketStream {
59    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
60        self.inner.write(buf)
61    }
62    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
63        self.inner.write_vectored(bufs)
64    }
65    fn flush(&mut self) -> io::Result<()> {
66        self.inner.flush()
67    }
68}
69impl Debug for LocalSocketStream {
70    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
71        Debug::fmt(&self.inner, f)
72    }
73}
74impl_handle_manip!(LocalSocketStream);