// Fills buffer with data from file, and returns the number of bytes read
Fills buffer with data from file, and returns the number of bytes read
Fills buffer with data from file and returns the number of bytes read
pub fn read_timeout(file: &mut File, mut buffer: &mut Vec<u8>, duration: u32) -> Result<usize, Error> {
	let file_fd = file.as_raw_fd();
	//println!("read_timeout FD: {:?}", file_fd);
	//let mut blank_fd_set = FdSet::new();
	let mut file_fd_set = FdSet::new();
	file_fd_set.insert(file_fd);
	let duration_i64 = duration as i64;
	let mut timeval_millis = TimeValLike::milliseconds(duration_i64);
	match select(file_fd+1, Some(&mut file_fd_set), None, None, Some(&mut timeval_millis)) {
		Ok(1) => {
			// Ok to read from file
			// debug
			//println!("Select succeeded with a 1");
			// Read tup to the max number of bytes to fill the packet (probably 58)
			let bytes_read = file.read(&mut buffer).unwrap();
			Ok(bytes_read)
		}
		Ok(0) => {
			// Timeout
			//println!("Select timed out");
			Err(Error::new(ErrorKind::Other, "Read pty timed out"))
		}
		Ok(_) => {
			// Something else we don't expect
			panic!("Got a weird return code from select");
		}
		Err(e) => {
			// Timeout
			panic!("Select had an error {}",e);
		}
	}
}
pub fn read_timeout ( file : & mut File , mut buffer : & mut Vec < u8 > , duration : u32 ) -> Result < usize , Error > { let file_fd = file . as_raw_fd ( ) ; //println!("read_timeout FD: {:?}", file_fd); //let mut blank_fd_set = FdSet::new(); let mut file_fd_set = FdSet :: new ( ) ; file_fd_set . insert ( file_fd ) ; let duration_i64 = duration as i64 ; let mut timeval_millis = TimeValLike :: milliseconds ( duration_i64 ) ; match select ( file_fd + 1 , Some ( & mut file_fd_set ) , None , None , Some ( & mut timeval_millis ) ) { Ok ( 1 ) => { // Ok to read from file // debug //println!("Select succeeded with a 1"); // Read tup to the max number of bytes to fill the packet (probably 58) let bytes_read = file . read ( & mut buffer ) . unwrap ( ) ; Ok ( bytes_read ) } Ok ( 0 ) => { // Timeout //println!("Select timed out"); Err ( Error :: new ( ErrorKind :: Other , " " ) ) } Ok ( _ ) => { // Something else we don't expect panic ! ( " " ) ; } Err ( e ) => { // Timeout panic ! ( " " e ) ; } } }
Fills buffer with data from file, and returns the number of bytes read
Fills buffer with data from file and returns the number of bytes read
file
&mut File
buffer
&mut Vec<u8>
duration
u32