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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::protocol::{RemoteInfo, IdentifyProtocolConfig};
use futures::prelude::*;
use libp2p_core::upgrade::{DeniedUpgrade, OutboundUpgrade};
use libp2p_swarm::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use std::{io, marker::PhantomData, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};
use wasm_timer::{Delay, Instant};
use void::{Void, unreachable};
const DELAY_TO_FIRST_ID: Duration = Duration::from_millis(500);
const DELAY_TO_NEXT_ID: Duration = Duration::from_secs(5 * 60);
const TRY_AGAIN_ON_ERR: Duration = Duration::from_secs(60 * 60);
pub struct PeriodicIdHandler<TSubstream> {
config: IdentifyProtocolConfig,
pending_result: Option<PeriodicIdHandlerEvent>,
next_id: Delay,
first_id_happened: bool,
marker: PhantomData<TSubstream>,
}
#[derive(Debug)]
pub enum PeriodicIdHandlerEvent {
Identified(RemoteInfo),
IdentificationError(ProtocolsHandlerUpgrErr<io::Error>),
}
impl<TSubstream> PeriodicIdHandler<TSubstream> {
#[inline]
pub fn new() -> Self {
PeriodicIdHandler {
config: IdentifyProtocolConfig,
pending_result: None,
next_id: Delay::new(Instant::now() + DELAY_TO_FIRST_ID),
first_id_happened: false,
marker: PhantomData,
}
}
}
impl<TSubstream> ProtocolsHandler for PeriodicIdHandler<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite,
{
type InEvent = Void;
type OutEvent = PeriodicIdHandlerEvent;
type Error = wasm_timer::Error;
type Substream = TSubstream;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = IdentifyProtocolConfig;
type OutboundOpenInfo = ();
#[inline]
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
SubstreamProtocol::new(DeniedUpgrade)
}
fn inject_fully_negotiated_inbound(&mut self, protocol: Void) {
unreachable(protocol)
}
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgrade<TSubstream>>::Output,
_info: Self::OutboundOpenInfo,
) {
self.pending_result = Some(PeriodicIdHandlerEvent::Identified(protocol));
self.first_id_happened = true;
}
#[inline]
fn inject_event(&mut self, _: Self::InEvent) {}
#[inline]
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, err: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
self.pending_result = Some(PeriodicIdHandlerEvent::IdentificationError(err));
self.first_id_happened = true;
self.next_id.reset(Instant::now() + TRY_AGAIN_ON_ERR);
}
#[inline]
fn connection_keep_alive(&self) -> KeepAlive {
if self.first_id_happened {
KeepAlive::No
} else {
KeepAlive::Yes
}
}
fn poll(
&mut self,
) -> Poll<
ProtocolsHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
PeriodicIdHandlerEvent,
>,
Self::Error,
> {
if let Some(pending_result) = self.pending_result.take() {
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(
pending_result,
)));
}
match self.next_id.poll()? {
Async::NotReady => Ok(Async::NotReady),
Async::Ready(()) => {
self.next_id.reset(Instant::now() + DELAY_TO_NEXT_ID);
let ev = ProtocolsHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(self.config.clone()),
info: (),
};
Ok(Async::Ready(ev))
}
}
}
}