mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic_http: add compact response peer repr, not tested
This commit is contained in:
parent
d2e950b431
commit
5e7f8bea20
2 changed files with 29 additions and 2 deletions
|
|
@ -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<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)]
|
||||
#[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<ResponsePeer>,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue