diff options
| author | Roy Buitenhuis <[email protected]> | 2023-07-12 11:32:02 +0200 |
|---|---|---|
| committer | Roy Buitenhuis <[email protected]> | 2023-07-12 11:32:02 +0200 |
| commit | f54e1cea90527871f65ea449e58dc57fcf7cdb37 (patch) | |
| tree | dbb71ce755b68ad324c5704fb2bba985fb2b0633 /embassy-net | |
| parent | c6e2f4a90b8bc1caf2b62e355af6285347e99c21 (diff) | |
Add poll functions on UdpSocket.
Diffstat (limited to 'embassy-net')
| -rw-r--r-- | embassy-net/src/udp.rs | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs index 36f8d06f2..bd7a32f7c 100644 --- a/embassy-net/src/udp.rs +++ b/embassy-net/src/udp.rs | |||
| @@ -102,17 +102,18 @@ impl<'a> UdpSocket<'a> { | |||
| 102 | /// | 102 | /// |
| 103 | /// Returns the number of bytes received and the remote endpoint. | 103 | /// Returns the number of bytes received and the remote endpoint. |
| 104 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { | 104 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { |
| 105 | poll_fn(move |cx| { | 105 | poll_fn(move |cx| self.poll_recv_from(buf, cx)).await |
| 106 | self.with_mut(|s, _| match s.recv_slice(buf) { | 106 | } |
| 107 | Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), | 107 | |
| 108 | // No data ready | 108 | pub fn poll_recv_from(&self, buf: &mut[u8], cx: Context) -> Poll<Result<(usize, IpEndpoint), Error>> { |
| 109 | Err(udp::RecvError::Exhausted) => { | 109 | self.with_mut(|s, _| match s.recv_slice(buf) { |
| 110 | s.register_recv_waker(cx.waker()); | 110 | Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), |
| 111 | Poll::Pending | 111 | // No data ready |
| 112 | } | 112 | Err(udp::RecvError::Exhausted) => { |
| 113 | }) | 113 | s.register_recv_waker(cx.waker()); |
| 114 | Poll::Pending | ||
| 115 | } | ||
| 114 | }) | 116 | }) |
| 115 | .await | ||
| 116 | } | 117 | } |
| 117 | 118 | ||
| 118 | /// Send a datagram to the specified remote endpoint. | 119 | /// Send a datagram to the specified remote endpoint. |
| @@ -120,19 +121,23 @@ impl<'a> UdpSocket<'a> { | |||
| 120 | where | 121 | where |
| 121 | T: Into<IpEndpoint>, | 122 | T: Into<IpEndpoint>, |
| 122 | { | 123 | { |
| 124 | poll_fn(move |cx| self.poll_send_to(buf, remote_endpoint, cx)).await | ||
| 125 | } | ||
| 126 | |||
| 127 | pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: Context) -> Poll<Result<(), Error>> | ||
| 128 | where | ||
| 129 | T: Into<IpEndpoint>, | ||
| 130 | { | ||
| 123 | let remote_endpoint = remote_endpoint.into(); | 131 | let remote_endpoint = remote_endpoint.into(); |
| 124 | poll_fn(move |cx| { | 132 | self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) { |
| 125 | self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) { | 133 | // Entire datagram has been sent |
| 126 | // Entire datagram has been sent | 134 | Ok(()) => Poll::Ready(Ok(())), |
| 127 | Ok(()) => Poll::Ready(Ok(())), | 135 | Err(udp::SendError::BufferFull) => { |
| 128 | Err(udp::SendError::BufferFull) => { | 136 | s.register_send_waker(cx.waker()); |
| 129 | s.register_send_waker(cx.waker()); | 137 | Poll::Pending |
| 130 | Poll::Pending | 138 | } |
| 131 | } | 139 | Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)), |
| 132 | Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)), | ||
| 133 | }) | ||
| 134 | }) | 140 | }) |
| 135 | .await | ||
| 136 | } | 141 | } |
| 137 | 142 | ||
| 138 | /// Returns the local endpoint of the socket. | 143 | /// Returns the local endpoint of the socket. |
