mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 02:05:30 +00:00
aquatic_http: move protocol module to new crate aquatic_http_protocol
This commit is contained in:
parent
4caf174da5
commit
4ac2012a2a
36 changed files with 65 additions and 24 deletions
|
|
@ -1,73 +0,0 @@
|
|||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
|
||||
use serde::Serializer;
|
||||
use smartstring::{SmartString, LazyCompact};
|
||||
|
||||
use super::response::ResponsePeer;
|
||||
|
||||
|
||||
/// Not for serde
|
||||
pub fn deserialize_20_bytes(value: SmartString<LazyCompact>) -> anyhow::Result<[u8; 20]> {
|
||||
let mut arr = [0u8; 20];
|
||||
let mut char_iter = value.chars();
|
||||
|
||||
for a in arr.iter_mut(){
|
||||
if let Some(c) = char_iter.next(){
|
||||
if c as u32 > 255 {
|
||||
return Err(anyhow::anyhow!(
|
||||
"character not in single byte range: {:#?}",
|
||||
c
|
||||
));
|
||||
}
|
||||
|
||||
*a = c as u8;
|
||||
} else {
|
||||
return Err(anyhow::anyhow!("less than 20 bytes: {:#?}", value));
|
||||
}
|
||||
}
|
||||
|
||||
if char_iter.next().is_some(){
|
||||
Err(anyhow::anyhow!("more than 20 bytes: {:#?}", value))
|
||||
} else {
|
||||
Ok(arr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub fn serialize_20_bytes<S>(
|
||||
bytes: &[u8; 20],
|
||||
serializer: S
|
||||
) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
serializer.serialize_bytes(bytes)
|
||||
}
|
||||
|
||||
|
||||
pub fn serialize_response_peers_ipv4<S>(
|
||||
response_peers: &[ResponsePeer<Ipv4Addr>],
|
||||
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 {
|
||||
bytes.extend_from_slice(&u32::from(peer.ip_address).to_be_bytes());
|
||||
bytes.extend_from_slice(&peer.port.to_be_bytes())
|
||||
}
|
||||
|
||||
serializer.serialize_bytes(&bytes)
|
||||
}
|
||||
|
||||
|
||||
pub fn serialize_response_peers_ipv6<S>(
|
||||
response_peers: &[ResponsePeer<Ipv6Addr>],
|
||||
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 {
|
||||
bytes.extend_from_slice(&u128::from(peer.ip_address).to_be_bytes());
|
||||
bytes.extend_from_slice(&peer.port.to_be_bytes())
|
||||
}
|
||||
|
||||
serializer.serialize_bytes(&bytes)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue