1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::sync::Arc;
use time;
use bytes::Bytes;
use message::{Error, Payload, deserialize_payload};
use message::types::{Ping, Pong};
use message::common::Command;
use protocol::Protocol;
use net::PeerContext;
use util::nonce::{NonceGenerator, RandomNonce};
const PING_INTERVAL_S: f64 = 60f64;
const MAX_PING_RESPONSE_TIME_S: f64 = 60f64;
#[derive(Debug, Copy, Clone, PartialEq)]
enum State {
WaitingTimeout(f64),
WaitingPong(f64),
}
pub struct PingProtocol<T = RandomNonce, C = PeerContext> {
context: Arc<C>,
nonce_generator: T,
state: State,
last_ping_nonce: Option<u64>,
}
impl PingProtocol {
pub fn new(context: Arc<PeerContext>) -> Self {
PingProtocol {
context: context,
nonce_generator: RandomNonce::default(),
state: State::WaitingTimeout(time::precise_time_s()),
last_ping_nonce: None,
}
}
}
impl Protocol for PingProtocol {
fn initialize(&mut self) {
self.maintain();
}
fn maintain(&mut self) {
let now = time::precise_time_s();
match self.state {
State::WaitingTimeout(time) => {
if now - time > PING_INTERVAL_S {
let nonce = self.nonce_generator.get();
self.state = State::WaitingPong(now);
self.last_ping_nonce = Some(nonce);
let ping = Ping::new(nonce);
self.context.send_request(&ping);
}
},
State::WaitingPong(time) => {
if now - time > MAX_PING_RESPONSE_TIME_S {
trace!("closing connection to peer {}: no messages for last {} seconds", self.context.info().id, now - time);
self.context.close();
}
},
}
}
fn on_message(&mut self, command: &Command, payload: &Bytes) -> Result<(), Error> {
self.state = State::WaitingTimeout(time::precise_time_s());
if command == &Ping::command() {
let ping: Ping = try!(deserialize_payload(payload, self.context.info().version));
let pong = Pong::new(ping.nonce);
self.context.send_response_inline(&pong);
} else if command == &Pong::command() {
let pong: Pong = try!(deserialize_payload(payload, self.context.info().version));
if Some(pong.nonce) != self.last_ping_nonce.take() {
return Err(Error::InvalidCommand)
}
}
Ok(())
}
}