pub struct TcpReader<'w, W5500> { /* private fields */ }
Expand description
Streaming reader for a TCP socket buffer.
This implements the Read
and Seek
traits.
Created with Tcp::tcp_reader
.
Example
use w5500_hl::{
io::Read,
ll::{Registers, Sn, SocketInterrupt},
net::{Ipv4Addr, SocketAddrV4},
Tcp, TcpReader,
};
const MQTT_SOCKET: Sn = Sn::Sn0;
const MQTT_SOURCE_PORT: u16 = 33650;
const MQTT_SERVER: SocketAddrV4 = SocketAddrV4::new(Ipv4Addr::new(192, 168, 2, 10), 1883);
w5500.tcp_connect(MQTT_SOCKET, MQTT_SOURCE_PORT, &MQTT_SERVER)?;
// ... wait for a CON interrupt
const CONNECT: [u8; 14] = [
0x10, 0x0C, 0x00, 0x04, b'M', b'Q', b'T', b'T', 0x04, 0x02, 0x0E, 0x10, 0x00, 0x00,
];
let tx_bytes: u16 = w5500.tcp_write(MQTT_SOCKET, &CONNECT)?;
assert_eq!(usize::from(tx_bytes), CONNECT.len());
// ... wait for a RECV interrupt
let mut reader: TcpReader<_> = w5500.tcp_reader(MQTT_SOCKET)?;
let mut buf = [0; 2];
// read the first two bytes
reader.read_exact(&mut buf)?;
// ... do something with the data
// read another two bytes into the same buffer
reader.read_exact(&mut buf)?;
// ... do something with the data
// mark the data as read
reader.done()?;
Trait Implementations§
source§impl<'a, W5500: Registers> Read<<W5500 as Registers>::Error> for TcpReader<'a, W5500>
impl<'a, W5500: Registers> Read<<W5500 as Registers>::Error> for TcpReader<'a, W5500>
source§impl<'w, W5500> Seek for TcpReader<'w, W5500>
impl<'w, W5500> Seek for TcpReader<'w, W5500>
source§fn seek<E>(&mut self, pos: SeekFrom) -> Result<(), Error<E>>
fn seek<E>(&mut self, pos: SeekFrom) -> Result<(), Error<E>>
Seek to an offset, in bytes, within the socket buffer. Read more
source§fn stream_len(&self) -> u16
fn stream_len(&self) -> u16
Return the length of the stream, in bytes. Read more
source§fn stream_position(&self) -> u16
fn stream_position(&self) -> u16
Returns the current seek position from the start of the stream.
Auto Trait Implementations§
impl<'w, W5500> RefUnwindSafe for TcpReader<'w, W5500>where W5500: RefUnwindSafe,
impl<'w, W5500> Send for TcpReader<'w, W5500>where W5500: Send,
impl<'w, W5500> Sync for TcpReader<'w, W5500>where W5500: Sync,
impl<'w, W5500> Unpin for TcpReader<'w, W5500>
impl<'w, W5500> !UnwindSafe for TcpReader<'w, W5500>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more