aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src/tcp.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-net/src/tcp.rs')
-rw-r--r--embassy-net/src/tcp.rs120
1 files changed, 41 insertions, 79 deletions
diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs
index f3bd2361c..85d9e5ee1 100644
--- a/embassy-net/src/tcp.rs
+++ b/embassy-net/src/tcp.rs
@@ -271,8 +271,6 @@ impl<'d> TcpIo<'d> {
271 271
272#[cfg(feature = "nightly")] 272#[cfg(feature = "nightly")]
273mod embedded_io_impls { 273mod embedded_io_impls {
274 use core::future::Future;
275
276 use super::*; 274 use super::*;
277 275
278 impl embedded_io::Error for ConnectError { 276 impl embedded_io::Error for ConnectError {
@@ -292,30 +290,18 @@ mod embedded_io_impls {
292 } 290 }
293 291
294 impl<'d> embedded_io::asynch::Read for TcpSocket<'d> { 292 impl<'d> embedded_io::asynch::Read for TcpSocket<'d> {
295 type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 293 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
296 where 294 self.io.read(buf).await
297 Self: 'a;
298
299 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
300 self.io.read(buf)
301 } 295 }
302 } 296 }
303 297
304 impl<'d> embedded_io::asynch::Write for TcpSocket<'d> { 298 impl<'d> embedded_io::asynch::Write for TcpSocket<'d> {
305 type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 299 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
306 where 300 self.io.write(buf).await
307 Self: 'a;
308
309 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
310 self.io.write(buf)
311 } 301 }
312 302
313 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a 303 async fn flush(&mut self) -> Result<(), Self::Error> {
314 where 304 self.io.flush().await
315 Self: 'a;
316
317 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
318 self.io.flush()
319 } 305 }
320 } 306 }
321 307
@@ -324,12 +310,8 @@ mod embedded_io_impls {
324 } 310 }
325 311
326 impl<'d> embedded_io::asynch::Read for TcpReader<'d> { 312 impl<'d> embedded_io::asynch::Read for TcpReader<'d> {
327 type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 313 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
328 where 314 self.io.read(buf).await
329 Self: 'a;
330
331 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
332 self.io.read(buf)
333 } 315 }
334 } 316 }
335 317
@@ -338,27 +320,18 @@ mod embedded_io_impls {
338 } 320 }
339 321
340 impl<'d> embedded_io::asynch::Write for TcpWriter<'d> { 322 impl<'d> embedded_io::asynch::Write for TcpWriter<'d> {
341 type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 323 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
342 where 324 self.io.write(buf).await
343 Self: 'a;
344
345 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
346 self.io.write(buf)
347 } 325 }
348 326
349 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a 327 async fn flush(&mut self) -> Result<(), Self::Error> {
350 where 328 self.io.flush().await
351 Self: 'a;
352
353 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
354 self.io.flush()
355 } 329 }
356 } 330 }
357} 331}
358 332
359#[cfg(all(feature = "unstable-traits", feature = "nightly"))] 333#[cfg(all(feature = "unstable-traits", feature = "nightly"))]
360pub mod client { 334pub mod client {
361 use core::future::Future;
362 use core::mem::MaybeUninit; 335 use core::mem::MaybeUninit;
363 use core::ptr::NonNull; 336 use core::ptr::NonNull;
364 337
@@ -385,28 +358,29 @@ pub mod client {
385 { 358 {
386 type Error = Error; 359 type Error = Error;
387 type Connection<'m> = TcpConnection<'m, N, TX_SZ, RX_SZ> where Self: 'm; 360 type Connection<'m> = TcpConnection<'m, N, TX_SZ, RX_SZ> where Self: 'm;
388 type ConnectFuture<'m> = impl Future<Output = Result<Self::Connection<'m>, Self::Error>> + 'm 361
389 where 362 async fn connect<'a>(
390 Self: 'm; 363 &'a self,
391 364 remote: embedded_nal_async::SocketAddr,
392 fn connect<'m>(&'m self, remote: embedded_nal_async::SocketAddr) -> Self::ConnectFuture<'m> { 365 ) -> Result<Self::Connection<'a>, Self::Error>
393 async move { 366 where
394 let addr: crate::IpAddress = match remote.ip() { 367 Self: 'a,
395 IpAddr::V4(addr) => crate::IpAddress::Ipv4(crate::Ipv4Address::from_bytes(&addr.octets())), 368 {
396 #[cfg(feature = "proto-ipv6")] 369 let addr: crate::IpAddress = match remote.ip() {
397 IpAddr::V6(addr) => crate::IpAddress::Ipv6(crate::Ipv6Address::from_bytes(&addr.octets())), 370 IpAddr::V4(addr) => crate::IpAddress::Ipv4(crate::Ipv4Address::from_bytes(&addr.octets())),
398 #[cfg(not(feature = "proto-ipv6"))] 371 #[cfg(feature = "proto-ipv6")]
399 IpAddr::V6(_) => panic!("ipv6 support not enabled"), 372 IpAddr::V6(addr) => crate::IpAddress::Ipv6(crate::Ipv6Address::from_bytes(&addr.octets())),
400 }; 373 #[cfg(not(feature = "proto-ipv6"))]
401 let remote_endpoint = (addr, remote.port()); 374 IpAddr::V6(_) => panic!("ipv6 support not enabled"),
402 let mut socket = TcpConnection::new(&self.stack, self.state)?; 375 };
403 socket 376 let remote_endpoint = (addr, remote.port());
404 .socket 377 let mut socket = TcpConnection::new(&self.stack, self.state)?;
405 .connect(remote_endpoint) 378 socket
406 .await 379 .socket
407 .map_err(|_| Error::ConnectionReset)?; 380 .connect(remote_endpoint)
408 Ok(socket) 381 .await
409 } 382 .map_err(|_| Error::ConnectionReset)?;
383 Ok(socket)
410 } 384 }
411 } 385 }
412 386
@@ -445,32 +419,20 @@ pub mod client {
445 impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> embedded_io::asynch::Read 419 impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> embedded_io::asynch::Read
446 for TcpConnection<'d, N, TX_SZ, RX_SZ> 420 for TcpConnection<'d, N, TX_SZ, RX_SZ>
447 { 421 {
448 type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 422 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
449 where 423 self.socket.read(buf).await
450 Self: 'a;
451
452 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
453 self.socket.read(buf)
454 } 424 }
455 } 425 }
456 426
457 impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> embedded_io::asynch::Write 427 impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> embedded_io::asynch::Write
458 for TcpConnection<'d, N, TX_SZ, RX_SZ> 428 for TcpConnection<'d, N, TX_SZ, RX_SZ>
459 { 429 {
460 type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 430 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
461 where 431 self.socket.write(buf).await
462 Self: 'a;
463
464 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
465 self.socket.write(buf)
466 } 432 }
467 433
468 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a 434 async fn flush(&mut self) -> Result<(), Self::Error> {
469 where 435 self.socket.flush().await
470 Self: 'a;
471
472 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
473 self.socket.flush()
474 } 436 }
475 } 437 }
476 438