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

View file

@ -158,10 +158,10 @@ where
read_body(&header, conn)
}
pub fn write_to_bufs<T>(
pub fn write_to_buf<T>(
msg: T,
msg_type: Type,
) -> (Vec<u8>, Vec<u8>)
) -> Vec<u8>
where
T: Writeable,
{
@ -170,11 +170,12 @@ where
ser::serialize(&mut body_buf, &msg).unwrap();
// 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;
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>(
@ -185,10 +186,9 @@ pub fn write_message<T>(
where
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
conn.write_all(&header_buf[..])?;
conn.write_all(&body_buf[..])?;
conn.write_all(&buf[..])?;
Ok(())
}

View file

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