-
Notifications
You must be signed in to change notification settings - Fork 59
/
connection.rs
491 lines (405 loc) Β· 14.5 KB
/
connection.rs
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use super::{
negotiate_connection, SendEvent, UdpConnectionConfig, UdpConnectionOrchestrator,
UdpConnectionState, UdpConnectionVars,
};
use anyhow::{Error, Result};
use log::*;
use std::io;
use std::mem;
use std::net::IpAddr;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio::net::UdpSocket;
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};
/// This struct is the entry point into the reliable UDP connection API
pub struct UdpConnection {
state: State,
}
#[derive(Clone)]
pub struct UdpConnectParams {
pub ip: IpAddr,
pub suggested_port: u16,
pub master_side: bool,
}
#[allow(dead_code)]
struct Running {
con: Arc<Mutex<UdpConnectionVars>>,
orchestrator: UdpConnectionOrchestrator,
sender: UnboundedSender<SendEvent>,
}
enum State {
New(UdpConnectionConfig),
Binding,
Bound(UdpConnectionConfig, UdpSocket),
Connecting,
Running(Running),
Stopping,
Disconnecting(Running),
Disconnected,
}
impl UdpConnection {
/// Initialises a new UDP connection
pub fn new(config: UdpConnectionConfig) -> Self {
Self {
state: State::New(config),
}
}
/// Whether the connection has been activated
pub fn is_new(&self) -> bool {
match &self.state {
State::New(_) => true,
_ => false,
}
}
/// Whether the connection is bound
pub fn is_bound(&self) -> bool {
match &self.state {
State::Bound(_, _) => true,
_ => false,
}
}
/// Whether the connection is in a valid connected state.
pub fn is_connected(&self) -> bool {
let running = match &self.state {
State::Running(running) => running,
_ => return false,
};
let con = running.con.lock().unwrap();
con.state == UdpConnectionState::Connected
}
/// Whether the connection has been disconnected
#[allow(dead_code)]
pub fn is_disconnected(&self) -> bool {
let running = match &self.state {
State::Disconnected => return true,
State::Running(running) => running,
_ => return false,
};
let con = running.con.lock().unwrap();
return !con.is_connected();
}
/// Bind the connection to the UDP socket
pub async fn bind(&mut self) -> Result<u16> {
if !self.is_new() {
return Err(Error::msg("Connection must be in NEW state"));
}
match self.do_bind().await {
Ok(port) => Ok(port),
Err((config, err)) => {
debug!("UDP bind failed: {}", err);
self.state = State::New(config);
Err(err)
}
}
}
async fn do_bind(&mut self) -> Result<u16, (UdpConnectionConfig, Error)> {
assert!(self.is_new());
let config = match mem::replace(&mut self.state, State::Binding) {
State::New(config) => config,
_ => unreachable!(),
};
let socket = UdpSocket::bind(config.bind_addr())
.await
.map_err(|err| (config.clone(), Error::from(err)))?;
let port = socket
.local_addr()
.map_err(|err| (config.clone(), Error::from(err)))?
.port();
self.state = State::Bound(config, socket);
Ok(port)
}
/// Attempts to make a connection with the peer as specified in the UdpConnectParams.
/// This does not guarantee that the connection will be made on the suggested port.
pub async fn connect(&mut self, params: UdpConnectParams) -> Result<()> {
if !self.is_new() & !self.is_bound() {
return Err(Error::msg("Connection must be in NEW or BOUND state"));
}
if !self.is_bound() {
self.bind().await.map(|_| ())?
}
match self.do_connect(params).await {
Ok(_) => Ok(()),
Err((config, err)) => {
debug!("UDP connection failed: {}", err);
self.state = State::New(config);
Err(err)
}
}
}
async fn do_connect(
&mut self,
params: UdpConnectParams,
) -> Result<(), (UdpConnectionConfig, Error)> {
assert!(self.is_bound());
let (config, mut socket) = match mem::replace(&mut self.state, State::Connecting) {
State::Bound(config, socket) => (config, socket),
_ => unreachable!(),
};
let mut con = UdpConnectionVars::new(config);
let result = negotiate_connection(
&mut con,
&mut socket,
params.ip,
params.suggested_port,
params.master_side,
)
.await;
let (send_tx, send_rx) = unbounded_channel();
match result {
Ok(_) => con.set_state_connected(send_tx.clone()),
Err(err) => {
con.set_state_connect_failed();
return Err((con.config().clone(), err));
}
}
let con = Arc::new(Mutex::new(con));
let mut orchestrator = UdpConnectionOrchestrator::new(socket, Arc::clone(&con), send_rx);
orchestrator.start_orchestration_loop();
self.state = State::Running(Running {
con,
orchestrator,
sender: send_tx,
});
Ok(())
}
/// Closes the connection
#[allow(dead_code)]
pub async fn close(&mut self) -> Result<()> {
if !self.is_connected() {
return Err(Error::msg("Connection must be in CONNECTED state"));
}
self.shutdown().await.map_err(|err| Error::from(err))
}
}
impl AsyncRead for UdpConnection {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buff: &mut [u8],
) -> Poll<io::Result<usize>> {
if !self.is_connected() {
warn!("attempted to poll connection which is not connected");
return Poll::Ready(Err(io::Error::from(io::ErrorKind::NotConnected)));
}
let running = match &self.state {
State::Running(running) => running,
_ => unreachable!(),
};
let mut con = running.con.lock().unwrap();
if con.recv_available_bytes() == 0 {
con.recv_wakers.push(cx.waker().clone());
return Poll::Pending;
} else {
let data = con.recv_drain_bytes(buff.len());
buff[..data.len()].copy_from_slice(&data[..]);
return Poll::Ready(Ok(data.len()));
}
}
}
impl AsyncWrite for UdpConnection {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buff: &[u8],
) -> Poll<io::Result<usize>> {
assert!(buff.len() > 0);
if !self.is_connected() {
warn!("attempted to poll connection which is not connected");
return Poll::Ready(Err(io::Error::from(io::ErrorKind::NotConnected)));
}
let running = match &self.state {
State::Running(running) => running,
_ => unreachable!(),
};
// Create the packet to be sent
let packet = {
let mut con = running.con.lock().unwrap();
// We first check if the connection is currently congested
// and wait until the connection decongests if so
if con.is_congested() {
debug!(
"connection is congested, waiting until window grows before continuing sending"
);
con.wait_until_decongested(cx.waker().clone());
return Poll::Pending;
}
con.create_data_packet(buff)
};
let bytes_sent = packet.payload.len();
let result = running.sender.send(SendEvent::Send(packet));
match result {
Ok(_) => Poll::Ready(Ok(bytes_sent)),
Err(err) => {
warn!("failed to send event to orchestration sender: {}", err);
Poll::Ready(Err(io::Error::from(io::ErrorKind::BrokenPipe)))
}
}
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
// Flushing is handled by the orchestrator
Poll::Ready(Ok(()))
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
if let State::Disconnecting(running) = &self.state {
if !self.is_connected() {
self.state = State::Disconnected;
return Poll::Ready(Ok(()));
} else {
let mut con = running.con.lock().unwrap();
con.close_wakers.push(cx.waker().clone());
return Poll::Pending;
}
}
let running = match mem::replace(&mut self.state, State::Stopping) {
State::Running(running) => running,
_ => unreachable!(),
};
let result = running.sender.send(SendEvent::Close);
match result {
Ok(_) => {}
Err(err) => {
warn!("failed to send event to orchestration sender: {}", err);
return Poll::Ready(Err(io::Error::from(io::ErrorKind::BrokenPipe)));
}
};
{
let mut con = running.con.lock().unwrap();
con.close_wakers.push(cx.waker().clone());
}
self.state = State::Disconnecting(running);
return Poll::Pending;
}
}
#[cfg(test)]
mod tests {
use super::*;
use lazy_static::lazy_static;
use std::net::SocketAddr;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::runtime::Runtime;
use tokio::time::delay_for;
lazy_static! {
static ref UDP_PORT_NUMBER: Mutex<u16> = Mutex::from(27660);
}
fn init_udp_port_number_pairs() -> (u16, u16) {
let mut port = UDP_PORT_NUMBER.lock().unwrap();
*port += 2;
(*port - 2, *port - 1)
}
async fn init_connection_pair() -> (UdpConnection, UdpConnection) {
let (port1, port2) = init_udp_port_number_pairs();
let config1 = UdpConnectionConfig::default()
.with_connect_timeout(Duration::from_millis(500))
.with_bind_addr(SocketAddr::from(([0, 0, 0, 0], port1)));
let config2 = UdpConnectionConfig::default()
.with_connect_timeout(Duration::from_millis(500))
.with_bind_addr(SocketAddr::from(([0, 0, 0, 0], port2)));
let mut connection1 = UdpConnection::new(config1);
let mut connection2 = UdpConnection::new(config2);
let (result1, result2) = tokio::join!(
connection1.connect(UdpConnectParams {
ip: "127.0.0.1".parse().unwrap(),
suggested_port: port2,
master_side: true
}),
connection2.connect(UdpConnectParams {
ip: "127.0.0.1".parse().unwrap(),
suggested_port: port1,
master_side: false
})
);
result1.unwrap();
result2.unwrap();
(connection1, connection2)
}
#[test]
fn test_new_connection() {
let connection = UdpConnection::new(UdpConnectionConfig::default());
assert_eq!(connection.is_new(), true);
assert_eq!(connection.is_bound(), false);
assert_eq!(connection.is_connected(), false);
assert_eq!(connection.is_disconnected(), false);
}
#[test]
fn test_bind() {
Runtime::new().unwrap().block_on(async {
let config = UdpConnectionConfig::default();
let mut connection = UdpConnection::new(config);
let result = connection.bind().await;
result.unwrap();
assert_eq!(connection.is_new(), false);
assert_eq!(connection.is_bound(), true);
assert_eq!(connection.is_connected(), false);
assert_eq!(connection.is_disconnected(), false);
});
}
#[test]
fn test_connect_without_peer_should_fail() {
Runtime::new().unwrap().block_on(async {
let config =
UdpConnectionConfig::default().with_connect_timeout(Duration::from_millis(50));
let mut connection = UdpConnection::new(config);
let result = connection
.connect(UdpConnectParams {
ip: "127.0.0.1".parse().unwrap(),
suggested_port: 1234,
master_side: true,
})
.await;
assert_eq!(result.is_err(), true);
assert_eq!(connection.is_new(), true);
assert_eq!(connection.is_bound(), false);
assert_eq!(connection.is_connected(), false);
assert_eq!(connection.is_disconnected(), false);
});
}
#[test]
fn test_connect_with_peer() {
Runtime::new().unwrap().block_on(async {
init_connection_pair().await;
});
}
#[test]
fn test_connect_write_then_read() {
Runtime::new().unwrap().block_on(async {
let (mut con1, mut con2) = init_connection_pair().await;
con1.write(&[1u8, 2, 3, 4, 5]).await.unwrap();
let mut buff = [0u8; 1024];
let read = con2.read(&mut buff).await.unwrap();
assert_eq!(&buff[..read], [1u8, 2, 3, 4, 5]);
con2.write(&[6u8, 7, 8, 9, 10]).await.unwrap();
let mut buff = [0u8; 1024];
let read = con1.read(&mut buff).await.unwrap();
assert_eq!(&buff[..read], [6u8, 7, 8, 9, 10]);
});
}
#[test]
fn test_connect_write_then_close_one_side() {
// TODO: fix flaky test
if std::env::var("CI").is_ok() {
return;
}
Runtime::new().unwrap().block_on(async {
let (mut con1, mut con2) = init_connection_pair().await;
con1.write(&[1u8, 2, 3, 4, 5]).await.unwrap();
con2.close().await.unwrap();
assert_eq!(con2.is_new(), false);
assert_eq!(con2.is_connected(), false);
assert_eq!(con2.is_disconnected(), true);
// Wait for close packet to be sent and process
delay_for(Duration::from_millis(500)).await;
assert_eq!(con1.is_new(), false);
assert_eq!(con1.is_connected(), false);
assert_eq!(con1.is_disconnected(), true);
// Write / recv should fail
let result = con1.write(&[1u8, 2, 3, 4, 5]).await;
assert_eq!(result.is_err(), true);
let mut buff = [0u8; 1024];
let result = con1.write(&mut buff).await;
assert_eq!(result.is_err(), true);
});
}
}