pub async fn receive_bytes(socket: &mut TcpStream) -> Result<Vec<u8>> {
    // Receive the header with the overall message size
    let mut header = vec![0; 8];
    socket.read(&mut header).await?;
    let mut header = Cursor::new(header);
    let message_size = header.read_u64::<BigEndian>()? as usize;

    // Buffer for the whole payload
    let mut payload_bytes = Vec::with_capacity(message_size);

    // Receive chunks until we reached the expected message size
    while payload_bytes.len() < message_size {
        // Calculate the amount of bytes left
        // By default try a buffer size of 1024 bytes
        let mut chunk_size = message_size - payload_bytes.len();
        if chunk_size > 1024 {
            chunk_size = 1024;
        }

        // Read data and get the amount of received bytes
        let mut chunk = vec![0; chunk_size];
        let received_bytes = socket.read(&mut chunk).await?;

        // If we received less bytes than the chunk buffer size,
        // split the unneeded bytes, since they are filled with zeros
        if received_bytes < chunk_size {
            chunk.split_off(received_bytes);
        }

        payload_bytes.append(&mut chunk);
    }

    Ok(payload_bytes)
}
/// Receive a byte stream depending on a given header
/// This is the basic protocol beneath all pueue communication
Receive a byte stream depending on a given header
This is the basic protocol beneath all pueue communication
Receive a byte stream depending on a given header This is the basic protocol beneath all pueue communication
pub async fn receive_bytes(socket: &mut TcpStream) -> Result<Vec<u8>> {
    // Receive the header with the overall message size
    let mut header = vec![0; 8];
    socket.read(&mut header).await?;
    let mut header = Cursor::new(header);
    let message_size = header.read_u64::<BigEndian>()? as usize;
    // Buffer for the whole payload
    let mut payload_bytes = Vec::with_capacity(message_size);
    // Receive chunks until we reached the expected message size
    while payload_bytes.len() < message_size {
        // Calculate the amount of bytes left
        // By default try a buffer size of 1024 bytes
        let mut chunk_size = message_size - payload_bytes.len();
        if chunk_size > 1024 {
            chunk_size = 1024;
        }
        // Read data and get the amount of received bytes
        let mut chunk = vec![0; chunk_size];
        let received_bytes = socket.read(&mut chunk).await?;
        // If we received less bytes than the chunk buffer size,
        // split the unneeded bytes, since they are filled with zeros
        if received_bytes < chunk_size {
            chunk.split_off(received_bytes);
        }
        payload_bytes.append(&mut chunk);
    }
    Ok(payload_bytes)
}
pub async fn receive_bytes ( socket : & mut TcpStream ) -> Result < Vec < u8 > > { // Receive the header with the overall message size let mut header = vec ! [ 0 8 ] ; socket . read ( & mut header ) . await ? ; let mut header = Cursor :: new ( header ) ; let message_size = header . read_u64 :: < BigEndian > ( ) ? as usize ; // Buffer for the whole payload let mut payload_bytes = Vec :: with_capacity ( message_size ) ; // Receive chunks until we reached the expected message size while payload_bytes . len ( ) < message_size { // Calculate the amount of bytes left // By default try a buffer size of 1024 bytes let mut chunk_size = message_size - payload_bytes . len ( ) ; if chunk_size > 1024 { chunk_size = 1024 ; } // Read data and get the amount of received bytes let mut chunk = vec ! [ 0 chunk_size ] ; let received_bytes = socket . read ( & mut chunk ) . await ? ; // If we received less bytes than the chunk buffer size, // split the unneeded bytes, since they are filled with zeros if received_bytes < chunk_size { chunk . split_off ( received_bytes ) ; } payload_bytes . append ( & mut chunk ) ; } Ok ( payload_bytes ) }
Receive a byte stream depending on a given header
This is the basic protocol beneath all pueue communication
Receive a byte stream depending on a given header This is the basic protocol beneath all pueue communication
socket
&mut TcpStream
socket
&mut TcpStream