mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 02:35:31 +00:00
aquatic_http protocol: refactor into more submodules, other fixes
This commit is contained in:
parent
52cc7d8acb
commit
7419c51434
9 changed files with 303 additions and 290 deletions
85
aquatic_http/src/lib/protocol/response.rs
Normal file
85
aquatic_http/src/lib/protocol/response.rs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
use std::net::IpAddr;
|
||||
|
||||
use hashbrown::HashMap;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::common::Peer;
|
||||
|
||||
use super::common::*;
|
||||
use super::utils::*;
|
||||
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize)]
|
||||
pub struct ResponsePeer {
|
||||
pub ip_address: IpAddr,
|
||||
pub port: u16
|
||||
}
|
||||
|
||||
|
||||
impl ResponsePeer {
|
||||
pub fn from_peer(peer: &Peer) -> Self {
|
||||
let ip_address = peer.connection_meta.peer_addr.ip();
|
||||
|
||||
Self {
|
||||
ip_address,
|
||||
port: peer.port
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ScrapeStatistics {
|
||||
pub complete: usize,
|
||||
pub incomplete: usize,
|
||||
pub downloaded: usize,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct AnnounceResponseSuccess {
|
||||
#[serde(rename = "interval")]
|
||||
pub announce_interval: usize,
|
||||
pub tracker_id: String, // Optional??
|
||||
pub complete: usize,
|
||||
pub incomplete: usize,
|
||||
#[serde(
|
||||
serialize_with = "serialize_response_peers_compact"
|
||||
)]
|
||||
pub peers: Vec<ResponsePeer>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct AnnounceResponseFailure {
|
||||
pub failure_reason: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ScrapeResponse {
|
||||
pub files: HashMap<InfoHash, ScrapeStatistics>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Response {
|
||||
AnnounceSuccess(AnnounceResponseSuccess),
|
||||
AnnounceFailure(AnnounceResponseFailure),
|
||||
Scrape(ScrapeResponse)
|
||||
}
|
||||
|
||||
|
||||
impl Response {
|
||||
pub fn to_bytes(self) -> Vec<u8> {
|
||||
match bendy::serde::to_bytes(&self){
|
||||
Ok(bytes) => bytes,
|
||||
Err(err) => {
|
||||
log::error!("error encoding response: {}", err);
|
||||
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue