rqbit/crates/librqbit/src/peer_state.rs

40 lines
1 KiB
Rust
Raw Normal View History

2021-06-28 14:23:28 +01:00
use std::{collections::HashSet, net::SocketAddr, sync::Arc};
use tokio::sync::{Notify, Semaphore};
use crate::{torrent_state::InflightRequest, type_aliases::BF};
pub enum PeerState {
Connecting(SocketAddr),
Live(LivePeerState),
}
pub struct LivePeerState {
#[allow(unused)]
2021-06-28 14:42:19 +01:00
peer_id: [u8; 20],
2021-06-28 14:23:28 +01:00
pub i_am_choked: bool,
#[allow(unused)]
pub peer_choked: bool,
#[allow(unused)]
pub peer_interested: bool,
pub outstanding_requests: Arc<Semaphore>,
pub have_notify: Arc<Notify>,
pub bitfield: Option<BF>,
pub inflight_requests: HashSet<InflightRequest>,
}
2021-06-28 14:42:19 +01:00
impl LivePeerState {
pub fn new(peer_id: [u8; 20]) -> Self {
LivePeerState {
peer_id: peer_id,
i_am_choked: true,
peer_choked: true,
peer_interested: false,
bitfield: None,
have_notify: Arc::new(Notify::new()),
outstanding_requests: Arc::new(Semaphore::new(0)),
inflight_requests: Default::default(),
}
}
}