broker_tokio/net/unix/
datagram.rs

1use crate::future::poll_fn;
2use crate::io::PollEvented;
3
4use std::convert::TryFrom;
5use std::fmt;
6use std::io;
7use std::net::Shutdown;
8use std::os::unix::io::{AsRawFd, RawFd};
9use std::os::unix::net::{self, SocketAddr};
10use std::path::Path;
11use std::task::{Context, Poll};
12
13cfg_uds! {
14    /// An I/O object representing a Unix datagram socket.
15    pub struct UnixDatagram {
16        io: PollEvented<mio_uds::UnixDatagram>,
17    }
18}
19
20impl UnixDatagram {
21    /// Creates a new `UnixDatagram` bound to the specified path.
22    pub fn bind<P>(path: P) -> io::Result<UnixDatagram>
23    where
24        P: AsRef<Path>,
25    {
26        let socket = mio_uds::UnixDatagram::bind(path)?;
27        UnixDatagram::new(socket)
28    }
29
30    /// Creates an unnamed pair of connected sockets.
31    ///
32    /// This function will create a pair of interconnected Unix sockets for
33    /// communicating back and forth between one another. Each socket will
34    /// be associated with the default event loop's handle.
35    pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
36        let (a, b) = mio_uds::UnixDatagram::pair()?;
37        let a = UnixDatagram::new(a)?;
38        let b = UnixDatagram::new(b)?;
39
40        Ok((a, b))
41    }
42
43    /// Consumes a `UnixDatagram` in the standard library and returns a
44    /// nonblocking `UnixDatagram` from this crate.
45    ///
46    /// The returned datagram will be associated with the given event loop
47    /// specified by `handle` and is ready to perform I/O.
48    ///
49    /// # Panics
50    ///
51    /// This function panics if thread-local runtime is not set.
52    ///
53    /// The runtime is usually set implicitly when this function is called
54    /// from a future driven by a tokio runtime, otherwise runtime can be set
55    /// explicitly with [`Handle::enter`](crate::runtime::Handle::enter) function.
56    pub fn from_std(datagram: net::UnixDatagram) -> io::Result<UnixDatagram> {
57        let socket = mio_uds::UnixDatagram::from_datagram(datagram)?;
58        let io = PollEvented::new(socket)?;
59        Ok(UnixDatagram { io })
60    }
61
62    fn new(socket: mio_uds::UnixDatagram) -> io::Result<UnixDatagram> {
63        let io = PollEvented::new(socket)?;
64        Ok(UnixDatagram { io })
65    }
66
67    /// Creates a new `UnixDatagram` which is not bound to any address.
68    pub fn unbound() -> io::Result<UnixDatagram> {
69        let socket = mio_uds::UnixDatagram::unbound()?;
70        UnixDatagram::new(socket)
71    }
72
73    /// Connects the socket to the specified address.
74    ///
75    /// The `send` method may be used to send data to the specified address.
76    /// `recv` and `recv_from` will only receive data from that address.
77    pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
78        self.io.get_ref().connect(path)
79    }
80
81    /// Sends data on the socket to the socket's peer.
82    pub async fn send(&mut self, buf: &[u8]) -> io::Result<usize> {
83        poll_fn(|cx| self.poll_send_priv(cx, buf)).await
84    }
85
86    // Poll IO functions that takes `&self` are provided for the split API.
87    //
88    // They are not public because (taken from the doc of `PollEvented`):
89    //
90    // While `PollEvented` is `Sync` (if the underlying I/O type is `Sync`), the
91    // caller must ensure that there are at most two tasks that use a
92    // `PollEvented` instance concurrently. One for reading and one for writing.
93    // While violating this requirement is "safe" from a Rust memory model point
94    // of view, it will result in unexpected behavior in the form of lost
95    // notifications and tasks hanging.
96    pub(crate) fn poll_send_priv(
97        &self,
98        cx: &mut Context<'_>,
99        buf: &[u8],
100    ) -> Poll<io::Result<usize>> {
101        ready!(self.io.poll_write_ready(cx))?;
102
103        match self.io.get_ref().send(buf) {
104            Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
105                self.io.clear_write_ready(cx)?;
106                Poll::Pending
107            }
108            x => Poll::Ready(x),
109        }
110    }
111
112    /// Receives data from the socket.
113    pub async fn recv(&mut self, buf: &mut [u8]) -> io::Result<usize> {
114        poll_fn(|cx| self.poll_recv_priv(cx, buf)).await
115    }
116
117    pub(crate) fn poll_recv_priv(
118        &self,
119        cx: &mut Context<'_>,
120        buf: &mut [u8],
121    ) -> Poll<io::Result<usize>> {
122        ready!(self.io.poll_read_ready(cx, mio::Ready::readable()))?;
123
124        match self.io.get_ref().recv(buf) {
125            Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
126                self.io.clear_read_ready(cx, mio::Ready::readable())?;
127                Poll::Pending
128            }
129            x => Poll::Ready(x),
130        }
131    }
132
133    /// Sends data on the socket to the specified address.
134    pub async fn send_to<P>(&mut self, buf: &[u8], target: P) -> io::Result<usize>
135    where
136        P: AsRef<Path> + Unpin,
137    {
138        poll_fn(|cx| self.poll_send_to_priv(cx, buf, target.as_ref())).await
139    }
140
141    pub(crate) fn poll_send_to_priv(
142        &self,
143        cx: &mut Context<'_>,
144        buf: &[u8],
145        target: &Path,
146    ) -> Poll<io::Result<usize>> {
147        ready!(self.io.poll_write_ready(cx))?;
148
149        match self.io.get_ref().send_to(buf, target) {
150            Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
151                self.io.clear_write_ready(cx)?;
152                Poll::Pending
153            }
154            x => Poll::Ready(x),
155        }
156    }
157
158    /// Receives data from the socket.
159    pub async fn recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
160        poll_fn(|cx| self.poll_recv_from_priv(cx, buf)).await
161    }
162
163    pub(crate) fn poll_recv_from_priv(
164        &self,
165        cx: &mut Context<'_>,
166        buf: &mut [u8],
167    ) -> Poll<Result<(usize, SocketAddr), io::Error>> {
168        ready!(self.io.poll_read_ready(cx, mio::Ready::readable()))?;
169
170        match self.io.get_ref().recv_from(buf) {
171            Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
172                self.io.clear_read_ready(cx, mio::Ready::readable())?;
173                Poll::Pending
174            }
175            x => Poll::Ready(x),
176        }
177    }
178
179    /// Returns the local address that this socket is bound to.
180    pub fn local_addr(&self) -> io::Result<SocketAddr> {
181        self.io.get_ref().local_addr()
182    }
183
184    /// Returns the address of this socket's peer.
185    ///
186    /// The `connect` method will connect the socket to a peer.
187    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
188        self.io.get_ref().peer_addr()
189    }
190
191    /// Returns the value of the `SO_ERROR` option.
192    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
193        self.io.get_ref().take_error()
194    }
195
196    /// Shut down the read, write, or both halves of this connection.
197    ///
198    /// This function will cause all pending and future I/O calls on the
199    /// specified portions to immediately return with an appropriate value
200    /// (see the documentation of `Shutdown`).
201    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
202        self.io.get_ref().shutdown(how)
203    }
204}
205
206impl TryFrom<UnixDatagram> for mio_uds::UnixDatagram {
207    type Error = io::Error;
208
209    /// Consumes value, returning the mio I/O object.
210    ///
211    /// See [`PollEvented::into_inner`] for more details about
212    /// resource deregistration that happens during the call.
213    ///
214    /// [`PollEvented::into_inner`]: crate::io::PollEvented::into_inner
215    fn try_from(value: UnixDatagram) -> Result<Self, Self::Error> {
216        value.io.into_inner()
217    }
218}
219
220impl TryFrom<net::UnixDatagram> for UnixDatagram {
221    type Error = io::Error;
222
223    /// Consumes stream, returning the tokio I/O object.
224    ///
225    /// This is equivalent to
226    /// [`UnixDatagram::from_std(stream)`](UnixDatagram::from_std).
227    fn try_from(stream: net::UnixDatagram) -> Result<Self, Self::Error> {
228        Self::from_std(stream)
229    }
230}
231
232impl fmt::Debug for UnixDatagram {
233    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234        self.io.get_ref().fmt(f)
235    }
236}
237
238impl AsRawFd for UnixDatagram {
239    fn as_raw_fd(&self) -> RawFd {
240        self.io.get_ref().as_raw_fd()
241    }
242}