diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-11-01 12:21:06 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-11-01 12:21:06 +0000 |
| commit | 10a9766046f7f8a2cf1c7b91209432ee15ee22a9 (patch) | |
| tree | 17ea3814bd1dfd4de1b7400b0b156b8d5fd8cd88 | |
| parent | 763de8a37e4ca4e92ca6ea904c72b45fe6eb84eb (diff) | |
| parent | acaba50704b27abe6160539e6e764f5df0d58120 (diff) | |
Merge pull request #3485 from AnthonyGrondin/main
Implement readable / writable types for TCP socket splits and raw sockets
| -rw-r--r-- | embassy-net/src/raw.rs | 53 | ||||
| -rw-r--r-- | embassy-net/src/tcp.rs | 20 |
2 files changed, 73 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.` |
diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index a00ced8f4..150b4b36b 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs | |||
| @@ -73,6 +73,16 @@ pub struct TcpWriter<'a> { | |||
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | impl<'a> TcpReader<'a> { | 75 | impl<'a> TcpReader<'a> { |
| 76 | /// Wait until the socket becomes readable. | ||
| 77 | /// | ||
| 78 | /// A socket becomes readable when the receive half of the full-duplex connection is open | ||
| 79 | /// (see [`may_recv()`](TcpSocket::may_recv)), and there is some pending data in the receive buffer. | ||
| 80 | /// | ||
| 81 | /// This is the equivalent of [read](#method.read), without buffering any data. | ||
| 82 | pub async fn wait_read_ready(&self) { | ||
| 83 | poll_fn(move |cx| self.io.poll_read_ready(cx)).await | ||
| 84 | } | ||
| 85 | |||
| 76 | /// Read data from the socket. | 86 | /// Read data from the socket. |
| 77 | /// | 87 | /// |
| 78 | /// Returns how many bytes were read, or an error. If no data is available, it waits | 88 | /// Returns how many bytes were read, or an error. If no data is available, it waits |
| @@ -115,6 +125,16 @@ impl<'a> TcpReader<'a> { | |||
| 115 | } | 125 | } |
| 116 | 126 | ||
| 117 | impl<'a> TcpWriter<'a> { | 127 | impl<'a> TcpWriter<'a> { |
| 128 | /// Wait until the socket becomes writable. | ||
| 129 | /// | ||
| 130 | /// A socket becomes writable when the transmit half of the full-duplex connection is open | ||
| 131 | /// (see [`may_send()`](TcpSocket::may_send)), and the transmit buffer is not full. | ||
| 132 | /// | ||
| 133 | /// This is the equivalent of [write](#method.write), without sending any data. | ||
| 134 | pub async fn wait_write_ready(&self) { | ||
| 135 | poll_fn(move |cx| self.io.poll_write_ready(cx)).await | ||
| 136 | } | ||
| 137 | |||
| 118 | /// Write data to the socket. | 138 | /// Write data to the socket. |
| 119 | /// | 139 | /// |
| 120 | /// Returns how many bytes were written, or an error. If the socket is not ready to | 140 | /// Returns how many bytes were written, or an error. If the socket is not ready to |
