forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.rs
More file actions
72 lines (60 loc) · 2.44 KB
/
processor.rs
File metadata and controls
72 lines (60 loc) · 2.44 KB
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
use std::io::Cursor;
use std::net::SocketAddr;
use std::sync::Arc;
use aquatic_udp_protocol::Response;
use tracing::{instrument, Level};
use super::bound_socket::BoundSocket;
use crate::core::Tracker;
use crate::servers::udp::{handlers, RawRequest};
pub struct Processor {
socket: Arc<BoundSocket>,
tracker: Arc<Tracker>,
}
impl Processor {
pub fn new(socket: Arc<BoundSocket>, tracker: Arc<Tracker>) -> Self {
Self { socket, tracker }
}
#[instrument(skip(self, request))]
pub async fn process_request(self, request: RawRequest) {
let from = request.from;
let response = handlers::handle_packet(request, &self.tracker, self.socket.address()).await;
self.send_response(from, response).await;
}
#[instrument(skip(self))]
async fn send_response(self, target: SocketAddr, response: Response) {
tracing::debug!("send response");
let response_type = match &response {
Response::Connect(_) => "Connect".to_string(),
Response::AnnounceIpv4(_) => "AnnounceIpv4".to_string(),
Response::AnnounceIpv6(_) => "AnnounceIpv6".to_string(),
Response::Scrape(_) => "Scrape".to_string(),
Response::Error(e) => format!("Error: {e:?}"),
};
let mut writer = Cursor::new(Vec::with_capacity(200));
match response.write_bytes(&mut writer) {
Ok(()) => {
let bytes_count = writer.get_ref().len();
let payload = writer.get_ref();
let () = match self.send_packet(&target, payload).await {
Ok(sent_bytes) => {
if tracing::event_enabled!(Level::TRACE) {
tracing::debug!(%bytes_count, %sent_bytes, ?payload, "sent {response_type}");
} else {
tracing::debug!(%bytes_count, %sent_bytes, "sent {response_type}");
}
}
Err(error) => tracing::warn!(%bytes_count, %error, ?payload, "failed to send"),
};
}
Err(e) => {
tracing::error!(%e, "error");
}
}
}
#[instrument(skip(self))]
async fn send_packet(&self, target: &SocketAddr, payload: &[u8]) -> std::io::Result<usize> {
tracing::trace!("send packet");
// doesn't matter if it reaches or not
self.socket.send_to(payload, target).await
}
}