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
use super::{Codec, DataStream, Transport};
use futures::stream::Stream;
use log::*;
use std::{future::Future, pin::Pin};
use tokio::{
io,
net::{TcpListener, TcpStream},
sync::mpsc,
task::JoinHandle,
};
pub struct TransportListener<T, U>
where
T: DataStream,
U: Codec,
{
listen_task: JoinHandle<()>,
accept_task: JoinHandle<()>,
rx: mpsc::Receiver<Transport<T, U>>,
}
impl<T, U> TransportListener<T, U>
where
T: DataStream + Send + 'static,
U: Codec + Send + 'static,
{
pub fn initialize<L, F>(listener: L, mut make_transport: F) -> Self
where
L: Listener<Output = T> + 'static,
F: FnMut(T) -> Transport<T, U> + Send + 'static,
{
let (stream_tx, mut stream_rx) = mpsc::channel::<T>(1);
let listen_task = tokio::spawn(async move {
loop {
match listener.accept().await {
Ok(stream) => {
if stream_tx.send(stream).await.is_err() {
error!("Listener failed to pass along stream");
break;
}
}
Err(x) => {
error!("Listener failed to accept stream: {}", x);
break;
}
}
}
});
let (tx, rx) = mpsc::channel::<Transport<T, U>>(1);
let accept_task = tokio::spawn(async move {
while let Some(stream) = stream_rx.recv().await {
let transport = make_transport(stream);
if let Err(x) = tx.send(transport).await {
error!("Failed to forward transport: {}", x);
}
}
});
Self {
listen_task,
accept_task,
rx,
}
}
pub fn abort(&self) {
self.listen_task.abort();
self.accept_task.abort();
}
pub async fn accept(&mut self) -> Option<Transport<T, U>> {
self.rx.recv().await
}
pub fn into_stream(self) -> impl Stream<Item = Transport<T, U>> {
futures::stream::unfold(self, |mut _self| async move {
_self
.accept()
.await
.map(move |transport| (transport, _self))
})
}
}
pub type AcceptFuture<'a, T> = Pin<Box<dyn Future<Output = io::Result<T>> + Send + 'a>>;
pub trait Listener: Send + Sync {
type Output;
fn accept<'a>(&'a self) -> AcceptFuture<'a, Self::Output>
where
Self: Sync + 'a;
}
impl Listener for TcpListener {
type Output = TcpStream;
fn accept<'a>(&'a self) -> AcceptFuture<'a, Self::Output>
where
Self: Sync + 'a,
{
async fn accept(_self: &TcpListener) -> io::Result<TcpStream> {
_self.accept().await.map(|(stream, _)| stream)
}
Box::pin(accept(self))
}
}
#[cfg(unix)]
impl Listener for tokio::net::UnixListener {
type Output = tokio::net::UnixStream;
fn accept<'a>(&'a self) -> AcceptFuture<'a, Self::Output>
where
Self: Sync + 'a,
{
async fn accept(_self: &tokio::net::UnixListener) -> io::Result<tokio::net::UnixStream> {
_self.accept().await.map(|(stream, _)| stream)
}
Box::pin(accept(self))
}
}
#[cfg(test)]
impl<T> Listener for tokio::sync::Mutex<tokio::sync::mpsc::Receiver<T>>
where
T: DataStream + Send + Sync + 'static,
{
type Output = T;
fn accept<'a>(&'a self) -> AcceptFuture<'a, Self::Output>
where
Self: Sync + 'a,
{
async fn accept<T>(
_self: &tokio::sync::Mutex<tokio::sync::mpsc::Receiver<T>>,
) -> io::Result<T>
where
T: DataStream + Send + Sync + 'static,
{
_self
.lock()
.await
.recv()
.await
.ok_or_else(|| io::Error::from(io::ErrorKind::BrokenPipe))
}
Box::pin(accept(self))
}
}