mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic: remove unnecessary fields from Peer; general clean up
This commit is contained in:
parent
31c8864658
commit
ad68dda9f5
2 changed files with 12 additions and 13 deletions
|
|
@ -22,6 +22,7 @@ pub struct ConnectionKey {
|
|||
pub socket_addr: SocketAddr
|
||||
}
|
||||
|
||||
|
||||
pub type ConnectionMap = DashMap<ConnectionKey, Time>;
|
||||
|
||||
|
||||
|
|
@ -32,13 +33,14 @@ pub enum PeerStatus {
|
|||
Stopped
|
||||
}
|
||||
|
||||
|
||||
impl PeerStatus {
|
||||
/// Determine peer status from announce event and number of bytes left.
|
||||
///
|
||||
/// Likely, the last branch will be taken most of the time.
|
||||
#[inline]
|
||||
pub fn from_event_and_bytes_left(
|
||||
event: AnnounceEvent,
|
||||
event: AnnounceEvent,
|
||||
bytes_left: NumberOfBytes
|
||||
) -> Self {
|
||||
if event == AnnounceEvent::Stopped {
|
||||
|
|
@ -51,10 +53,9 @@ impl PeerStatus {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Peer {
|
||||
pub id: PeerId,
|
||||
pub connection_id: ConnectionId,
|
||||
pub ip_address: IpAddr,
|
||||
pub port: Port,
|
||||
pub status: PeerStatus,
|
||||
|
|
@ -75,20 +76,21 @@ impl Peer {
|
|||
announce_request: &AnnounceRequest,
|
||||
ip_address: IpAddr
|
||||
) -> Self {
|
||||
let status = PeerStatus::from_event_and_bytes_left(
|
||||
announce_request.event,
|
||||
announce_request.bytes_left
|
||||
);
|
||||
|
||||
Self {
|
||||
id: announce_request.peer_id,
|
||||
connection_id: announce_request.connection_id,
|
||||
ip_address,
|
||||
port: announce_request.port,
|
||||
status: PeerStatus::from_event_and_bytes_left(
|
||||
announce_request.event,
|
||||
announce_request.bytes_left
|
||||
),
|
||||
status,
|
||||
last_announce: Time(Instant::now())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone)]
|
||||
pub struct PeerMapKey {
|
||||
pub ip: IpAddr,
|
||||
|
|
@ -98,6 +100,7 @@ pub struct PeerMapKey {
|
|||
|
||||
pub type PeerMap = IndexMap<PeerMapKey, Peer>;
|
||||
|
||||
|
||||
pub struct TorrentData {
|
||||
pub peers: PeerMap,
|
||||
pub num_seeders: AtomicUsize,
|
||||
|
|
@ -129,8 +132,6 @@ pub struct Statistics {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
pub connections: Arc<ConnectionMap>,
|
||||
|
|
|
|||
|
|
@ -259,9 +259,7 @@ mod tests {
|
|||
peer_id,
|
||||
};
|
||||
let value = Peer {
|
||||
connection_id: ConnectionId(0),
|
||||
ip_address,
|
||||
id: peer_id,
|
||||
port: Port(1),
|
||||
status: PeerStatus::Leeching,
|
||||
last_announce: Time(Instant::now()),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue