fn try_receive_packet(&mut self) -> anyhow::Result<Option<Packet>> {
        use std::io::Read as _;

        // All messages are length-prefixed by PROTOCOL_HEADER and u32 (LE).
        let mut header = [0_u8; 12];
        match self.tcp_stream.peek(&mut header) {
            Ok(12) => {}
            Ok(_) => {
                return Ok(None);
            }
            Err(err) => {
                if err.kind() == std::io::ErrorKind::WouldBlock {
                    return Ok(None);
                } else {
                    return Err(err.into());
                }
            }
        }

        let protocol = &header[..PROTOCOL_HEADER.len()];
        let length = &header[PROTOCOL_HEADER.len()..];
        let length = u32::from_le_bytes([length[0], length[1], length[2], length[3]]) as usize;

        if protocol[0..5] != PROTOCOL_HEADER[0..5] {
            anyhow::bail!("The other side is not eterm");
        }

        if protocol != PROTOCOL_HEADER {
            anyhow::bail!(
                "This side uses eterm {}.{}.{}, the other side is on {}.{}.{}",
                PROTOCOL_HEADER[5],
                PROTOCOL_HEADER[6],
                PROTOCOL_HEADER[7],
                protocol[5],
                protocol[6],
                protocol[7],
            );
        }

        if length > 32_000_000 {
            anyhow::bail!("Refusing packet of {:.1} MB", length as f32 * 1e-6);
        }

        // See if we have the whole packet yet:
        let mut length_and_packet = vec![0_u8; header.len() + length];
        match self.tcp_stream.peek(&mut length_and_packet) {
            Ok(bytes_read) => {
                if bytes_read != length_and_packet.len() {
                    return Ok(None); // not yet!
                }
            }
            Err(err) => {
                if err.kind() == std::io::ErrorKind::WouldBlock {
                    return Ok(None);
                } else {
                    return Err(err.into());
                }
            }
        }

        // consume the bytes:
        self.tcp_stream.read_exact(&mut length_and_packet)?;

        let packet = &length_and_packet[header.len()..];

        Ok(Some(packet.into()))
    }
/// returns immediately if there is nothing to read
returns immediately if there is nothing to read
returns immediately if there is nothing to read
fn try_receive_packet(&mut self) -> anyhow::Result<Option<Packet>> {
        use std::io::Read as _;
        // All messages are length-prefixed by PROTOCOL_HEADER and u32 (LE).
        let mut header = [0_u8; 12];
        match self.tcp_stream.peek(&mut header) {
            Ok(12) => {}
            Ok(_) => {
                return Ok(None);
            }
            Err(err) => {
                if err.kind() == std::io::ErrorKind::WouldBlock {
                    return Ok(None);
                } else {
                    return Err(err.into());
                }
            }
        }
        let protocol = &header[..PROTOCOL_HEADER.len()];
        let length = &header[PROTOCOL_HEADER.len()..];
        let length = u32::from_le_bytes([length[0], length[1], length[2], length[3]]) as usize;
        if protocol[0..5] != PROTOCOL_HEADER[0..5] {
            anyhow::bail!("The other side is not eterm");
        }
        if protocol != PROTOCOL_HEADER {
            anyhow::bail!(
                "This side uses eterm {}.{}.{}, the other side is on {}.{}.{}",
                PROTOCOL_HEADER[5],
                PROTOCOL_HEADER[6],
                PROTOCOL_HEADER[7],
                protocol[5],
                protocol[6],
                protocol[7],
            );
        }
        if length > 32_000_000 {
            anyhow::bail!("Refusing packet of {:.1} MB", length as f32 * 1e-6);
        }
        // See if we have the whole packet yet:
        let mut length_and_packet = vec![0_u8; header.len() + length];
        match self.tcp_stream.peek(&mut length_and_packet) {
            Ok(bytes_read) => {
                if bytes_read != length_and_packet.len() {
                    return Ok(None); // not yet!
                }
            }
            Err(err) => {
                if err.kind() == std::io::ErrorKind::WouldBlock {
                    return Ok(None);
                } else {
                    return Err(err.into());
                }
            }
        }
        // consume the bytes:
        self.tcp_stream.read_exact(&mut length_and_packet)?;
        let packet = &length_and_packet[header.len()..];
        Ok(Some(packet.into()))
    }
fn try_receive_packet ( & mut self ) -> anyhow :: Result < Option < Packet > > { use std :: io :: Read as _ ; // All messages are length-prefixed by PROTOCOL_HEADER and u32 (LE). let mut header = [ 0_u8 ; 12 ] ; match self . tcp_stream . peek ( & mut header ) { Ok ( 12 ) => { } Ok ( _ ) => { return Ok ( None ) ; } Err ( err ) => { if err . kind ( ) == std :: io :: ErrorKind :: WouldBlock { return Ok ( None ) ; } else { return Err ( err . into ( ) ) ; } } } let protocol = & header [ .. PROTOCOL_HEADER . len ( ) ] ; let length = & header [ PROTOCOL_HEADER . len ( ) .. ] ; let length = u32 :: from_le_bytes ( [ length [ 0 ] , length [ 1 ] , length [ 2 ] , length [ 3 ] ] ) as usize ; if protocol [ 0 .. 5 ] != PROTOCOL_HEADER [ 0 .. 5 ] { anyhow :: bail ! ( " " ) ; } if protocol != PROTOCOL_HEADER { anyhow :: bail ! ( " " PROTOCOL_HEADER [ 5 ] PROTOCOL_HEADER [ 6 ] PROTOCOL_HEADER [ 7 ] protocol [ 5 ] protocol [ 6 ] protocol [ 7 ] ) ; } if length > 32_000_000 { anyhow :: bail ! ( " " length as f32 1e-6 ) ; } // See if we have the whole packet yet: let mut length_and_packet = vec ! [ 0_u8 header len ( ) length ] ; match self . tcp_stream . peek ( & mut length_and_packet ) { Ok ( bytes_read ) => { if bytes_read != length_and_packet . len ( ) { return Ok ( None ) ; // not yet! } } Err ( err ) => { if err . kind ( ) == std :: io :: ErrorKind :: WouldBlock { return Ok ( None ) ; } else { return Err ( err . into ( ) ) ; } } } // consume the bytes: self . tcp_stream . read_exact ( & mut length_and_packet ) ? ; let packet = & length_and_packet [ header . len ( ) .. ] ; Ok ( Some ( packet . into ( ) ) ) }
returns immediately if there is nothing to read
returns immediately if there is nothing to read
&mut self
&mut self