aquatic_ws_protocol: refactor, moving code into submodules

This commit is contained in:
Joakim Frostegård 2021-10-16 01:46:32 +02:00
parent 7c833958d8
commit 71c43aca47
11 changed files with 345 additions and 304 deletions

View file

@ -0,0 +1,60 @@
use serde::{Deserialize, Serialize};
use crate::common::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AnnounceEvent {
Started,
Stopped,
Completed,
Update,
}
impl Default for AnnounceEvent {
fn default() -> Self {
Self::Update
}
}
/// Element of AnnounceRequest.offers
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnnounceRequestOffer {
pub offer: JsonValue,
pub offer_id: OfferId,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnnounceRequest {
pub action: AnnounceAction,
pub info_hash: InfoHash,
pub peer_id: PeerId,
/// Just called "left" in protocol. Is set to None in some cases, such as
/// when opening a magnet link
#[serde(rename = "left")]
pub bytes_left: Option<usize>,
/// Can be empty. Then, default is "update"
#[serde(skip_serializing_if = "Option::is_none")]
pub event: Option<AnnounceEvent>,
/// Only when this is an array offers are sent to random peers
/// Length of this is number of peers wanted?
/// Max length of this is 10 in reference client code
/// Not sent when announce event is stopped or completed
pub offers: Option<Vec<AnnounceRequestOffer>>,
/// Seems to only get sent by client when sending offers, and is also same
/// as length of offers vector (or at least never less)
/// Max length of this is 10 in reference client code
/// Could probably be ignored, `offers.len()` should provide needed info
pub numwant: Option<usize>,
/// If empty, send response before sending offers (or possibly "skip sending update back"?)
/// Else, send MiddlemanAnswerToPeer to peer with "to_peer_id" as peer_id.
/// I think using Option is good, it seems like this isn't always set
/// (same as `offers`)
pub answer: Option<JsonValue>,
/// Likely undefined if !(answer == true)
pub to_peer_id: Option<PeerId>,
/// Sent if answer is set
pub offer_id: Option<OfferId>,
}

View file

@ -0,0 +1,35 @@
use anyhow::Context;
use serde::{Deserialize, Serialize};
pub mod announce;
pub mod scrape;
pub use announce::*;
pub use scrape::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum InMessage {
AnnounceRequest(AnnounceRequest),
ScrapeRequest(ScrapeRequest),
}
impl InMessage {
#[inline]
pub fn to_ws_message(&self) -> ::tungstenite::Message {
::tungstenite::Message::from(::serde_json::to_string(&self).unwrap())
}
#[inline]
pub fn from_ws_message(ws_message: tungstenite::Message) -> ::anyhow::Result<Self> {
use tungstenite::Message::Text;
let mut text = if let Text(text) = ws_message {
text
} else {
return Err(anyhow::anyhow!("Message is not text"));
};
return ::simd_json::serde::from_str(&mut text).context("deserialize with serde");
}
}

View file

@ -0,0 +1,29 @@
use serde::{Deserialize, Serialize};
use crate::common::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ScrapeRequestInfoHashes {
Single(InfoHash),
Multiple(Vec<InfoHash>),
}
impl ScrapeRequestInfoHashes {
pub fn as_vec(self) -> Vec<InfoHash> {
match self {
Self::Single(info_hash) => vec![info_hash],
Self::Multiple(info_hashes) => info_hashes,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScrapeRequest {
pub action: ScrapeAction,
// If omitted, scrape for all torrents, apparently
// There is some kind of parsing here too which accepts a single info hash
// and puts it into a vector
#[serde(rename = "info_hash")]
pub info_hashes: Option<ScrapeRequestInfoHashes>,
}