diff options
| author | Artur Kowalski <[email protected]> | 2022-08-11 08:23:18 +0200 |
|---|---|---|
| committer | Artur Kowalski <[email protected]> | 2022-08-11 08:23:18 +0200 |
| commit | b97983242d16a321bab8c13f9df4c8af99d89a0f (patch) | |
| tree | 4c7c948969f6758cf2e1bf8adf33df5eaad9ffb1 | |
| parent | 865a91976c5b6b5c45b37e0286e7c328e8404dde (diff) | |
Simplify UDP code
Drop unneeded APIs: remove impls of embedded_io error traits, remove
flush() and split() methods.
| -rw-r--r-- | embassy-net/src/udp.rs | 64 |
1 files changed, 5 insertions, 59 deletions
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs index 6b15805c2..ee90c3010 100644 --- a/embassy-net/src/udp.rs +++ b/embassy-net/src/udp.rs | |||
| @@ -30,14 +30,6 @@ pub struct UdpSocket<'a> { | |||
| 30 | io: UdpIo<'a>, | 30 | io: UdpIo<'a>, |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | pub struct UdpReader<'a> { | ||
| 34 | io: UdpIo<'a>, | ||
| 35 | } | ||
| 36 | |||
| 37 | pub struct UdpWriter<'a> { | ||
| 38 | io: UdpIo<'a>, | ||
| 39 | } | ||
| 40 | |||
| 41 | impl<'a> UdpSocket<'a> { | 33 | impl<'a> UdpSocket<'a> { |
| 42 | pub fn new<D: Device>( | 34 | pub fn new<D: Device>( |
| 43 | stack: &'a Stack<D>, | 35 | stack: &'a Stack<D>, |
| @@ -66,10 +58,6 @@ impl<'a> UdpSocket<'a> { | |||
| 66 | } | 58 | } |
| 67 | } | 59 | } |
| 68 | 60 | ||
| 69 | pub fn split(&mut self) -> (UdpReader<'_>, UdpWriter<'_>) { | ||
| 70 | (UdpReader { io: self.io }, UdpWriter { io: self.io }) | ||
| 71 | } | ||
| 72 | |||
| 73 | pub fn bind<T>(&mut self, endpoint: T) -> Result<(), BindError> | 61 | pub fn bind<T>(&mut self, endpoint: T) -> Result<(), BindError> |
| 74 | where | 62 | where |
| 75 | T: Into<IpListenEndpoint>, | 63 | T: Into<IpListenEndpoint>, |
| @@ -90,21 +78,17 @@ impl<'a> UdpSocket<'a> { | |||
| 90 | } | 78 | } |
| 91 | } | 79 | } |
| 92 | 80 | ||
| 93 | pub async fn send_to<T>(&mut self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> | 81 | pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> |
| 94 | where | 82 | where |
| 95 | T: Into<IpEndpoint>, | 83 | T: Into<IpEndpoint>, |
| 96 | { | 84 | { |
| 97 | self.io.write(buf, remote_endpoint.into()).await | 85 | self.io.write(buf, remote_endpoint.into()).await |
| 98 | } | 86 | } |
| 99 | 87 | ||
| 100 | pub async fn recv_from(&mut self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { | 88 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { |
| 101 | self.io.read(buf).await | 89 | self.io.read(buf).await |
| 102 | } | 90 | } |
| 103 | 91 | ||
| 104 | pub async fn flush(&mut self) -> Result<(), Error> { | ||
| 105 | self.io.flush().await | ||
| 106 | } | ||
| 107 | |||
| 108 | pub fn endpoint(&self) -> IpListenEndpoint { | 92 | pub fn endpoint(&self) -> IpListenEndpoint { |
| 109 | unsafe { self.io.with(|s, _| s.endpoint()) } | 93 | unsafe { self.io.with(|s, _| s.endpoint()) } |
| 110 | } | 94 | } |
| @@ -149,7 +133,7 @@ impl UdpIo<'_> { | |||
| 149 | } | 133 | } |
| 150 | 134 | ||
| 151 | /// SAFETY: must not call reentrantly. | 135 | /// SAFETY: must not call reentrantly. |
| 152 | unsafe fn with_mut<R>(&mut self, f: impl FnOnce(&mut udp::Socket, &mut Interface) -> R) -> R { | 136 | unsafe fn with_mut<R>(&self, f: impl FnOnce(&mut udp::Socket, &mut Interface) -> R) -> R { |
| 153 | let s = &mut *self.stack.get(); | 137 | let s = &mut *self.stack.get(); |
| 154 | let socket = s.sockets.get_mut::<udp::Socket>(self.handle); | 138 | let socket = s.sockets.get_mut::<udp::Socket>(self.handle); |
| 155 | let res = f(socket, &mut s.iface); | 139 | let res = f(socket, &mut s.iface); |
| @@ -157,7 +141,7 @@ impl UdpIo<'_> { | |||
| 157 | res | 141 | res |
| 158 | } | 142 | } |
| 159 | 143 | ||
| 160 | async fn read(&mut self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { | 144 | async fn read(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { |
| 161 | poll_fn(move |cx| unsafe { | 145 | poll_fn(move |cx| unsafe { |
| 162 | self.with_mut(|s, _| match s.recv_slice(buf) { | 146 | self.with_mut(|s, _| match s.recv_slice(buf) { |
| 163 | Ok(x) => Poll::Ready(Ok(x)), | 147 | Ok(x) => Poll::Ready(Ok(x)), |
| @@ -172,7 +156,7 @@ impl UdpIo<'_> { | |||
| 172 | .await | 156 | .await |
| 173 | } | 157 | } |
| 174 | 158 | ||
| 175 | async fn write(&mut self, buf: &[u8], ep: IpEndpoint) -> Result<(), Error> { | 159 | async fn write(&self, buf: &[u8], ep: IpEndpoint) -> Result<(), Error> { |
| 176 | poll_fn(move |cx| unsafe { | 160 | poll_fn(move |cx| unsafe { |
| 177 | self.with_mut(|s, _| match s.send_slice(buf, ep) { | 161 | self.with_mut(|s, _| match s.send_slice(buf, ep) { |
| 178 | // Entire datagram has been sent | 162 | // Entire datagram has been sent |
| @@ -186,42 +170,4 @@ impl UdpIo<'_> { | |||
| 186 | }) | 170 | }) |
| 187 | .await | 171 | .await |
| 188 | } | 172 | } |
| 189 | |||
| 190 | async fn flush(&mut self) -> Result<(), Error> { | ||
| 191 | poll_fn(move |_| { | ||
| 192 | Poll::Ready(Ok(())) // TODO: Is there a better implementation for this? | ||
| 193 | }) | ||
| 194 | .await | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | impl UdpReader<'_> { | ||
| 199 | pub async fn recv_from(&mut self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { | ||
| 200 | self.io.read(buf).await | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | impl UdpWriter<'_> { | ||
| 205 | pub async fn send_to<T>(&mut self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> | ||
| 206 | where | ||
| 207 | T: Into<IpEndpoint>, | ||
| 208 | { | ||
| 209 | self.io.write(buf, remote_endpoint.into()).await | ||
| 210 | } | ||
| 211 | |||
| 212 | pub async fn flush(&mut self) -> Result<(), Error> { | ||
| 213 | self.io.flush().await | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | impl embedded_io::Error for BindError { | ||
| 218 | fn kind(&self) -> embedded_io::ErrorKind { | ||
| 219 | embedded_io::ErrorKind::Other | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | impl embedded_io::Error for Error { | ||
| 224 | fn kind(&self) -> embedded_io::ErrorKind { | ||
| 225 | embedded_io::ErrorKind::Other | ||
| 226 | } | ||
| 227 | } | 173 | } |
