mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
Move some code from aquatic_udp into aquatic_common
This commit is contained in:
parent
1b8d74e26d
commit
5c83af9f88
10 changed files with 111 additions and 82 deletions
|
|
@ -13,6 +13,7 @@ path = "src/lib/lib.rs"
|
|||
name = "aquatic_udp"
|
||||
|
||||
[dependencies]
|
||||
aquatic_common = { path = "../aquatic_common" }
|
||||
bittorrent_udp = { path = "../bittorrent_udp" }
|
||||
cli_helpers = { path = "../cli_helpers" }
|
||||
crossbeam-channel = "0.4"
|
||||
|
|
|
|||
|
|
@ -1,31 +1,17 @@
|
|||
use std::net::{SocketAddr, IpAddr};
|
||||
use std::sync::{Arc, atomic::AtomicUsize};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use hashbrown::HashMap;
|
||||
use indexmap::IndexMap;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
pub use aquatic_common::ValidUntil;
|
||||
pub use bittorrent_udp::types::*;
|
||||
|
||||
|
||||
pub const MAX_PACKET_SIZE: usize = 4096;
|
||||
|
||||
|
||||
/// Peer or connection valid until this instant
|
||||
///
|
||||
/// Used instead of "last seen" or similar to hopefully prevent arithmetic
|
||||
/// overflow when cleaning.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ValidUntil(pub Instant);
|
||||
|
||||
|
||||
impl ValidUntil {
|
||||
pub fn new(offset_seconds: u64) -> Self {
|
||||
Self(Instant::now() + Duration::from_secs(offset_seconds))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct ConnectionKey {
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ use std::time::Duration;
|
|||
use std::vec::Drain;
|
||||
|
||||
use crossbeam_channel::{Sender, Receiver};
|
||||
use indexmap::IndexMap;
|
||||
use parking_lot::MutexGuard;
|
||||
use rand::{SeedableRng, Rng, rngs::{SmallRng, StdRng}};
|
||||
|
||||
use aquatic_common::extract_response_peers;
|
||||
use bittorrent_udp::types::*;
|
||||
|
||||
use crate::common::*;
|
||||
|
|
@ -305,67 +305,6 @@ pub fn handle_scrape_requests(
|
|||
}
|
||||
|
||||
|
||||
/// Extract response peers
|
||||
///
|
||||
/// If there are more peers in map than `max_num_peers_to_take`, do a
|
||||
/// half-random selection of peers from first and second halves of map,
|
||||
/// in order to avoid returning too homogeneous peers.
|
||||
///
|
||||
/// Don't care if we send back announcing peer.
|
||||
#[inline]
|
||||
pub fn extract_response_peers<K, V, R, F>(
|
||||
rng: &mut impl Rng,
|
||||
peer_map: &IndexMap<K, V>,
|
||||
max_num_peers_to_take: usize,
|
||||
peer_conversion_function: F
|
||||
) -> Vec<R>
|
||||
where
|
||||
K: Eq + ::std::hash::Hash,
|
||||
F: Fn(&V) -> R
|
||||
|
||||
{
|
||||
let peer_map_len = peer_map.len();
|
||||
|
||||
if peer_map_len <= max_num_peers_to_take {
|
||||
peer_map.values()
|
||||
.map(peer_conversion_function)
|
||||
.collect()
|
||||
} else {
|
||||
let half_num_to_take = max_num_peers_to_take / 2;
|
||||
let half_peer_map_len = peer_map_len / 2;
|
||||
|
||||
let offset_first_half = rng.gen_range(
|
||||
0,
|
||||
(half_peer_map_len + (peer_map_len % 2)) - half_num_to_take
|
||||
);
|
||||
let offset_second_half = rng.gen_range(
|
||||
half_peer_map_len,
|
||||
peer_map_len - half_num_to_take
|
||||
);
|
||||
|
||||
let end_first_half = offset_first_half + half_num_to_take;
|
||||
let end_second_half = offset_second_half + half_num_to_take + (max_num_peers_to_take % 2);
|
||||
|
||||
let mut peers: Vec<R> = Vec::with_capacity(max_num_peers_to_take);
|
||||
|
||||
for i in offset_first_half..end_first_half {
|
||||
if let Some((_, peer)) = peer_map.get_index(i){
|
||||
peers.push(peer_conversion_function(peer))
|
||||
}
|
||||
}
|
||||
for i in offset_second_half..end_second_half {
|
||||
if let Some((_, peer)) = peer_map.get_index(i){
|
||||
peers.push(peer_conversion_function(peer))
|
||||
}
|
||||
}
|
||||
|
||||
debug_assert_eq!(peers.len(), max_num_peers_to_take);
|
||||
|
||||
peers
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn calc_max_num_peers_to_take(
|
||||
config: &Config,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue