aquatic_http: add compact response peer repr, not tested

This commit is contained in:
Joakim Frostegård 2020-07-02 14:37:47 +02:00
parent d2e950b431
commit 5e7f8bea20
2 changed files with 29 additions and 2 deletions

View file

@ -11,7 +11,7 @@
* support TLS and plain at the same time?? * support TLS and plain at the same time??
* really close connections after sending response?? * really close connections after sending response??
* fixed size buffer is probably bad * 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 * scrape info hash parsing: multiple ought to be accepted
* move stuff to common crate with ws: what about Request/InMessage etc? * move stuff to common crate with ws: what about Request/InMessage etc?
* info hashes, peer ids: verify that they are 20 bytes * info hashes, peer ids: verify that they are 20 bytes

View file

@ -1,6 +1,6 @@
use std::net::IpAddr; use std::net::IpAddr;
use hashbrown::HashMap; use hashbrown::HashMap;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize, Serializer};
use crate::common::Peer; use crate::common::Peer;
@ -9,6 +9,30 @@ use crate::common::Peer;
// use serde_helpers::*; // use serde_helpers::*;
pub fn serialize_response_peers_compact<S>(
response_peers: &Vec<ResponsePeer>,
serializer: S
) -> Result<S::Ok, S::Error> 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)] #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)] #[serde(transparent)]
pub struct PeerId( pub struct PeerId(
@ -90,6 +114,9 @@ pub struct AnnounceResponseSuccess {
pub tracker_id: String, // Optional?? pub tracker_id: String, // Optional??
pub complete: usize, pub complete: usize,
pub incomplete: usize, pub incomplete: usize,
#[serde(
serialize_with = "serialize_response_peers_compact"
)]
pub peers: Vec<ResponsePeer>, pub peers: Vec<ResponsePeer>,
} }