Skip to content

Commit

Permalink
net/udp: Relay full UdpMetadata instead of only remote endpoint in po…
Browse files Browse the repository at this point in the history
…ll_ functions

This is a breaking change for users of the poll_ functions. (Some might
not notice if they already pass in an IpEndpoint into poll_send_to, or
discard that item in poll_recv_from).

Cherry-picked-from: embassy-rs#2790
  • Loading branch information
chrysn committed Apr 8, 2024
1 parent 8350617 commit f333491
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions embassy-net/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::task::{Context, Poll};
use embassy_net_driver::Driver;
use smoltcp::iface::{Interface, SocketHandle};
use smoltcp::socket::udp;
pub use smoltcp::socket::udp::PacketMetadata;
pub use smoltcp::socket::udp::{PacketMetadata, UdpMetadata};
use smoltcp::wire::{IpEndpoint, IpListenEndpoint};

use crate::{SocketStack, Stack};
Expand Down Expand Up @@ -112,7 +112,9 @@ impl<'a> UdpSocket<'a> {
///
/// Returns the number of bytes received and the remote endpoint.
pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), RecvError> {
poll_fn(move |cx| self.poll_recv_from(buf, cx)).await
poll_fn(move |cx| self.poll_recv_from(buf, cx))
.await
.map(|(size, metadata)| (size, metadata.endpoint))
}

/// Receive a datagram.
Expand All @@ -122,9 +124,13 @@ impl<'a> UdpSocket<'a> {
///
/// When a datagram is received, this method will return `Poll::Ready` with the
/// number of bytes received and the remote endpoint.
pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<Result<(usize, IpEndpoint), RecvError>> {
pub fn poll_recv_from(
&self,
buf: &mut [u8],
cx: &mut Context<'_>,
) -> Poll<Result<(usize, UdpMetadata), RecvError>> {
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))),
// No data ready
Err(udp::RecvError::Truncated) => Poll::Ready(Err(RecvError::Truncated)),
Err(udp::RecvError::Exhausted) => {
Expand Down Expand Up @@ -157,7 +163,7 @@ impl<'a> UdpSocket<'a> {
/// When the remote endpoint is not reachable, this method will return `Poll::Ready(Err(Error::NoRoute))`.
pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll<Result<(), SendError>>
where
T: Into<IpEndpoint>,
T: Into<UdpMetadata>,
{
self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) {
// Entire datagram has been sent
Expand Down

0 comments on commit f333491

Please sign in to comment.