mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic_http: use new trait Ip for ip generic parameters
This commit is contained in:
parent
2386dd0e8b
commit
8ddccb20aa
3 changed files with 25 additions and 19 deletions
|
|
@ -16,6 +16,12 @@ use crate::protocol::request::Request;
|
|||
use crate::protocol::response::{Response, ResponsePeer};
|
||||
|
||||
|
||||
pub trait Ip: Copy + Eq + ::std::hash::Hash {}
|
||||
|
||||
impl Ip for Ipv4Addr {}
|
||||
impl Ip for Ipv6Addr {}
|
||||
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct ConnectionMeta {
|
||||
/// Index of socket worker responsible for this connection. Required for
|
||||
|
|
@ -27,10 +33,10 @@ pub struct ConnectionMeta {
|
|||
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct PeerConnectionMeta<P> {
|
||||
pub struct PeerConnectionMeta<I: Ip> {
|
||||
pub worker_index: usize,
|
||||
pub poll_token: Token,
|
||||
pub peer_ip_address: P,
|
||||
pub peer_ip_address: I,
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -63,16 +69,16 @@ impl PeerStatus {
|
|||
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Peer<P> {
|
||||
pub connection_meta: PeerConnectionMeta<P>,
|
||||
pub struct Peer<I: Ip> {
|
||||
pub connection_meta: PeerConnectionMeta<I>,
|
||||
pub port: u16,
|
||||
pub status: PeerStatus,
|
||||
pub valid_until: ValidUntil,
|
||||
}
|
||||
|
||||
|
||||
impl <S: Copy>Peer<S> {
|
||||
pub fn to_response_peer(&self) -> ResponsePeer<S> {
|
||||
impl <I: Ip>Peer<I> {
|
||||
pub fn to_response_peer(&self) -> ResponsePeer<I> {
|
||||
ResponsePeer {
|
||||
ip_address: self.connection_meta.peer_ip_address,
|
||||
port: self.port
|
||||
|
|
@ -82,23 +88,23 @@ impl <S: Copy>Peer<S> {
|
|||
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct PeerMapKey<P: Eq + ::std::hash::Hash> {
|
||||
pub struct PeerMapKey<I: Ip> {
|
||||
pub peer_id: PeerId,
|
||||
pub ip_or_key: Either<P, String>
|
||||
pub ip_or_key: Either<I, String>
|
||||
}
|
||||
|
||||
|
||||
pub type PeerMap<P> = IndexMap<PeerMapKey<P>, Peer<P>>;
|
||||
pub type PeerMap<I> = IndexMap<PeerMapKey<I>, Peer<I>>;
|
||||
|
||||
|
||||
pub struct TorrentData<P: Eq + ::std::hash::Hash> {
|
||||
pub peers: PeerMap<P>,
|
||||
pub struct TorrentData<I: Ip> {
|
||||
pub peers: PeerMap<I>,
|
||||
pub num_seeders: usize,
|
||||
pub num_leechers: usize,
|
||||
}
|
||||
|
||||
|
||||
impl <P: Eq + ::std::hash::Hash> Default for TorrentData<P> {
|
||||
impl <I: Ip> Default for TorrentData<I> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
|
@ -110,7 +116,7 @@ impl <P: Eq + ::std::hash::Hash> Default for TorrentData<P> {
|
|||
}
|
||||
|
||||
|
||||
pub type TorrentMap<P> = HashMap<InfoHash, TorrentData<P>>;
|
||||
pub type TorrentMap<I> = HashMap<InfoHash, TorrentData<I>>;
|
||||
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
|
|||
|
|
@ -170,14 +170,14 @@ pub fn handle_announce_requests(
|
|||
|
||||
|
||||
/// Insert/update peer. Return num_seeders, num_leechers and response peers
|
||||
fn upsert_peer_and_get_response_peers<P: Copy + Eq + ::std::hash::Hash>(
|
||||
fn upsert_peer_and_get_response_peers<I: Ip>(
|
||||
config: &Config,
|
||||
rng: &mut impl Rng,
|
||||
request_sender_meta: PeerConnectionMeta<P>,
|
||||
torrent_data: &mut TorrentData<P>,
|
||||
request_sender_meta: PeerConnectionMeta<I>,
|
||||
torrent_data: &mut TorrentData<I>,
|
||||
request: AnnounceRequest,
|
||||
valid_until: ValidUntil,
|
||||
) -> (usize, usize, Vec<ResponsePeer<P>>) {
|
||||
) -> (usize, usize, Vec<ResponsePeer<I>>) {
|
||||
// Insert/update/remove peer who sent this request
|
||||
{
|
||||
let peer_status = PeerStatus::from_event_and_bytes_left(
|
||||
|
|
@ -235,7 +235,7 @@ fn upsert_peer_and_get_response_peers<P: Copy + Eq + ::std::hash::Hash>(
|
|||
Some(numwant) => numwant.min(config.protocol.max_peers),
|
||||
};
|
||||
|
||||
let response_peers: Vec<ResponsePeer<P>> = extract_response_peers(
|
||||
let response_peers: Vec<ResponsePeer<I>> = extract_response_peers(
|
||||
rng,
|
||||
&torrent_data.peers,
|
||||
max_num_peers_to_take,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ pub fn clean_torrents(state: &State){
|
|||
}
|
||||
|
||||
|
||||
fn clean_torrent_map<I: Eq + ::std::hash::Hash>(
|
||||
fn clean_torrent_map<I: Ip>(
|
||||
torrent_map: &mut TorrentMap<I>,
|
||||
){
|
||||
let now = Instant::now();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue