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
use std::{
any::Any,
cmp::Ordering,
error::Error,
fmt,
fmt::{Debug, Display},
hash::{Hash, Hasher},
io::Result,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use aggligator::{control::Direction, id::ConnId, Control, IoRxBox, IoTxBox, Link, Listener, Server, Task};
mod acceptor;
mod connector;
pub use acceptor::*;
pub use connector::*;
#[derive(Clone, Debug)]
pub struct LinkError<TAG> {
pub id: Option<ConnId>,
pub tag: TAG,
pub error: Arc<std::io::Error>,
}
impl<TAG> LinkError<TAG>
where
TAG: Clone,
{
pub fn outgoing(id: ConnId, tag: &TAG, error: std::io::Error) -> Self {
Self { id: Some(id), tag: tag.clone(), error: Arc::new(error) }
}
pub fn incoming(tag: &TAG, error: std::io::Error) -> Self {
Self { id: None, tag: tag.clone(), error: Arc::new(error) }
}
pub fn direction(&self) -> Direction {
if self.id.is_some() {
Direction::Outgoing
} else {
Direction::Incoming
}
}
}
impl<TAG> fmt::Display for LinkError<TAG>
where
TAG: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: {}", &self.tag, &self.error)
}
}
impl<TAG> Error for LinkError<TAG> where TAG: fmt::Display + fmt::Debug {}
pub trait LinkTag: Debug + Display + Send + Sync + 'static {
fn transport_name(&self) -> &str;
fn direction(&self) -> Direction;
fn user_data(&self) -> Vec<u8>;
fn as_any(&self) -> &dyn Any;
fn box_clone(&self) -> LinkTagBox;
fn dyn_cmp(&self, other: &dyn LinkTag) -> Ordering;
fn dyn_hash(&self, state: &mut dyn Hasher);
}
impl PartialEq for dyn LinkTag {
fn eq(&self, other: &Self) -> bool {
self.cmp(other).is_eq()
}
}
impl Eq for dyn LinkTag {}
impl PartialOrd for dyn LinkTag {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(self, other))
}
}
impl Ord for dyn LinkTag {
fn cmp(&self, other: &Self) -> Ordering {
let id = self.as_any().type_id();
let other_id = other.as_any().type_id();
self.transport_name()
.cmp(other.transport_name())
.then(id.cmp(&other_id).then_with(|| self.dyn_cmp(other)))
}
}
impl Hash for dyn LinkTag {
fn hash<H: Hasher>(&self, state: &mut H) {
let id = self.as_any().type_id();
id.hash(state);
self.dyn_hash(state);
}
}
pub type LinkTagBox = Box<dyn LinkTag>;
impl Clone for LinkTagBox {
fn clone(&self) -> Self {
self.box_clone()
}
}
pub struct IoBox {
pub read: ReadBox,
pub write: WriteBox,
}
impl IoBox {
pub fn new(
read: impl AsyncRead + Send + Sync + 'static, write: impl AsyncWrite + Send + Sync + 'static,
) -> Self {
Self { read: Box::pin(read), write: Box::pin(write) }
}
pub fn into_split(self) -> (ReadBox, WriteBox) {
let Self { read, write } = self;
(read, write)
}
}
impl AsyncRead for IoBox {
fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut ReadBuf) -> Poll<Result<()>> {
Pin::new(&mut self.get_mut().read).poll_read(cx, buf)
}
}
impl AsyncWrite for IoBox {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<Result<usize>> {
Pin::new(&mut self.get_mut().write).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>> {
Pin::new(&mut self.get_mut().write).poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>> {
Pin::new(&mut self.get_mut().write).poll_shutdown(cx)
}
}
type ReadBox = Pin<Box<dyn AsyncRead + Send + Sync + 'static>>;
type WriteBox = Pin<Box<dyn AsyncWrite + Send + Sync + 'static>>;
type BoxControl = Control<IoTxBox, IoRxBox, LinkTagBox>;
type BoxServer = Server<IoTxBox, IoRxBox, LinkTagBox>;
type BoxListener = Listener<IoTxBox, IoRxBox, LinkTagBox>;
type BoxTask = Task<IoTxBox, IoRxBox, LinkTagBox>;
type BoxLink = Link<LinkTagBox>;
type BoxLinkError = LinkError<LinkTagBox>;
#[cfg(feature = "tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
pub mod tls;
#[cfg(feature = "tcp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
pub mod tcp;
#[cfg(feature = "rfcomm")]
#[cfg_attr(docsrs, doc(cfg(feature = "rfcomm")))]
pub mod rfcomm;