Fixed stale connections.

This commit is contained in:
Revertron
2022-09-05 15:35:28 +02:00
parent de4b9ac65e
commit 3949bd046b
3 changed files with 33 additions and 19 deletions
+9 -15
View File
@@ -1,11 +1,10 @@
use std::collections::HashMap;
use std::net::SocketAddr;
use std::time::Instant;
use mio::net::TcpStream;
use crate::crypto::Chacha;
use crate::p2p::State;
use crate::Block;
#[derive(Debug)]
pub struct Peer {
@@ -17,11 +16,11 @@ pub struct Peer {
inbound: bool,
public: bool,
active: bool,
last_active: Instant,
reconnects: u32,
received_block: u64,
sent_height: u64,
cipher: Option<Chacha>,
fork: HashMap<u64, Block>
cipher: Option<Chacha>
}
impl Peer {
@@ -35,11 +34,11 @@ impl Peer {
inbound,
public: false,
active: false,
last_active: Instant::now(),
reconnects: 0,
received_block: 0,
sent_height: 0,
cipher: None,
fork: HashMap::new()
cipher: None
}
}
@@ -133,10 +132,13 @@ impl Peer {
pub fn set_active(&mut self, active: bool) {
self.active = active;
if active {
self.last_active = Instant::now();
}
}
pub fn active(&self) -> bool {
self.active
self.active && self.last_active.elapsed().as_secs() < 120
}
pub fn reconnects(&self) -> u32 {
@@ -159,14 +161,6 @@ impl Peer {
self.inbound
}
pub fn add_fork_block(&mut self, block: Block) {
self.fork.insert(block.index, block);
}
pub fn get_fork(&self) -> &HashMap<u64, Block> {
&self.fork
}
/// If loopback address then we care about ip and port.
/// If regular address then we only care about the ip and ignore the port.
pub fn equals(&self, addr: &SocketAddr) -> bool {