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
use crate::*;
enum Cmd {
Recv(Vec<u8>),
AwaitPermit {
await_registered: tokio::sync::oneshot::Sender<()>,
got_permit: tokio::sync::oneshot::Sender<()>,
},
RemotePermit(tokio::sync::OwnedSemaphorePermit, u32),
Close,
}
/// Receive a framed message on the connection.
pub struct FramedConnRecv(tokio::sync::mpsc::Receiver<Vec<u8>>);
impl FramedConnRecv {
/// Receive a framed message on the connection.
pub async fn recv(&mut self) -> Option<Vec<u8>> {
self.0.recv().await
}
}
/// A framed wrapper that can send and receive larger messages than
/// the base connection.
///
/// If a message is under the frame limit, it is just sent.
///
/// If a message is OVER the frame limit, we instead request a permit
/// to send the frame count to the remote peer. We only begin sending frames
/// once we receive the permit to do so.
///
/// This allows individual peers to throttle the amount of pending-completion
/// message memory they are allocating by only issuing permits up to a
/// configured memory threshold.
pub struct FramedConn {
pub_key: PubKey,
weak_conn: Weak<Conn>,
conn: tokio::sync::Mutex<Arc<Conn>>,
cmd_send: tokio::sync::mpsc::Sender<Cmd>,
recv_task: tokio::task::JoinHandle<()>,
cmd_task: tokio::task::JoinHandle<()>,
}
impl Drop for FramedConn {
fn drop(&mut self) {
self.recv_task.abort();
self.cmd_task.abort();
}
}
impl FramedConn {
/// Construct a new framed wrapper around the base connection.
pub async fn new(
conn: Arc<Conn>,
mut conn_recv: ConnRecv,
recv_limit: Arc<tokio::sync::Semaphore>,
) -> Result<(Self, FramedConnRecv)> {
conn.ready().await;
// send the protocol header
let (a, b, c, d) = crate::proto::PROTO_VER_2.encode()?;
conn.send(vec![a, b, c, d]).await?;
let (cmd_send, mut cmd_recv) = tokio::sync::mpsc::channel(32);
let (msg_send, msg_recv) = tokio::sync::mpsc::channel(32);
// set up the receive to just feed straight into the cmd task
let cmd_send2 = cmd_send.clone();
let recv_task = tokio::task::spawn(async move {
while let Some(msg) = conn_recv.recv().await {
if cmd_send2.send(Cmd::Recv(msg)).await.is_err() {
break;
}
}
let _ = cmd_send2.send(Cmd::Close).await;
});
let pub_key = conn.pub_key().clone();
// set up the cmd task.
// this is the main event loop of the framed wrapper
let pub_key2 = pub_key.clone();
let cmd_send2 = cmd_send.clone();
let weak_conn = Arc::downgrade(&conn);
let cmd_task = tokio::task::spawn(async move {
// init the stateful protocol decoder
let mut dec = crate::proto::ProtoDecoder::default();
while let Some(cmd) = cmd_recv.recv().await {
match cmd {
Cmd::Recv(msg) => {
use crate::proto::ProtoDecodeResult::*;
match dec.decode(&msg) {
Err(_) => break,
Ok(Idle) => (),
Ok(Message(msg)) => {
// received a message, forward to receiver
tracing::trace!(
target: "NETAUDIT",
pub_key = ?pub_key2,
byte_count = msg.len(),
m = "tx5-connection",
a = "recv_framed",
);
if msg_send.send(msg).await.is_err() {
tracing::info!("FramedConnRecv closed, stopping cmd task");
break;
}
}
Ok(RemotePermitRequest(permit_len)) => {
// receive a permit request,
// await the semaphore outside this loop,
// the semaphore permits will be issued
// in the order the request come in
let recv_limit = recv_limit.clone();
let cmd_send = cmd_send2.clone();
// fire and forget
tokio::task::spawn(async move {
if let Ok(permit) = recv_limit
.acquire_many_owned(permit_len)
.await
{
let _ = cmd_send
.send(Cmd::RemotePermit(
permit, permit_len,
))
.await;
}
});
}
Ok(RemotePermitGrant(_)) => (),
}
}
Cmd::AwaitPermit {
await_registered,
got_permit,
} => {
// register a oneshot to be triggered when a
// permit request is responded to
// we register the oneshot request with the
// stateful decoder
if dec
.sent_remote_permit_request(Some(got_permit))
.is_err()
{
break;
}
// we also notify the caller that we registered it
let _ = await_registered.send(());
}
Cmd::RemotePermit(permit, permit_len) => {
// our semaphore has granted a permit for the
// remote peer to begin sending us data
// now we need to notify them of that fact
// our stateful decoder also needs to know about it
if dec.sent_remote_permit_grant(permit).is_err() {
break;
}
// now notify our peer
if let Some(conn) = weak_conn.upgrade() {
let (a, b, c, d) =
match crate::proto::ProtoHeader::PermitGrant(
permit_len,
)
.encode()
{
Ok(r) => r,
Err(_) => break,
};
if conn.send(vec![a, b, c, d]).await.is_err() {
break;
}
} else {
break;
}
}
Cmd::Close => break,
}
}
});
Ok((
Self {
pub_key,
weak_conn: Arc::downgrade(&conn),
conn: tokio::sync::Mutex::new(conn),
cmd_send,
recv_task,
cmd_task,
},
FramedConnRecv(msg_recv),
))
}
/// The pub key of the remote peer this is connected to.
pub fn pub_key(&self) -> &PubKey {
&self.pub_key
}
/// Returns `true` if we successfully connected over webrtc.
pub fn is_using_webrtc(&self) -> bool {
if let Some(conn) = self.weak_conn.upgrade() {
conn.is_using_webrtc()
} else {
false
}
}
/// Get connection statistics.
pub fn get_stats(&self) -> ConnStats {
if let Some(conn) = self.weak_conn.upgrade() {
conn.get_stats()
} else {
ConnStats::default()
}
}
/// Send a message on the connection.
pub async fn send(&self, msg: Vec<u8>) -> Result<()> {
let byte_count = msg.len();
match self.send_inner(msg).await {
Ok(_) => {
tracing::trace!(
target: "NETAUDIT",
pub_key = ?self.pub_key,
byte_count,
m = "tx5-connection",
a = "send_framed_success",
);
Ok(())
}
Err(err) => {
tracing::debug!(
target: "NETAUDIT",
pub_key = ?self.pub_key,
byte_count,
?err,
m = "tx5-connection",
a = "send_framed_error",
);
Err(err)
}
}
}
/// Helper to do the sending, breaking up the messages if needed and
/// awaiting the permit before sending broken up messages.
async fn send_inner(&self, msg: Vec<u8>) -> Result<()> {
let conn = self.conn.lock().await;
match crate::proto::proto_encode(&msg)? {
crate::proto::ProtoEncodeResult::OneMessage(msg) => {
// it's a small message, just send it as one chunk
conn.send(msg).await?;
}
crate::proto::ProtoEncodeResult::NeedPermit {
permit_req,
msg_payload,
} => {
// it's a big message, we've got chunks
let (s_reg, r_reg) = tokio::sync::oneshot::channel();
let (s_perm, r_perm) = tokio::sync::oneshot::channel();
// coordinate with the cmd task that we need a permit
self.cmd_send
.send(Cmd::AwaitPermit {
await_registered: s_reg,
got_permit: s_perm,
})
.await
.map_err(|_| Error::other("closed"))?;
// wait for the want permit to be registered
r_reg.await.map_err(|_| Error::other("closed"))?;
// send the permit request
conn.send(permit_req).await?;
// wait for the permit to be authorized by the peer
r_perm.await.map_err(|_| Error::other("closed"))?;
// send the chunked messages
for msg in msg_payload {
conn.send(msg).await?;
}
}
}
Ok(())
}
}