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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use async_trait::async_trait;
use futures::prelude::*;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use std::{error::Error, fmt};
use libp2prs_multiaddr::Multiaddr;
use crate::multistream::NegotiationError;
use crate::pnet::PnetError;
pub mod dummy;
pub mod memory;
pub mod protector;
pub mod timeout;
pub mod upgrade;
#[async_trait]
pub trait Transport: Send {
type Output;
fn listen_on(&mut self, addr: Multiaddr) -> Result<IListener<Self::Output>, TransportError>;
async fn dial(&mut self, addr: Multiaddr) -> Result<Self::Output, TransportError>;
fn box_clone(&self) -> ITransport<Self::Output>;
fn protocols(&self) -> Vec<u32>;
fn timeout(self, timeout: Duration) -> timeout::TransportTimeout<Self>
where
Self: Sized,
{
timeout::TransportTimeout::new(self, timeout)
}
fn outbound_timeout(self, timeout: Duration) -> timeout::TransportTimeout<Self>
where
Self: Sized,
{
timeout::TransportTimeout::with_outgoing_timeout(self, timeout)
}
fn inbound_timeout(self, timeout: Duration) -> timeout::TransportTimeout<Self>
where
Self: Sized,
{
timeout::TransportTimeout::with_ingoing_timeout(self, timeout)
}
}
#[derive(Clone, Debug)]
pub enum ListenerEvent<TOutput> {
AddressAdded(Multiaddr),
AddressDeleted(Multiaddr),
Accepted(TOutput),
}
impl<TOutput> ListenerEvent<TOutput> {
pub fn map<U>(self, f: impl FnOnce(TOutput) -> Result<U, TransportError>) -> Result<ListenerEvent<U>, TransportError> {
match self {
ListenerEvent::Accepted(o) => f(o).map(ListenerEvent::Accepted),
ListenerEvent::AddressAdded(a) => Ok(ListenerEvent::AddressAdded(a)),
ListenerEvent::AddressDeleted(a) => Ok(ListenerEvent::AddressDeleted(a)),
}
}
pub fn is_address_added(&self) -> bool {
matches!(self, ListenerEvent::AddressAdded(_))
}
pub fn into_new_address(self) -> Option<Multiaddr> {
if let ListenerEvent::AddressAdded(a) = self {
Some(a)
} else {
None
}
}
pub fn is_address_deleted(&self) -> bool {
matches!(self, ListenerEvent::AddressDeleted(_))
}
pub fn into_address_deleted(self) -> Option<Multiaddr> {
if let ListenerEvent::AddressDeleted(a) = self {
Some(a)
} else {
None
}
}
}
#[async_trait]
pub trait TransportListener: Send {
type Output: Send;
async fn accept(&mut self) -> Result<ListenerEvent<Self::Output>, TransportError>;
fn multi_addr(&self) -> Option<&Multiaddr>;
fn incoming(&mut self) -> Incoming<Self>
where
Self: Sized,
{
Incoming(self)
}
async fn accept_output(&mut self) -> Result<Self::Output, TransportError> {
loop {
if let ListenerEvent::Accepted(o) = self.accept().await? {
break Ok(o);
}
}
}
}
pub type IListener<TOutput> = Box<dyn TransportListener<Output = TOutput> + Send>;
pub type ITransport<TOutput> = Box<dyn Transport<Output = TOutput> + Send>;
impl<TOutput: ConnectionInfo> Clone for ITransport<TOutput> {
fn clone(&self) -> Self {
self.box_clone()
}
}
pub struct Incoming<'a, T>(&'a mut T);
impl<'a, T> Stream for Incoming<'a, T>
where
T: TransportListener,
{
type Item = Result<ListenerEvent<T::Output>, TransportError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let future = self.0.accept();
futures::pin_mut!(future);
let evt = futures::ready!(future.poll(cx))?;
Poll::Ready(Some(Ok(evt)))
}
}
pub trait ConnectionInfo: Send {
fn local_multiaddr(&self) -> Multiaddr;
fn remote_multiaddr(&self) -> Multiaddr;
}
#[derive(Debug)]
pub enum TransportError {
MultiaddrNotSupported(Multiaddr),
Timeout,
Unreachable,
Routing(String),
Internal,
IoError(std::io::Error),
ResolveFail(String),
NegotiationError(NegotiationError),
ProtectorError(PnetError),
SecurityError(Box<dyn Error + Send + Sync>),
StreamMuxerError(Box<dyn Error + Send + Sync>),
WsError(Box<dyn Error + Send + Sync>),
}
impl From<std::io::Error> for TransportError {
fn from(e: std::io::Error) -> Self {
TransportError::IoError(e)
}
}
impl From<NegotiationError> for TransportError {
fn from(e: NegotiationError) -> Self {
TransportError::NegotiationError(e)
}
}
impl From<PnetError> for TransportError {
fn from(e: PnetError) -> Self {
TransportError::ProtectorError(e)
}
}
impl fmt::Display for TransportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TransportError::MultiaddrNotSupported(addr) => write!(f, "Multiaddr is not supported: {}", addr),
TransportError::Timeout => write!(f, "Operation timeout"),
TransportError::Routing(s) => write!(f, "Routing not available {}", s),
TransportError::Unreachable => write!(f, "Memory transport unreachable"),
TransportError::Internal => write!(f, "Internal error"),
TransportError::IoError(err) => write!(f, "IO error {}", err),
TransportError::ResolveFail(name) => write!(f, "resolve dns {} failed", name),
TransportError::NegotiationError(err) => write!(f, "Negotiation error {:?}", err),
TransportError::ProtectorError(err) => write!(f, "Protector error {:?}", err),
TransportError::SecurityError(err) => write!(f, "SecurityError layer error {:?}", err),
TransportError::StreamMuxerError(err) => write!(f, "StreamMuxerError layer error {:?}", err),
TransportError::WsError(err) => write!(f, "Websocket transport error: {}", err),
}
}
}
impl Error for TransportError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
TransportError::MultiaddrNotSupported(_) => None,
TransportError::Timeout => None,
TransportError::Unreachable => None,
TransportError::Routing(_) => None,
TransportError::Internal => None,
TransportError::IoError(err) => Some(err),
TransportError::ResolveFail(_) => None,
TransportError::NegotiationError(err) => Some(err),
TransportError::ProtectorError(err) => Some(err),
TransportError::SecurityError(err) => Some(&**err),
TransportError::StreamMuxerError(err) => Some(&**err),
TransportError::WsError(err) => Some(&**err),
}
}
}