fix for peer connect fail on 'WouldBlock' Error ()

* fix for fail of peer connect: 'WouldBlock' Error.

* reuse the blocking variation of read_exact instead of re-implementing the loop
This commit is contained in:
Gary Yu 2018-08-30 15:21:13 +08:00 committed by Antioch Peverell
parent 436e68d6e0
commit 471e80e69e
2 changed files with 8 additions and 4 deletions

View file

@ -200,7 +200,7 @@ fn poll<H>(
let mut retry_send = Err(());
loop {
// check the read end
if let Some(h) = try_break!(error_tx, read_header(conn)) {
if let Some(h) = try_break!(error_tx, read_header(conn, None)) {
let msg = Message::from_header(h, conn);
trace!(
LOGGER,

View file

@ -188,9 +188,13 @@ pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: u32) -> io::Result<(
/// Read a header from the provided connection 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(conn: &mut TcpStream) -> Result<MsgHeader, Error> {
pub fn read_header(conn: &mut TcpStream, msg_type: Option<Type>) -> Result<MsgHeader, Error> {
let mut head = vec![0u8; HEADER_LEN as usize];
read_exact(conn, &mut head, 10000, false)?;
if Some(Type::Hand) == msg_type {
read_exact(conn, &mut head, 10, true)?;
} else {
read_exact(conn, &mut head, 10000, 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
@ -220,7 +224,7 @@ pub fn read_message<T>(conn: &mut TcpStream, msg_type: Type) -> Result<T, Error>
where
T: Readable,
{
let header = read_header(conn)?;
let header = read_header(conn, Some(msg_type))?;
if header.msg_type != msg_type {
return Err(Error::BadMessage);
}