diff options
Diffstat (limited to 'embassy-net/src/udp.rs')
| -rw-r--r-- | embassy-net/src/udp.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs index ac650364e..76602edc2 100644 --- a/embassy-net/src/udp.rs +++ b/embassy-net/src/udp.rs | |||
| @@ -103,6 +103,32 @@ impl<'a> UdpSocket<'a> { | |||
| 103 | }) | 103 | }) |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | /// Wait until the socket becomes readable. | ||
| 107 | /// | ||
| 108 | /// A socket is readable when a packet has been received, or when there are queued packets in | ||
| 109 | /// the buffer. | ||
| 110 | pub async fn wait_recv_ready(&self) { | ||
| 111 | poll_fn(move |cx| self.poll_recv_ready(cx)).await | ||
| 112 | } | ||
| 113 | |||
| 114 | /// Wait until a datagram can be read. | ||
| 115 | /// | ||
| 116 | /// When no datagram is readable, this method will return `Poll::Pending` and | ||
| 117 | /// register the current task to be notified when a datagram is received. | ||
| 118 | /// | ||
| 119 | /// When a datagram is received, this method will return `Poll::Ready`. | ||
| 120 | pub fn poll_recv_ready(&self, cx: &mut Context<'_>) -> Poll<()> { | ||
| 121 | self.with_mut(|s, _| { | ||
| 122 | if s.can_recv() { | ||
| 123 | Poll::Ready(()) | ||
| 124 | } else { | ||
| 125 | // socket buffer is empty wait until at least one byte has arrived | ||
| 126 | s.register_recv_waker(cx.waker()); | ||
| 127 | Poll::Pending | ||
| 128 | } | ||
| 129 | }) | ||
| 130 | } | ||
| 131 | |||
| 106 | /// Receive a datagram. | 132 | /// Receive a datagram. |
| 107 | /// | 133 | /// |
| 108 | /// This method will wait until a datagram is received. | 134 | /// This method will wait until a datagram is received. |
| @@ -164,6 +190,33 @@ impl<'a> UdpSocket<'a> { | |||
| 164 | .await | 190 | .await |
| 165 | } | 191 | } |
| 166 | 192 | ||
| 193 | /// Wait until the socket becomes writable. | ||
| 194 | /// | ||
| 195 | /// A socket becomes writable when there is space in the buffer, from initial memory or after | ||
| 196 | /// dispatching datagrams on a full buffer. | ||
| 197 | pub async fn wait_send_ready(&self) { | ||
| 198 | poll_fn(move |cx| self.poll_send_ready(cx)).await | ||
| 199 | } | ||
| 200 | |||
| 201 | /// Wait until a datagram can be sent. | ||
| 202 | /// | ||
| 203 | /// When no datagram can be sent (i.e. the buffer is full), this method will return | ||
| 204 | /// `Poll::Pending` and register the current task to be notified when | ||
| 205 | /// space is freed in the buffer after a datagram has been dispatched. | ||
| 206 | /// | ||
| 207 | /// When a datagram can be sent, this method will return `Poll::Ready`. | ||
| 208 | pub fn poll_send_ready(&self, cx: &mut Context<'_>) -> Poll<()> { | ||
| 209 | self.with_mut(|s, _| { | ||
| 210 | if s.can_send() { | ||
| 211 | Poll::Ready(()) | ||
| 212 | } else { | ||
| 213 | // socket buffer is full wait until a datagram has been dispatched | ||
| 214 | s.register_send_waker(cx.waker()); | ||
| 215 | Poll::Pending | ||
| 216 | } | ||
| 217 | }) | ||
| 218 | } | ||
| 219 | |||
| 167 | /// Send a datagram to the specified remote endpoint. | 220 | /// Send a datagram to the specified remote endpoint. |
| 168 | /// | 221 | /// |
| 169 | /// This method will wait until the datagram has been sent. | 222 | /// This method will wait until the datagram has been sent. |
