Skip to content

Commit a08814c

Browse files
committed
Add UdpSocket::{poll_recv_from, poll_send_to}
This is needed for QUIC implementations based on async-std. It could be done with `UdpSocket::{recv_from, send_to}`, but this requires boxing, reference counting, and `unsafe` code.
1 parent d2c25f4 commit a08814c

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

src/net/udp/mod.rs

+44-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io;
2-
use std::net::SocketAddr;
3-
use std::net::{Ipv4Addr, Ipv6Addr};
2+
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
3+
use std::task::{Context, Poll};
44

55
use crate::future;
66
use crate::net::driver::Watcher;
@@ -69,9 +69,7 @@ impl UdpSocket {
6969
/// ```
7070
pub async fn bind<A: ToSocketAddrs>(addrs: A) -> io::Result<UdpSocket> {
7171
let mut last_err = None;
72-
let addrs = addrs
73-
.to_socket_addrs()
74-
.await?;
72+
let addrs = addrs.to_socket_addrs().await?;
7573

7674
for addr in addrs {
7775
match mio::net::UdpSocket::bind(&addr) {
@@ -116,6 +114,19 @@ impl UdpSocket {
116114
.context(|| String::from("could not get local address"))
117115
}
118116

117+
/// Sends data on the socket to the given address.
118+
///
119+
/// If this function returns `Poll::Ready(Ok(_))`, returns the number of bytes written.
120+
pub fn poll_send_to(
121+
&self,
122+
cx: &mut Context<'_>,
123+
buf: &[u8],
124+
addr: &SocketAddr,
125+
) -> Poll<io::Result<usize>> {
126+
self.watcher
127+
.poll_write_with(cx, |inner| inner.send_to(buf, &addr))
128+
}
129+
119130
/// Sends data on the socket to the given address.
120131
///
121132
/// On success, returns the number of bytes written.
@@ -153,12 +164,21 @@ impl UdpSocket {
153164
}
154165
};
155166

156-
future::poll_fn(|cx| {
157-
self.watcher
158-
.poll_write_with(cx, |inner| inner.send_to(buf, &addr))
159-
})
160-
.await
161-
.context(|| format!("could not send packet to {}", addr))
167+
future::poll_fn(|cx| self.poll_send_to(cx, buf, &addr))
168+
.await
169+
.context(|| format!("could not send packet to {}", addr))
170+
}
171+
172+
/// Receives data from the socket.
173+
///
174+
/// On success, returns the number of bytes read and the origin.
175+
pub fn poll_recv_from(
176+
&self,
177+
cx: &mut Context<'_>,
178+
buf: &mut [u8],
179+
) -> Poll<io::Result<(usize, SocketAddr)>> {
180+
self.watcher
181+
.poll_read_with(cx, |inner| inner.recv_from(buf))
162182
}
163183

164184
/// Receives data from the socket.
@@ -181,22 +201,19 @@ impl UdpSocket {
181201
/// # Ok(()) }) }
182202
/// ```
183203
pub async fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
184-
future::poll_fn(|cx| {
185-
self.watcher
186-
.poll_read_with(cx, |inner| inner.recv_from(buf))
187-
})
188-
.await
189-
.context(|| {
190-
use std::fmt::Write;
191-
192-
let mut error = String::from("could not receive data on ");
193-
if let Ok(addr) = self.local_addr() {
194-
let _ = write!(&mut error, "{}", addr);
195-
} else {
196-
error.push_str("socket");
197-
}
198-
error
199-
})
204+
future::poll_fn(|cx| self.poll_recv_from(cx, buf))
205+
.await
206+
.context(|| {
207+
use std::fmt::Write;
208+
209+
let mut error = String::from("could not receive data on ");
210+
if let Ok(addr) = self.local_addr() {
211+
let _ = write!(&mut error, "{}", addr);
212+
} else {
213+
error.push_str("socket");
214+
}
215+
error
216+
})
200217
}
201218

202219
/// Connects the UDP socket to a remote address.

0 commit comments

Comments
 (0)