diff options
| author | Anthony Grondin <[email protected]> | 2024-10-31 22:19:49 -0400 |
|---|---|---|
| committer | Anthony Grondin <[email protected]> | 2024-10-31 22:25:48 -0400 |
| commit | acaba50704b27abe6160539e6e764f5df0d58120 (patch) | |
| tree | b5fd78c84d88d2260ab7b39fa934fde6ef9fe160 /embassy-net | |
| parent | 70214fc2c23aded1f5a1b8a62425ac3aa581102f (diff) | |
feat(embassy-net): Implement `wait_send_ready()` + `wait_recv_ready()` for Raw sockets.
Diffstat (limited to 'embassy-net')
| -rw-r--r-- | embassy-net/src/raw.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/embassy-net/src/raw.rs b/embassy-net/src/raw.rs index 1f725d00b..a88bcc458 100644 --- a/embassy-net/src/raw.rs +++ b/embassy-net/src/raw.rs | |||
| @@ -62,6 +62,14 @@ impl<'a> RawSocket<'a> { | |||
| 62 | }) | 62 | }) |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | /// Wait until the socket becomes readable. | ||
| 66 | /// | ||
| 67 | /// A socket is readable when a packet has been received, or when there are queued packets in | ||
| 68 | /// the buffer. | ||
| 69 | pub async fn wait_recv_ready(&self) { | ||
| 70 | poll_fn(move |cx| self.poll_recv_ready(cx)).await | ||
| 71 | } | ||
| 72 | |||
| 65 | /// Receive a datagram. | 73 | /// Receive a datagram. |
| 66 | /// | 74 | /// |
| 67 | /// This method will wait until a datagram is received. | 75 | /// This method will wait until a datagram is received. |
| @@ -69,6 +77,24 @@ impl<'a> RawSocket<'a> { | |||
| 69 | poll_fn(move |cx| self.poll_recv(buf, cx)).await | 77 | poll_fn(move |cx| self.poll_recv(buf, cx)).await |
| 70 | } | 78 | } |
| 71 | 79 | ||
| 80 | /// Wait until a datagram can be read. | ||
| 81 | /// | ||
| 82 | /// When no datagram is readable, this method will return `Poll::Pending` and | ||
| 83 | /// register the current task to be notified when a datagram is received. | ||
| 84 | /// | ||
| 85 | /// When a datagram is received, this method will return `Poll::Ready`. | ||
| 86 | pub fn poll_recv_ready(&self, cx: &mut Context<'_>) -> Poll<()> { | ||
| 87 | self.with_mut(|s, _| { | ||
| 88 | if s.can_recv() { | ||
| 89 | Poll::Ready(()) | ||
| 90 | } else { | ||
| 91 | // socket buffer is empty wait until at least one byte has arrived | ||
| 92 | s.register_recv_waker(cx.waker()); | ||
| 93 | Poll::Pending | ||
| 94 | } | ||
| 95 | }) | ||
| 96 | } | ||
| 97 | |||
| 72 | /// Receive a datagram. | 98 | /// Receive a datagram. |
| 73 | /// | 99 | /// |
| 74 | /// When no datagram is available, this method will return `Poll::Pending` and | 100 | /// When no datagram is available, this method will return `Poll::Pending` and |
| @@ -85,6 +111,33 @@ impl<'a> RawSocket<'a> { | |||
| 85 | }) | 111 | }) |
| 86 | } | 112 | } |
| 87 | 113 | ||
| 114 | /// Wait until the socket becomes writable. | ||
| 115 | /// | ||
| 116 | /// A socket becomes writable when there is space in the buffer, from initial memory or after | ||
| 117 | /// dispatching datagrams on a full buffer. | ||
| 118 | pub async fn wait_send_ready(&self) { | ||
| 119 | poll_fn(move |cx| self.poll_send_ready(cx)).await | ||
| 120 | } | ||
| 121 | |||
| 122 | /// Wait until a datagram can be sent. | ||
| 123 | /// | ||
| 124 | /// When no datagram can be sent (i.e. the buffer is full), this method will return | ||
| 125 | /// `Poll::Pending` and register the current task to be notified when | ||
| 126 | /// space is freed in the buffer after a datagram has been dispatched. | ||
| 127 | /// | ||
| 128 | /// When a datagram can be sent, this method will return `Poll::Ready`. | ||
| 129 | pub fn poll_send_ready(&self, cx: &mut Context<'_>) -> Poll<()> { | ||
| 130 | self.with_mut(|s, _| { | ||
| 131 | if s.can_send() { | ||
| 132 | Poll::Ready(()) | ||
| 133 | } else { | ||
| 134 | // socket buffer is full wait until a datagram has been dispatched | ||
| 135 | s.register_send_waker(cx.waker()); | ||
| 136 | Poll::Pending | ||
| 137 | } | ||
| 138 | }) | ||
| 139 | } | ||
| 140 | |||
| 88 | /// Send a datagram. | 141 | /// Send a datagram. |
| 89 | /// | 142 | /// |
| 90 | /// This method will wait until the datagram has been sent.` | 143 | /// This method will wait until the datagram has been sent.` |
