From 5e7f8bea20e253eee650626733c4e2c644e6148c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Thu, 2 Jul 2020 14:37:47 +0200 Subject: [PATCH] aquatic_http: add compact response peer repr, not tested --- TODO.md | 2 +- aquatic_http/src/lib/protocol/mod.rs | 29 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 2f35f49..3997808 100644 --- a/TODO.md +++ b/TODO.md @@ -11,7 +11,7 @@ * support TLS and plain at the same time?? * really close connections after sending response?? * fixed size buffer is probably bad -* compact peer representation in announce response +* compact peer representation in announce response: is implementation correct? * scrape info hash parsing: multiple ought to be accepted * move stuff to common crate with ws: what about Request/InMessage etc? * info hashes, peer ids: verify that they are 20 bytes diff --git a/aquatic_http/src/lib/protocol/mod.rs b/aquatic_http/src/lib/protocol/mod.rs index 8bf5488..d56e21c 100644 --- a/aquatic_http/src/lib/protocol/mod.rs +++ b/aquatic_http/src/lib/protocol/mod.rs @@ -1,6 +1,6 @@ use std::net::IpAddr; use hashbrown::HashMap; -use serde::{Serialize, Deserialize}; +use serde::{Serialize, Deserialize, Serializer}; use crate::common::Peer; @@ -9,6 +9,30 @@ use crate::common::Peer; // use serde_helpers::*; +pub fn serialize_response_peers_compact( + response_peers: &Vec, + serializer: S +) -> Result where S: Serializer { + let mut bytes = Vec::with_capacity(response_peers.len() * 6); + + for peer in response_peers { + match peer.ip_address { + IpAddr::V4(ip) => { + bytes.extend_from_slice(&u32::from(ip).to_be_bytes()); + bytes.extend_from_slice(&peer.port.to_be_bytes()) + }, + IpAddr::V6(_) => { + continue + } + } + } + + let text: String = bytes.into_iter().map(|byte| byte as char).collect(); + + serializer.serialize_str(&text) +} + + #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] #[serde(transparent)] pub struct PeerId( @@ -90,6 +114,9 @@ pub struct AnnounceResponseSuccess { pub tracker_id: String, // Optional?? pub complete: usize, pub incomplete: usize, + #[serde( + serialize_with = "serialize_response_peers_compact" + )] pub peers: Vec, }