aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Grondin <[email protected]>2024-09-23 16:30:14 -0400
committerAnthony Grondin <[email protected]>2024-09-24 22:01:49 -0400
commitae5b78b27a9c644850c905c197e216a6cd2ab0b9 (patch)
treebcffa90d4041274e4288df28a45bb43b66cdebf7
parent0ede8479dc4c6a58cfab0a5d4df41c0592405971 (diff)
feat(embassy-net): Implement `wait_recv_ready()` + `wait_send_ready()` for UdpSocket
- Provides `pub async fn wait_recv_ready(&self) -> ()` and `pub fn poll_recv_ready(&self, cx: &mut Context<'_>) -> Poll<()>`. This allows polling / waiting on a socket until it can be read, without dequeuing any packets. - Provides `pub async fn wait_send_ready(&self) -> ()` and `pub fn poll_send_ready(&self, cx: &mut Context<'_> -> Poll<()>` This allows polling / waiting on a socket until it becomes writable.
-rw-r--r--embassy-net/src/udp.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs
index 3eb6e2f83..b71037522 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.