forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannounce.rs
48 lines (42 loc) · 1.64 KB
/
announce.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
//! The `announce` service.
//!
//! The service is responsible for handling the `announce` requests.
//!
//! It delegates the `announce` logic to the [`AnnounceHandler`] and it returns
//! the [`AnnounceData`].
//!
//! It also sends an [`udp_tracker_core::statistics::event::Event`]
//! because events are specific for the HTTP tracker.
use std::net::IpAddr;
use std::sync::Arc;
use bittorrent_primitives::info_hash::InfoHash;
use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted};
use torrust_tracker_primitives::core::AnnounceData;
use torrust_tracker_primitives::peer;
use crate::packages::udp_tracker_core;
pub async fn invoke(
announce_handler: Arc<AnnounceHandler>,
opt_udp_stats_event_sender: Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>,
info_hash: InfoHash,
peer: &mut peer::Peer,
peers_wanted: &PeersWanted,
) -> AnnounceData {
let original_peer_ip = peer.peer_addr.ip();
// The tracker could change the original peer ip
let announce_data = announce_handler.announce(&info_hash, peer, &original_peer_ip, peers_wanted);
if let Some(udp_stats_event_sender) = opt_udp_stats_event_sender.as_deref() {
match original_peer_ip {
IpAddr::V4(_) => {
udp_stats_event_sender
.send_event(udp_tracker_core::statistics::event::Event::Udp4Announce)
.await;
}
IpAddr::V6(_) => {
udp_stats_event_sender
.send_event(udp_tracker_core::statistics::event::Event::Udp6Announce)
.await;
}
}
}
announce_data
}