Report size to trackers

This commit is contained in:
Igor Katson 2024-03-01 07:54:27 +00:00
parent 5488e1d40f
commit a6ebecee97
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
8 changed files with 183 additions and 42 deletions

View file

@ -28,11 +28,21 @@ pub struct TrackerComms {
tcp_listen_port: Option<u16>,
}
#[derive(Default)]
pub enum TrackerCommsStatsState {
#[default]
None,
Initializing,
Paused,
Live,
}
#[derive(Default)]
pub struct TrackerCommsStats {
pub uploaded_bytes: u64,
pub downloaded_bytes: u64,
pub total_bytes: u64,
pub torrent_state: TrackerCommsStatsState,
}
impl TrackerCommsStats {
@ -44,6 +54,10 @@ impl TrackerCommsStats {
}
0
}
pub fn is_completed(&self) -> bool {
self.downloaded_bytes >= self.total_bytes
}
}
pub trait TorrentStatsProvider: Send + Sync {
@ -164,7 +178,7 @@ impl TrackerComms {
let request = tracker_comms_http::TrackerRequest {
info_hash: self.info_hash,
peer_id: self.peer_id,
port: 6778,
port: self.tcp_listen_port.unwrap_or(0),
uploaded: stats.uploaded_bytes,
downloaded: stats.downloaded_bytes,
left: stats.get_left_to_download_bytes(),
@ -249,7 +263,18 @@ impl TrackerComms {
downloaded: stats.downloaded_bytes,
left: stats.get_left_to_download_bytes(),
uploaded: stats.uploaded_bytes,
event: EVENT_NONE,
event: match stats.torrent_state {
TrackerCommsStatsState::None => EVENT_NONE,
TrackerCommsStatsState::Initializing => EVENT_STARTED,
TrackerCommsStatsState::Paused => EVENT_STOPPED,
TrackerCommsStatsState::Live => {
if stats.is_completed() {
EVENT_COMPLETED
} else {
EVENT_STARTED
}
}
},
key: 0, // whatever that is?
port: self.tcp_listen_port.unwrap_or(0),
};

View file

@ -12,9 +12,9 @@ const ACTION_ANNOUNCE: u32 = 1;
// const ACTION_ERROR: u32 = 3;
pub const EVENT_NONE: u32 = 0;
// pub const EVENT_COMPLETED: u32 = 1;
// pub const EVENT_STARTED: u32 = 2;
// pub const EVENT_STOPPED: u32 = 3;
pub const EVENT_COMPLETED: u32 = 1;
pub const EVENT_STARTED: u32 = 2;
pub const EVENT_STOPPED: u32 = 3;
pub type ConnectionId = u64;
const CONNECTION_ID_MAGIC: ConnectionId = 0x41727101980;