/// Attempts to decode a connection header from an I/O stream.
///
/// If the header is not present, the non-header bytes that were read are
/// returned.
///
/// An I/O error is returned if the connection header is invalid.
Attempts to decode a connection header from an I/O stream.
If the header is not present, the non-header bytes that were read are
returned.

An I/O error is returned if the connection header is invalid.
Attempts to decode a connection header from an I / O stream . If the header is not present the non - header bytes that were read are returned . An I / O error is returned if the connection header is invalid .
async fn read_prefaced(
        io: &mut (impl io::AsyncRead + Unpin),
        buf: &mut BytesMut,
    ) -> io::Result<Option<Self>> {
        // Read at least enough data to determine whether a connection header is
        // present and, if so, how long it is.
        while buf.len() < PREFACE_AND_SIZE_LEN {
            if io.read_buf(buf).await? == 0 {
                return Ok(None);
            }
        }
        // Advance the buffer past the preface if it matches.
        if &buf.chunk()[..PREFACE.len()] != PREFACE {
            return Ok(None);
        }
        buf.advance(PREFACE.len());
        // Read the message length. If it is larger than our allowed buffer
        // capacity, fail the connection.
        let msg_len = buf.get_u32() as usize;
        if msg_len > buf.capacity() + PREFACE_AND_SIZE_LEN {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Message length exceeds capacity",
            ));
        }
        // Free up parsed preface data and ensure there's enough capacity for
        // the message.
        buf.reserve(msg_len);
        while buf.len() < msg_len {
            if io.read_buf(buf).await? == 0 {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "Full header message not provided",
                ));
            }
        }
        // Take the bytes needed to parse the message and leave the remaining
        // bytes in the caller-provided buffer.
        let msg = buf.split_to(msg_len);
        Self::decode(msg.freeze())
    }
async fn read_prefaced ( io : & mut ( impl io :: AsyncRead + Unpin ) , buf : & mut BytesMut , ) -> io :: Result < Option < Self > > { // Read at least enough data to determine whether a connection header is // present and, if so, how long it is. while buf . len ( ) < PREFACE_AND_SIZE_LEN { if io . read_buf ( buf ) . await ? == 0 { return Ok ( None ) ; } } // Advance the buffer past the preface if it matches. if & buf . chunk ( ) [ .. PREFACE . len ( ) ] != PREFACE { return Ok ( None ) ; } buf . advance ( PREFACE . len ( ) ) ; // Read the message length. If it is larger than our allowed buffer // capacity, fail the connection. let msg_len = buf . get_u32 ( ) as usize ; if msg_len > buf . capacity ( ) + PREFACE_AND_SIZE_LEN { return Err ( io :: Error :: new ( io :: ErrorKind :: InvalidData , " " , ) ) ; } // Free up parsed preface data and ensure there's enough capacity for // the message. buf . reserve ( msg_len ) ; while buf . len ( ) < msg_len { if io . read_buf ( buf ) . await ? == 0 { return Err ( io :: Error :: new ( io :: ErrorKind :: UnexpectedEof , " " , ) ) ; } } // Take the bytes needed to parse the message and leave the remaining // bytes in the caller-provided buffer. let msg = buf . split_to ( msg_len ) ; Self :: decode ( msg . freeze ( ) ) }
Attempts to decode a connection header from an I/O stream.
Attempts to decode a connection header from an I / O stream .
io
&mut (impl io::AsyncRead + Unpin)
buf
&mut BytesMut