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
use crate::{
either::EitherOutput,
protocols_handler::{KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
upgrade::{
DeniedUpgrade,
EitherUpgrade,
InboundUpgrade,
OutboundUpgrade,
}
};
use futures::prelude::*;
#[derive(Debug, Copy, Clone)]
pub struct Fuse<TProtoHandler> {
inner: Option<TProtoHandler>,
}
impl<TProtoHandler> Fuse<TProtoHandler> {
#[inline]
pub(crate) fn new(inner: TProtoHandler) -> Self {
Fuse {
inner: Some(inner),
}
}
#[inline]
pub fn is_shutdown(&self) -> bool {
self.inner.is_none()
}
}
impl<TProtoHandler> ProtocolsHandler for Fuse<TProtoHandler>
where
TProtoHandler: ProtocolsHandler,
{
type InEvent = TProtoHandler::InEvent;
type OutEvent = TProtoHandler::OutEvent;
type Error = TProtoHandler::Error;
type Substream = TProtoHandler::Substream;
type InboundProtocol = EitherUpgrade<TProtoHandler::InboundProtocol, DeniedUpgrade>;
type OutboundProtocol = TProtoHandler::OutboundProtocol;
type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
#[inline]
fn listen_protocol(&self) -> Self::InboundProtocol {
if let Some(inner) = self.inner.as_ref() {
EitherUpgrade::A(inner.listen_protocol())
} else {
EitherUpgrade::B(DeniedUpgrade)
}
}
#[inline]
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
) {
match (protocol, self.inner.as_mut()) {
(EitherOutput::First(proto), Some(inner)) => {
inner.inject_fully_negotiated_inbound(proto)
},
(EitherOutput::Second(_), None) => {}
(EitherOutput::First(_), None) => {}
(EitherOutput::Second(_), Some(_)) => {
panic!("Wrong API usage; an upgrade was passed to a different object that the \
one that asked for the upgrade")
},
}
}
#[inline]
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
info: Self::OutboundOpenInfo
) {
if let Some(inner) = self.inner.as_mut() {
inner.inject_fully_negotiated_outbound(protocol, info)
}
}
#[inline]
fn inject_event(&mut self, event: Self::InEvent) {
if let Some(inner) = self.inner.as_mut() {
inner.inject_event(event)
}
}
#[inline]
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
if let Some(inner) = self.inner.as_mut() {
inner.inject_dial_upgrade_error(info, error)
}
}
#[inline]
fn inject_inbound_closed(&mut self) {
if let Some(inner) = self.inner.as_mut() {
inner.inject_inbound_closed()
}
}
#[inline]
fn connection_keep_alive(&self) -> KeepAlive {
if let Some(inner) = self.inner.as_ref() {
inner.connection_keep_alive()
} else {
KeepAlive::Now
}
}
#[inline]
fn shutdown(&mut self) {
if let Some(inner) = self.inner.as_mut() {
inner.shutdown()
}
}
#[inline]
fn poll(
&mut self,
) -> Poll<
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
Self::Error,
> {
if let Some(mut inner) = self.inner.take() {
let poll = inner.poll();
if let Ok(Async::Ready(ProtocolsHandlerEvent::Shutdown)) = poll {
poll
} else {
self.inner = Some(inner);
poll
}
} else {
Ok(Async::Ready(ProtocolsHandlerEvent::Shutdown))
}
}
}