mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 10:15:31 +00:00
aquatic_ws_protocol: refactor, moving code into submodules
This commit is contained in:
parent
7c833958d8
commit
71c43aca47
11 changed files with 345 additions and 304 deletions
14
aquatic_ws_protocol/src/response/announce.rs
Normal file
14
aquatic_ws_protocol/src/response/announce.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AnnounceResponse {
|
||||
pub action: AnnounceAction,
|
||||
pub info_hash: InfoHash,
|
||||
/// Client checks if this is null, not clear why
|
||||
pub complete: usize,
|
||||
pub incomplete: usize,
|
||||
#[serde(rename = "interval")]
|
||||
pub announce_interval: usize, // Default 2 min probably
|
||||
}
|
||||
16
aquatic_ws_protocol/src/response/answer.rs
Normal file
16
aquatic_ws_protocol/src/response/answer.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::*;
|
||||
|
||||
/// If announce request has answer = true, send this to peer with
|
||||
/// peer id == "to_peer_id" field
|
||||
/// Action field should be 'announce'
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct MiddlemanAnswerToPeer {
|
||||
pub action: AnnounceAction,
|
||||
/// Note: if equal to client peer_id, client ignores answer
|
||||
pub peer_id: PeerId,
|
||||
pub info_hash: InfoHash,
|
||||
pub answer: JsonValue,
|
||||
pub offer_id: OfferId,
|
||||
}
|
||||
24
aquatic_ws_protocol/src/response/error.rs
Normal file
24
aquatic_ws_protocol/src/response/error.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ErrorResponseAction {
|
||||
Announce,
|
||||
Scrape,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ErrorResponse {
|
||||
#[serde(rename = "failure reason")]
|
||||
pub failure_reason: Cow<'static, str>,
|
||||
/// Action of original request
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub action: Option<ErrorResponseAction>,
|
||||
// Should not be renamed
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub info_hash: Option<InfoHash>,
|
||||
}
|
||||
43
aquatic_ws_protocol/src/response/mod.rs
Normal file
43
aquatic_ws_protocol/src/response/mod.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod announce;
|
||||
pub mod answer;
|
||||
pub mod error;
|
||||
pub mod offer;
|
||||
pub mod scrape;
|
||||
|
||||
pub use announce::*;
|
||||
pub use answer::*;
|
||||
pub use error::*;
|
||||
pub use offer::*;
|
||||
pub use scrape::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum OutMessage {
|
||||
Offer(offer::MiddlemanOfferToPeer),
|
||||
Answer(answer::MiddlemanAnswerToPeer),
|
||||
AnnounceResponse(announce::AnnounceResponse),
|
||||
ScrapeResponse(scrape::ScrapeResponse),
|
||||
ErrorResponse(error::ErrorResponse),
|
||||
}
|
||||
|
||||
impl OutMessage {
|
||||
#[inline]
|
||||
pub fn to_ws_message(&self) -> tungstenite::Message {
|
||||
::tungstenite::Message::from(::serde_json::to_string(&self).unwrap())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_ws_message(message: ::tungstenite::Message) -> ::anyhow::Result<Self> {
|
||||
use tungstenite::Message::{Binary, Text};
|
||||
|
||||
let mut text = match message {
|
||||
Text(text) => text,
|
||||
Binary(bytes) => String::from_utf8(bytes)?,
|
||||
_ => return Err(anyhow::anyhow!("Message is neither text nor bytes")),
|
||||
};
|
||||
|
||||
Ok(::simd_json::serde::from_str(&mut text)?)
|
||||
}
|
||||
}
|
||||
19
aquatic_ws_protocol/src/response/offer.rs
Normal file
19
aquatic_ws_protocol/src/response/offer.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::*;
|
||||
|
||||
/// Apparently, these are sent to a number of peers when they are set
|
||||
/// in an AnnounceRequest
|
||||
/// action = "announce"
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct MiddlemanOfferToPeer {
|
||||
pub action: AnnounceAction,
|
||||
/// Peer id of peer sending offer
|
||||
/// Note: if equal to client peer_id, client ignores offer
|
||||
pub peer_id: PeerId,
|
||||
pub info_hash: InfoHash,
|
||||
/// Gets copied from AnnounceRequestOffer
|
||||
pub offer: JsonValue,
|
||||
/// Gets copied from AnnounceRequestOffer
|
||||
pub offer_id: OfferId,
|
||||
}
|
||||
19
aquatic_ws_protocol/src/response/scrape.rs
Normal file
19
aquatic_ws_protocol/src/response/scrape.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use hashbrown::HashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ScrapeResponse {
|
||||
pub action: ScrapeAction,
|
||||
pub files: HashMap<InfoHash, ScrapeStatistics>,
|
||||
// Looks like `flags` field is ignored in reference client
|
||||
// pub flags: HashMap<String, usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ScrapeStatistics {
|
||||
pub complete: usize,
|
||||
pub incomplete: usize,
|
||||
pub downloaded: usize,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue