Skip to content

Commit

Permalink
dev: use stream for udp requests
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Jun 19, 2024
1 parent 27933b9 commit 7df1201
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 199 deletions.
1 change: 0 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ rustflags = [
"-D",
"unused",
]

2 changes: 2 additions & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
"codecov",
"codegen",
"completei",
"Condvar",
"connectionless",
"Containerfile",
"conv",
"curr",
"cvar",
"Cyberneering",
"dashmap",
"datagram",
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ pub mod bootstrap;
pub mod console;
pub mod core;
pub mod servers;
pub mod shared;
pub mod shared;

#[macro_use]
extern crate lazy_static;
Expand Down
8 changes: 3 additions & 5 deletions src/servers/udp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use aquatic_udp_protocol::{
ErrorResponse, Ipv4AddrBytes, Ipv6AddrBytes, NumberOfDownloads, NumberOfPeers, Port, Request, Response, ResponsePeer,
ScrapeRequest, ScrapeResponse, TorrentScrapeStatistics, TransactionId,
};
use tokio::net::UdpSocket;
use torrust_tracker_located_error::DynError;
use torrust_tracker_primitives::info_hash::InfoHash;
use tracing::debug;
Expand All @@ -34,13 +33,12 @@ use crate::shared::bit_torrent::common::MAX_SCRAPE_TORRENTS;
/// - Delegating the request to the correct handler depending on the request type.
///
/// It will return an `Error` response if the request is invalid.
pub(crate) async fn handle_packet(udp_request: UdpRequest, tracker: &Arc<Tracker>, socket: Arc<UdpSocket>) -> Response {
pub(crate) async fn handle_packet(udp_request: UdpRequest, tracker: &Arc<Tracker>, addr: SocketAddr) -> Response {
debug!("Handling Packets: {udp_request:?}");

let start_time = Instant::now();

let request_id = RequestId::make(&udp_request);
let server_socket_addr = socket.local_addr().expect("Could not get local_addr for socket.");

match Request::parse_bytes(&udp_request.payload[..udp_request.payload.len()], MAX_SCRAPE_TORRENTS).map_err(|e| {
Error::InternalServer {
Expand All @@ -49,7 +47,7 @@ pub(crate) async fn handle_packet(udp_request: UdpRequest, tracker: &Arc<Tracker
}
}) {
Ok(request) => {
log_request(&request, &request_id, &server_socket_addr);
log_request(&request, &request_id, &addr);

let transaction_id = match &request {
Request::Connect(connect_request) => connect_request.transaction_id,
Expand All @@ -64,7 +62,7 @@ pub(crate) async fn handle_packet(udp_request: UdpRequest, tracker: &Arc<Tracker

let latency = start_time.elapsed();

log_response(&response, &transaction_id, &request_id, &server_socket_addr, latency);
log_response(&response, &transaction_id, &request_id, &addr, latency);

response
}
Expand Down
Loading

0 comments on commit 7df1201

Please sign in to comment.