broker_tokio/net/unix/
datagram.rs1use 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 pub struct UnixDatagram {
16 io: PollEvented<mio_uds::UnixDatagram>,
17 }
18}
19
20impl UnixDatagram {
21 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 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 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 pub fn unbound() -> io::Result<UnixDatagram> {
69 let socket = mio_uds::UnixDatagram::unbound()?;
70 UnixDatagram::new(socket)
71 }
72
73 pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
78 self.io.get_ref().connect(path)
79 }
80
81 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 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 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 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 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 pub fn local_addr(&self) -> io::Result<SocketAddr> {
181 self.io.get_ref().local_addr()
182 }
183
184 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
188 self.io.get_ref().peer_addr()
189 }
190
191 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
193 self.io.get_ref().take_error()
194 }
195
196 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 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 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}