Add forgotten ping loop, fix TCP send interlacing

This commit is contained in:
Ignotus Peverell 2018-02-05 23:09:57 +00:00
parent a9f4f36117
commit eb0ebab2d3
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
3 changed files with 20 additions and 19 deletions

View file

@ -87,9 +87,8 @@ impl Tracker {
where where
T: ser::Writeable T: ser::Writeable
{ {
let (header_buf, body_buf) = write_to_bufs(body, msg_type); let buf = write_to_buf(body, msg_type);
self.send_channel.send(header_buf)?; self.send_channel.send(buf)?;
self.send_channel.send(body_buf)?;
Ok(()) Ok(())
} }
} }
@ -175,8 +174,8 @@ where
}); });
} }
fn respond(send_tx: &mpsc::Sender<Vec<u8>>, msg_type: Type, body: Vec<u8>) { fn respond(send_tx: &mpsc::Sender<Vec<u8>>, msg_type: Type, mut body: Vec<u8>) {
let header = ser::ser_vec(&MsgHeader::new(msg_type, body.len() as u64)).unwrap(); let mut msg = ser::ser_vec(&MsgHeader::new(msg_type, body.len() as u64)).unwrap();
send_tx.send(header).unwrap(); msg.append(&mut body);
send_tx.send(body).unwrap(); send_tx.send(msg).unwrap();
} }

View file

@ -158,10 +158,10 @@ where
read_body(&header, conn) read_body(&header, conn)
} }
pub fn write_to_bufs<T>( pub fn write_to_buf<T>(
msg: T, msg: T,
msg_type: Type, msg_type: Type,
) -> (Vec<u8>, Vec<u8>) ) -> Vec<u8>
where where
T: Writeable, T: Writeable,
{ {
@ -170,11 +170,12 @@ where
ser::serialize(&mut body_buf, &msg).unwrap(); ser::serialize(&mut body_buf, &msg).unwrap();
// build and serialize the header using the body size // build and serialize the header using the body size
let mut header_buf = vec![]; let mut msg_buf = vec![];
let blen = body_buf.len() as u64; let blen = body_buf.len() as u64;
ser::serialize(&mut header_buf, &MsgHeader::new(msg_type, blen)).unwrap(); ser::serialize(&mut msg_buf, &MsgHeader::new(msg_type, blen)).unwrap();
msg_buf.append(&mut body_buf);
(header_buf, body_buf) msg_buf
} }
pub fn write_message<T>( pub fn write_message<T>(
@ -185,10 +186,9 @@ pub fn write_message<T>(
where where
T: Writeable + 'static, T: Writeable + 'static,
{ {
let (header_buf, body_buf) = write_to_bufs(msg, msg_type); let buf = write_to_buf(msg, msg_type);
// send the whole thing // send the whole thing
conn.write_all(&header_buf[..])?; conn.write_all(&buf[..])?;
conn.write_all(&body_buf[..])?;
Ok(()) Ok(())
} }

View file

@ -65,10 +65,12 @@ impl Server {
// start peer monitoring thread // start peer monitoring thread
let peers_inner = self.peers.clone(); let peers_inner = self.peers.clone();
let _ = thread::Builder::new().name("p2p-monitor".to_string()).spawn(move || { let _ = thread::Builder::new().name("p2p-monitor".to_string()).spawn(move || {
let total_diff = peers_inner.total_difficulty(); loop {
let total_height = peers_inner.total_height(); let total_diff = peers_inner.total_difficulty();
peers_inner.check_all(total_diff, total_height); let total_height = peers_inner.total_height();
thread::sleep(Duration::from_secs(20)); peers_inner.check_all(total_diff, total_height);
thread::sleep(Duration::from_secs(20));
}
}); });
// start TCP listener and handle incoming connections // start TCP listener and handle incoming connections