pub fn from_stream<S>( stream: &mut S ) -> Result<WitcherPacket, String> where S: Read {
        let err = String::from("Failed to read packet from stream - ");
        let mut short_buffer = [0,0];
        let mut payload_buffer = Vec::<u8>::with_capacity(512);
        let mut packet = WitcherPacket::new();

        if let Err(e) = stream.read_exact(&mut short_buffer) {
            return Err(err + &e.to_string());
        }
        if short_buffer != constants::PACKET_HEAD {
            return Err(err + "Incorrect packet head");
        }

        if let Err(e) = stream.read_exact(&mut short_buffer) {
            return Err(err + &e.to_string());
        }
        let packet_len: u16 = u16::from_be_bytes(short_buffer);
        if packet_len > 6 {
            if let Err(e) = stream.take(packet_len as u64 - 6).read_to_end(&mut payload_buffer) {
                return Err(err + &e.to_string());
            }        
        }

        if let Err(e) = stream.read_exact(&mut short_buffer) {
            return Err(err + &e.to_string());
        }
        if short_buffer != constants::PACKET_TAIL {
            return Err(err + "Incorrect packet tail");
        }

        match WitcherPacketData::from_bytes(&payload_buffer) {
            Ok(v) => {
                packet.payload = v;
            }
            Err(e) => {
                return Err(err + &e); 
            }
        }

        Ok(packet)
    }
/// Will block until is able to read all the data it needs or encounters an error
Will block until is able to read all the data it needs or encounters an error
Will block until is able to read all the data it needs or encounters an error
pub fn from_stream<S>( stream: &mut S ) -> Result<WitcherPacket, String> where S: Read {
        let err = String::from("Failed to read packet from stream - ");
        let mut short_buffer = [0,0];
        let mut payload_buffer = Vec::<u8>::with_capacity(512);
        let mut packet = WitcherPacket::new();
        if let Err(e) = stream.read_exact(&mut short_buffer) {
            return Err(err + &e.to_string());
        }
        if short_buffer != constants::PACKET_HEAD {
            return Err(err + "Incorrect packet head");
        }
        if let Err(e) = stream.read_exact(&mut short_buffer) {
            return Err(err + &e.to_string());
        }
        let packet_len: u16 = u16::from_be_bytes(short_buffer);
        if packet_len > 6 {
            if let Err(e) = stream.take(packet_len as u64 - 6).read_to_end(&mut payload_buffer) {
                return Err(err + &e.to_string());
            }        
        }
        if let Err(e) = stream.read_exact(&mut short_buffer) {
            return Err(err + &e.to_string());
        }
        if short_buffer != constants::PACKET_TAIL {
            return Err(err + "Incorrect packet tail");
        }
        match WitcherPacketData::from_bytes(&payload_buffer) {
            Ok(v) => {
                packet.payload = v;
            }
            Err(e) => {
                return Err(err + &e); 
            }
        }
        Ok(packet)
    }
pub fn from_stream < S > ( stream : & mut S ) -> Result < WitcherPacket , String > where S : Read { let err = String :: from ( " " ) ; let mut short_buffer = [ 0 , 0 ] ; let mut payload_buffer = Vec :: < u8 > :: with_capacity ( 512 ) ; let mut packet = WitcherPacket :: new ( ) ; if let Err ( e ) = stream . read_exact ( & mut short_buffer ) { return Err ( err + & e . to_string ( ) ) ; } if short_buffer != constants :: PACKET_HEAD { return Err ( err + " " ) ; } if let Err ( e ) = stream . read_exact ( & mut short_buffer ) { return Err ( err + & e . to_string ( ) ) ; } let packet_len : u16 = u16 :: from_be_bytes ( short_buffer ) ; if packet_len > 6 { if let Err ( e ) = stream . take ( packet_len as u64 - 6 ) . read_to_end ( & mut payload_buffer ) { return Err ( err + & e . to_string ( ) ) ; } } if let Err ( e ) = stream . read_exact ( & mut short_buffer ) { return Err ( err + & e . to_string ( ) ) ; } if short_buffer != constants :: PACKET_TAIL { return Err ( err + " " ) ; } match WitcherPacketData :: from_bytes ( & payload_buffer ) { Ok ( v ) => { packet . payload = v ; } Err ( e ) => { return Err ( err + & e ) ; } } Ok ( packet ) }
Will block until is able to read all the data it needs or encounters an error
Will block until is able to read all the data it needs or encounters an error
stream
&mut S
stream
&mut S