@@ -11,6 +11,7 @@ use bittorrent_primitives::info_hash::InfoHash;
11
11
use bittorrent_tracker_core:: announce_handler:: AnnounceHandler ;
12
12
use bittorrent_tracker_core:: whitelist;
13
13
use torrust_tracker_configuration:: Core ;
14
+ use torrust_tracker_primitives:: core:: AnnounceData ;
14
15
use tracing:: { instrument, Level } ;
15
16
use zerocopy:: network_endian:: I32 ;
16
17
@@ -41,7 +42,7 @@ pub async fn handle_announce(
41
42
42
43
tracing:: trace!( "handle announce" ) ;
43
44
44
- let response = udp_tracker_core:: services:: announce:: handle_announce (
45
+ let announce_data = udp_tracker_core:: services:: announce:: handle_announce (
45
46
remote_addr,
46
47
request,
47
48
announce_handler,
@@ -52,18 +53,25 @@ pub async fn handle_announce(
52
53
. await
53
54
. map_err ( |e| ( e. into ( ) , request. transaction_id ) ) ?;
54
55
55
- // todo: extract `build_response` function.
56
+ Ok ( build_response ( remote_addr, request, core_config, & announce_data) )
57
+ }
56
58
59
+ fn build_response (
60
+ remote_addr : SocketAddr ,
61
+ request : & AnnounceRequest ,
62
+ core_config : & Arc < Core > ,
63
+ announce_data : & AnnounceData ,
64
+ ) -> Response {
57
65
#[ allow( clippy:: cast_possible_truncation) ]
58
66
if remote_addr. is_ipv4 ( ) {
59
67
let announce_response = AnnounceResponse {
60
68
fixed : AnnounceResponseFixedData {
61
69
transaction_id : request. transaction_id ,
62
70
announce_interval : AnnounceInterval ( I32 :: new ( i64:: from ( core_config. announce_policy . interval ) as i32 ) ) ,
63
- leechers : NumberOfPeers ( I32 :: new ( i64:: from ( response . stats . incomplete ) as i32 ) ) ,
64
- seeders : NumberOfPeers ( I32 :: new ( i64:: from ( response . stats . complete ) as i32 ) ) ,
71
+ leechers : NumberOfPeers ( I32 :: new ( i64:: from ( announce_data . stats . incomplete ) as i32 ) ) ,
72
+ seeders : NumberOfPeers ( I32 :: new ( i64:: from ( announce_data . stats . complete ) as i32 ) ) ,
65
73
} ,
66
- peers : response
74
+ peers : announce_data
67
75
. peers
68
76
. iter ( )
69
77
. filter_map ( |peer| {
@@ -79,16 +87,16 @@ pub async fn handle_announce(
79
87
. collect ( ) ,
80
88
} ;
81
89
82
- Ok ( Response :: from ( announce_response) )
90
+ Response :: from ( announce_response)
83
91
} else {
84
92
let announce_response = AnnounceResponse {
85
93
fixed : AnnounceResponseFixedData {
86
94
transaction_id : request. transaction_id ,
87
95
announce_interval : AnnounceInterval ( I32 :: new ( i64:: from ( core_config. announce_policy . interval ) as i32 ) ) ,
88
- leechers : NumberOfPeers ( I32 :: new ( i64:: from ( response . stats . incomplete ) as i32 ) ) ,
89
- seeders : NumberOfPeers ( I32 :: new ( i64:: from ( response . stats . complete ) as i32 ) ) ,
96
+ leechers : NumberOfPeers ( I32 :: new ( i64:: from ( announce_data . stats . incomplete ) as i32 ) ) ,
97
+ seeders : NumberOfPeers ( I32 :: new ( i64:: from ( announce_data . stats . complete ) as i32 ) ) ,
90
98
} ,
91
- peers : response
99
+ peers : announce_data
92
100
. peers
93
101
. iter ( )
94
102
. filter_map ( |peer| {
@@ -104,7 +112,7 @@ pub async fn handle_announce(
104
112
. collect ( ) ,
105
113
} ;
106
114
107
- Ok ( Response :: from ( announce_response) )
115
+ Response :: from ( announce_response)
108
116
}
109
117
}
110
118
0 commit comments