pub fn read_header(stream: &mut dyn Read, msg_type: Option<Type>) -> Result<MsgHeader, Error> {
	let mut head = vec![0u8; MsgHeader::LEN];
	if Some(Type::Hand) == msg_type {
		read_exact(stream, &mut head, time::Duration::from_millis(10), true)?;
	} else {
		read_exact(stream, &mut head, time::Duration::from_secs(10), false)?;
	}
	let header = ser::deserialize::<MsgHeader>(&mut &head[..])?;
	let max_len = max_msg_size(header.msg_type);

	// TODO 4x the limits for now to leave ourselves space to change things
	if header.msg_len > max_len * 4 {
		error!(
			"Too large read {}, had {}, wanted {}.",
			header.msg_type as u8, max_len, header.msg_len
		);
		return Err(Error::Serialization(ser::Error::TooLargeReadErr));
	}
	Ok(header)
}
/// Read a header from the provided stream without blocking if the
/// underlying stream is async. Typically headers will be polled for, so
/// we do not want to block.
Read a header from the provided stream without blocking if the
underlying stream is async. Typically headers will be polled for, so
we do not want to block.
Read a header from the provided stream without blocking if the underlying stream is async . Typically headers will be polled for so we do not want to block .
pub fn read_header(stream: &mut dyn Read, msg_type: Option<Type>) -> Result<MsgHeader, Error> {
	let mut head = vec![0u8; MsgHeader::LEN];
	if Some(Type::Hand) == msg_type {
		read_exact(stream, &mut head, time::Duration::from_millis(10), true)?;
	} else {
		read_exact(stream, &mut head, time::Duration::from_secs(10), false)?;
	}
	let header = ser::deserialize::<MsgHeader>(&mut &head[..])?;
	let max_len = max_msg_size(header.msg_type);
	// TODO 4x the limits for now to leave ourselves space to change things
	if header.msg_len > max_len * 4 {
		error!(
			"Too large read {}, had {}, wanted {}.",
			header.msg_type as u8, max_len, header.msg_len
		);
		return Err(Error::Serialization(ser::Error::TooLargeReadErr));
	}
	Ok(header)
}
pub fn read_header ( stream : & mut dyn Read , msg_type : Option < Type > ) -> Result < MsgHeader , Error > { let mut head = vec ! [ 0u8 MsgHeader LEN ] ; if Some ( Type :: Hand ) == msg_type { read_exact ( stream , & mut head , time :: Duration :: from_millis ( 10 ) , true ) ? ; } else { read_exact ( stream , & mut head , time :: Duration :: from_secs ( 10 ) , false ) ? ; } let header = ser :: deserialize :: < MsgHeader > ( & mut & head [ .. ] ) ? ; let max_len = max_msg_size ( header . msg_type ) ; // TODO 4x the limits for now to leave ourselves space to change things if header . msg_len > max_len * 4 { error ! ( " " header msg_type as u8 max_len header msg_len ) ; return Err ( Error :: Serialization ( ser :: Error :: TooLargeReadErr ) ) ; } Ok ( header ) }
Read a header from the provided stream without blocking if the
underlying stream is async.
Read a header from the provided stream without blocking if the underlying stream is async .
stream
&mut dyn Read
msg_type
Option<Type>