Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: [#682] handle UDP Tracker timeouts #827

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/console/clients/udp/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum ClientError {
NotConnected,
#[error("Unexpected response while connecting the the remote server.")]
UnexpectedConnectionResponse,
#[error("Connection timeout.")]
ConnectionTimeout,
}

/// A UDP Tracker client to make test requests (checks).
Expand Down
11 changes: 6 additions & 5 deletions src/shared/bit_torrent/tracker/udp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
use std::sync::Arc;
use std::time::Duration;

use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use aquatic_udp_protocol::{ConnectRequest, Request, Response, TransactionId};
use log::debug;
use tokio::net::UdpSocket;
use tokio::time;
use zerocopy::network_endian::I32;

use crate::console::clients::udp::checker::ClientError;
use crate::shared::bit_torrent::tracker::udp::{source_address, MAX_PACKET_SIZE};

/// Default timeout for sending and receiving packets. And waiting for sockets
Expand Down Expand Up @@ -79,15 +80,15 @@
Err(e) => return Err(anyhow!("IO error waiting for the socket to become readable: {e:?}")),
};
}
Err(e) => return Err(anyhow!("Timeout waiting for the socket to become readable: {e:?}")),
Err(_) => bail!(ClientError::ConnectionTimeout),

Check warning on line 83 in src/shared/bit_torrent/tracker/udp/client.rs

View check run for this annotation

Codecov / codecov/patch

src/shared/bit_torrent/tracker/udp/client.rs#L83

Added line #L83 was not covered by tests
};

match time::timeout(self.timeout, self.socket.send(bytes)).await {
Ok(send_result) => match send_result {
Ok(size) => Ok(size),
Err(e) => Err(anyhow!("IO error during send: {e:?}")),
},
Err(e) => Err(anyhow!("Send operation timed out: {e:?}")),
Err(_) => bail!(ClientError::ConnectionTimeout),

Check warning on line 91 in src/shared/bit_torrent/tracker/udp/client.rs

View check run for this annotation

Codecov / codecov/patch

src/shared/bit_torrent/tracker/udp/client.rs#L91

Added line #L91 was not covered by tests
}
}

Expand All @@ -110,15 +111,15 @@
Err(e) => return Err(anyhow!("IO error waiting for the socket to become readable: {e:?}")),
};
}
Err(e) => return Err(anyhow!("Timeout waiting for the socket to become readable: {e:?}")),
Err(_) => bail!(ClientError::ConnectionTimeout),

Check warning on line 114 in src/shared/bit_torrent/tracker/udp/client.rs

View check run for this annotation

Codecov / codecov/patch

src/shared/bit_torrent/tracker/udp/client.rs#L114

Added line #L114 was not covered by tests
};

let size_result = match time::timeout(self.timeout, self.socket.recv(bytes)).await {
Ok(recv_result) => match recv_result {
Ok(size) => Ok(size),
Err(e) => Err(anyhow!("IO error during send: {e:?}")),
},
Err(e) => Err(anyhow!("Receive operation timed out: {e:?}")),
Err(_) => bail!(ClientError::ConnectionTimeout),

Check warning on line 122 in src/shared/bit_torrent/tracker/udp/client.rs

View check run for this annotation

Codecov / codecov/patch

src/shared/bit_torrent/tracker/udp/client.rs#L122

Added line #L122 was not covered by tests
};

if size_result.is_ok() {
Expand Down
Loading