Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.

Commit ee736f6

Browse files
committed
Limit maximum timeouts while receiving a packet.
1 parent e808c1d commit ee736f6

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/socket.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const INITIAL_CONGESTION_TIMEOUT: u64 = 1000; // one second
2020
const MIN_CONGESTION_TIMEOUT: u64 = 500; // 500 ms
2121
const MAX_CONGESTION_TIMEOUT: u64 = 60_000; // one minute
2222
const BASE_HISTORY: usize = 10; // base delays history size
23-
const MAX_CONNECT_TRIES: u16 = 5;
23+
const MAX_TIMEOUTS: u16 = 5;
2424

2525
#[derive(Debug)]
2626
pub enum SocketError {
@@ -255,7 +255,7 @@ impl UtpSocket {
255255
Ok((read, src)) => { socket.connected_to = src; len = read; break; },
256256
Err(ref e) if (e.kind() == ErrorKind::WouldBlock ||
257257
e.kind() == ErrorKind::TimedOut) => {
258-
if try == MAX_CONNECT_TRIES {
258+
if try == MAX_TIMEOUTS {
259259
return Err(Error::from(SocketError::ConnectionTimedOut));
260260
}
261261
debug!("Timed out, retrying");
@@ -327,6 +327,7 @@ impl UtpSocket {
327327
return Err(Error::from(SocketError::ConnectionReset));
328328
}
329329

330+
let mut timeouts = 0;
330331
loop {
331332
// A closed socket with no pending data can only "read" 0 new bytes.
332333
if self.state == SocketState::Closed {
@@ -336,6 +337,12 @@ impl UtpSocket {
336337
match self.recv(buf) {
337338
Ok((0, _src)) => continue,
338339
Ok(x) => return Ok(x),
340+
Err(ref e) if e.kind() == ErrorKind::TimedOut => {
341+
timeouts += 1;
342+
if timeouts == MAX_TIMEOUTS {
343+
return Err(Error::from(SocketError::ConnectionTimedOut));
344+
}
345+
}
339346
Err(e) => return Err(e)
340347
}
341348
}
@@ -355,7 +362,7 @@ impl UtpSocket {
355362
self.congestion_timeout = self.congestion_timeout * 2;
356363
self.cwnd = MSS;
357364
self.send_fast_resend_request();
358-
return Ok((0, self.connected_to));
365+
return Err(Error::from(SocketError::ConnectionTimedOut));
359366
},
360367
Ok(x) => x,
361368
Err(e) => return Err(e),

0 commit comments

Comments
 (0)