mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 02:05:30 +00:00
improve ws protocol struct naming and documentation
This commit is contained in:
parent
b473bb6fba
commit
0789f7ec3b
17 changed files with 193 additions and 137 deletions
15
crates/ws_protocol/src/outgoing/announce.rs
Normal file
15
crates/ws_protocol/src/outgoing/announce.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::*;
|
||||
|
||||
/// Plain response to an AnnounceRequest
|
||||
#[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
|
||||
}
|
||||
17
crates/ws_protocol/src/outgoing/answer.rs
Normal file
17
crates/ws_protocol/src/outgoing/answer.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::*;
|
||||
|
||||
/// Message sent to peer when other peer has replied to its WebRTC offer
|
||||
///
|
||||
/// Sent if fields answer, to_peer_id and offer_id are set in announce request
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AnswerOutMessage {
|
||||
/// Always "announce"
|
||||
pub action: AnnounceAction,
|
||||
/// Note: if equal to client peer_id, client ignores answer
|
||||
pub peer_id: PeerId,
|
||||
pub info_hash: InfoHash,
|
||||
pub answer: RtcAnswer,
|
||||
pub offer_id: OfferId,
|
||||
}
|
||||
24
crates/ws_protocol/src/outgoing/error.rs
Normal file
24
crates/ws_protocol/src/outgoing/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)]
|
||||
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>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ErrorResponseAction {
|
||||
Announce,
|
||||
Scrape,
|
||||
}
|
||||
44
crates/ws_protocol/src/outgoing/mod.rs
Normal file
44
crates/ws_protocol/src/outgoing/mod.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
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::*;
|
||||
|
||||
/// Message sent by tracker
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum OutMessage {
|
||||
OfferOutMessage(OfferOutMessage),
|
||||
AnswerOutMessage(AnswerOutMessage),
|
||||
AnnounceResponse(AnnounceResponse),
|
||||
ScrapeResponse(ScrapeResponse),
|
||||
ErrorResponse(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: Vec<u8> = match message {
|
||||
Text(text) => text.into(),
|
||||
Binary(bytes) => String::from_utf8(bytes)?.into(),
|
||||
_ => return Err(anyhow::anyhow!("Message is neither text nor bytes")),
|
||||
};
|
||||
|
||||
Ok(::simd_json::serde::from_slice(&mut text)?)
|
||||
}
|
||||
}
|
||||
22
crates/ws_protocol/src/outgoing/offer.rs
Normal file
22
crates/ws_protocol/src/outgoing/offer.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::*;
|
||||
|
||||
/// Message sent to peer when other peer wants to initiate a WebRTC connection
|
||||
///
|
||||
/// One is sent for each offer in an announce request
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct OfferOutMessage {
|
||||
/// Always "announce"
|
||||
pub action: AnnounceAction,
|
||||
/// Peer id of peer sending offer
|
||||
///
|
||||
/// Note: if equal to client peer_id, reference client ignores offer
|
||||
pub peer_id: PeerId,
|
||||
/// Torrent info hash
|
||||
pub info_hash: InfoHash,
|
||||
/// Gets copied from AnnounceRequestOffer
|
||||
pub offer: RtcOffer,
|
||||
/// Gets copied from AnnounceRequestOffer
|
||||
pub offer_id: OfferId,
|
||||
}
|
||||
19
crates/ws_protocol/src/outgoing/scrape.rs
Normal file
19
crates/ws_protocol/src/outgoing/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>,
|
||||
// It 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