/// If this function returns an error, then recovery is almost impossible, since no state about the channel is kept; it is undeterminable at what stage of message parsing an error occured.
/// This means all non-terminal errors should be handled by this function.
/// TODO: Possibly refactor this and message sending out into a Sender and Receiver Class
If this function returns an error, then recovery is almost impossible, since no state about the channel is kept; it is undeterminable at what stage of message parsing an error occured.
This means all non-terminal errors should be handled by this function.
TODO: Possibly refactor this and message sending out into a Sender and Receiver Class
If this function returns an error then recovery is almost impossible since no state about the channel is kept ; it is undeterminable at what stage of message parsing an error occured . This means all non - terminal errors should be handled by this function . TODO : Possibly refactor this and message sending out into a Sender and Receiver Class
pub fn recv<R: Read>(mut reader: R) -> Result<Self, Error> {
        let length = reader.read_u32::<BE>()?;
        if length == 0 {
            return Ok(Message::KeepAlive);
        }
        let id = reader.read_u8()?;
        let ret = match id {
            0 => Message::Choke,
            1 => Message::Unchoke,
            2 => Message::Interested,
            3 => Message::NotInterested,
            4 => Message::Have(reader.read_u32::<BE>()?),
            5 => {
                if length < 2 {
                    return Err(Error::SmallLength(2, length));
                }
                let bv_len = 8 * (length - 1);
                let mut bv = bitvec![0; bv_len as usize];
                reader.read_exact(bv.as_mut_slice())?;
                Message::BitField(bv)
            }
            6 => {
                let index = reader.read_u32::<BE>()?;
                let begin = reader.read_u32::<BE>()?;
                let length = reader.read_u32::<BE>()?;
                Message::Request(index, begin, length)
            }
            7 => {
                let index = reader.read_u32::<BE>()?;
                let begin = reader.read_u32::<BE>()?;
                if length < 10 {
                    return Err(Error::SmallLength(10, length));
                }
                let vec_len = length - 9;
                let mut v: Vec<u8> = Vec::with_capacity(vec_len as usize);
                reader.take(vec_len.into()).read_to_end(&mut v)?;
                Message::Piece(index, begin, Arc::new(v))
            }
            8 => {
                let index = reader.read_u32::<BE>()?;
                let begin = reader.read_u32::<BE>()?;
                let length = reader.read_u32::<BE>()?;
                Message::Cancel(index, begin, length)
            }
            9 => Message::Port(reader.read_u16::<BE>()?),
            _ => return Err(Error::Invalid(id)),
        };
        ret.validate(length)?;
        Ok(ret)
    }
pub fn recv < R : Read > ( mut reader : R ) -> Result < Self , Error > { let length = reader . read_u32 :: < BE > ( ) ? ; if length == 0 { return Ok ( Message :: KeepAlive ) ; } let id = reader . read_u8 ( ) ? ; let ret = match id { 0 => Message :: Choke , 1 => Message :: Unchoke , 2 => Message :: Interested , 3 => Message :: NotInterested , 4 => Message :: Have ( reader . read_u32 :: < BE > ( ) ? ) , 5 => { if length < 2 { return Err ( Error :: SmallLength ( 2 , length ) ) ; } let bv_len = 8 * ( length - 1 ) ; let mut bv = bitvec ! [ 0 bv_len as usize ] ; reader . read_exact ( bv . as_mut_slice ( ) ) ? ; Message :: BitField ( bv ) } 6 => { let index = reader . read_u32 :: < BE > ( ) ? ; let begin = reader . read_u32 :: < BE > ( ) ? ; let length = reader . read_u32 :: < BE > ( ) ? ; Message :: Request ( index , begin , length ) } 7 => { let index = reader . read_u32 :: < BE > ( ) ? ; let begin = reader . read_u32 :: < BE > ( ) ? ; if length < 10 { return Err ( Error :: SmallLength ( 10 , length ) ) ; } let vec_len = length - 9 ; let mut v : Vec < u8 > = Vec :: with_capacity ( vec_len as usize ) ; reader . take ( vec_len . into ( ) ) . read_to_end ( & mut v ) ? ; Message :: Piece ( index , begin , Arc :: new ( v ) ) } 8 => { let index = reader . read_u32 :: < BE > ( ) ? ; let begin = reader . read_u32 :: < BE > ( ) ? ; let length = reader . read_u32 :: < BE > ( ) ? ; Message :: Cancel ( index , begin , length ) } 9 => Message :: Port ( reader . read_u16 :: < BE > ( ) ? ) , _ => return Err ( Error :: Invalid ( id ) ) , } ; ret . validate ( length ) ? ; Ok ( ret ) }
If this function returns an error, then recovery is almost impossible, since no state about the channel is kept; it is undeterminable at what stage of message parsing an error occured.
If this function returns an error then recovery is almost impossible since no state about the channel is kept ; it is undeterminable at what stage of message parsing an error occured .
reader
R