diff --git a/p2p/src/conn.rs b/p2p/src/conn.rs index 3e0042d57..358568610 100644 --- a/p2p/src/conn.rs +++ b/p2p/src/conn.rs @@ -82,7 +82,12 @@ impl<'a> Message<'a> { while written < len { let read_len = cmp::min(8000, len - written); let mut buf = vec![0u8; read_len]; - read_exact(&mut self.conn, &mut buf[..], 10000, true)?; + read_exact( + &mut self.conn, + &mut buf[..], + time::Duration::from_secs(10), + true, + )?; writer.write_all(&mut buf)?; written += read_len; } @@ -117,13 +122,13 @@ impl<'a> Response<'a> { let mut msg = ser::ser_vec(&MsgHeader::new(self.resp_type, self.body.len() as u64)).unwrap(); msg.append(&mut self.body); - write_all(&mut self.conn, &msg[..], 10000)?; + write_all(&mut self.conn, &msg[..], time::Duration::from_secs(10))?; if let Some(mut file) = self.attachment { let mut buf = [0u8; 8000]; loop { match file.read(&mut buf[..]) { Ok(0) => break, - Ok(n) => write_all(&mut self.conn, &buf[..n], 10000)?, + Ok(n) => write_all(&mut self.conn, &buf[..n], time::Duration::from_secs(10))?, Err(e) => return Err(From::from(e)), } } diff --git a/p2p/src/msg.rs b/p2p/src/msg.rs index dbcd9a3b7..e5e8ad7d0 100644 --- a/p2p/src/msg.rs +++ b/p2p/src/msg.rs @@ -112,11 +112,11 @@ fn max_msg_size(msg_type: Type) -> u64 { pub fn read_exact( conn: &mut TcpStream, mut buf: &mut [u8], - timeout: u32, + timeout: time::Duration, block_on_empty: bool, ) -> io::Result<()> { - let sleep_time = time::Duration::from_millis(1); - let mut count = 0; + let sleep_time = time::Duration::from_micros(10); + let mut count = time::Duration::new(0, 0); let mut read = 0; loop { @@ -137,7 +137,7 @@ pub fn read_exact( } if !buf.is_empty() { thread::sleep(sleep_time); - count += 1; + count += sleep_time; } else { break; } @@ -152,9 +152,9 @@ pub fn read_exact( } /// Same as `read_exact` but for writing. -pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: u32) -> io::Result<()> { - let sleep_time = time::Duration::from_millis(1); - let mut count = 0; +pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: time::Duration) -> io::Result<()> { + let sleep_time = time::Duration::from_micros(10); + let mut count = time::Duration::new(0, 0); while !buf.is_empty() { match conn.write(buf) { @@ -171,7 +171,7 @@ pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: u32) -> io::Result<( } if !buf.is_empty() { thread::sleep(sleep_time); - count += 1; + count += sleep_time; } else { break; } @@ -191,9 +191,9 @@ pub fn write_all(conn: &mut Write, mut buf: &[u8], timeout: u32) -> io::Result<( pub fn read_header(conn: &mut TcpStream, msg_type: Option) -> Result { let mut head = vec![0u8; HEADER_LEN as usize]; if Some(Type::Hand) == msg_type { - read_exact(conn, &mut head, 10, true)?; + read_exact(conn, &mut head, time::Duration::from_millis(10), true)?; } else { - read_exact(conn, &mut head, 10000, false)?; + read_exact(conn, &mut head, time::Duration::from_secs(10), false)?; } let header = ser::deserialize::(&mut &head[..])?; let max_len = max_msg_size(header.msg_type); @@ -215,7 +215,7 @@ where T: Readable, { let mut body = vec![0u8; h.msg_len as usize]; - read_exact(conn, &mut body, 20000, true)?; + read_exact(conn, &mut body, time::Duration::from_secs(20), true)?; ser::deserialize(&mut &body[..]).map_err(From::from) } diff --git a/p2p/src/protocol.rs b/p2p/src/protocol.rs index 029f40d04..b9a1a2f41 100644 --- a/p2p/src/protocol.rs +++ b/p2p/src/protocol.rs @@ -17,6 +17,7 @@ use std::fs::File; use std::io::{self, BufWriter}; use std::net::{SocketAddr, TcpStream}; use std::sync::Arc; +use std::time; use conn::{Message, MessageHandler, Response}; use core::core::{self, hash::Hash, CompactBlock}; @@ -317,7 +318,7 @@ impl MessageHandler for Protocol { fn headers_header_size(conn: &mut TcpStream, msg_len: u64) -> Result { let mut size = vec![0u8; 2]; // read size of Vec - read_exact(conn, &mut size, 20000, true)?; + read_exact(conn, &mut size, time::Duration::from_millis(10), true)?; let total_headers = size[0] as u64 * 256 + size[1] as u64; if total_headers == 0 || total_headers > 10_000 { @@ -391,7 +392,7 @@ fn headers_streaming_body( // 3rd part let mut read_body = vec![0u8; read_size as usize]; if read_size > 0 { - read_exact(conn, &mut read_body, 20000, true)?; + read_exact(conn, &mut read_body, time::Duration::from_secs(20), true)?; *total_read += read_size; } body.append(&mut read_body);