Add poll functions on UdpSocket.

This commit is contained in:
Roy Buitenhuis 2023-07-12 11:32:02 +02:00
parent c6e2f4a90b
commit f54e1cea90

View File

@ -102,7 +102,10 @@ impl<'a> UdpSocket<'a> {
/// ///
/// Returns the number of bytes received and the remote endpoint. /// Returns the number of bytes received and the remote endpoint.
pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> {
poll_fn(move |cx| { poll_fn(move |cx| self.poll_recv_from(buf, cx)).await
}
pub fn poll_recv_from(&self, buf: &mut[u8], cx: Context) -> Poll<Result<(usize, IpEndpoint), Error>> {
self.with_mut(|s, _| match s.recv_slice(buf) { self.with_mut(|s, _| match s.recv_slice(buf) {
Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))),
// No data ready // No data ready
@ -111,17 +114,21 @@ impl<'a> UdpSocket<'a> {
Poll::Pending Poll::Pending
} }
}) })
})
.await
} }
/// Send a datagram to the specified remote endpoint. /// Send a datagram to the specified remote endpoint.
pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error>
where
T: Into<IpEndpoint>,
{
poll_fn(move |cx| self.poll_send_to(buf, remote_endpoint, cx)).await
}
pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: Context) -> Poll<Result<(), Error>>
where where
T: Into<IpEndpoint>, T: Into<IpEndpoint>,
{ {
let remote_endpoint = remote_endpoint.into(); let remote_endpoint = remote_endpoint.into();
poll_fn(move |cx| {
self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) { self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) {
// Entire datagram has been sent // Entire datagram has been sent
Ok(()) => Poll::Ready(Ok(())), Ok(()) => Poll::Ready(Ok(())),
@ -131,8 +138,6 @@ impl<'a> UdpSocket<'a> {
} }
Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)), Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)),
}) })
})
.await
} }
/// Returns the local endpoint of the socket. /// Returns the local endpoint of the socket.