Peer struct knows its addr

This commit is contained in:
Igor Katson 2024-12-04 10:06:08 +00:00
parent adc2ca97b3
commit fa3e8d949b
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 8 additions and 2 deletions

View file

@ -381,6 +381,7 @@ impl TorrentStateLive {
Entry::Vacant(vac) => {
atomic_inc(&self.peers.stats.seen);
let peer = Peer::new_live_for_incoming_connection(
*vac.key(),
Id20::new(checked_peer.handshake.peer_id),
tx.clone(),
&self.peers,

View file

@ -18,8 +18,9 @@ pub(crate) type InflightRequest = ChunkInfo;
pub(crate) type PeerRx = UnboundedReceiver<WriterRequest>;
pub(crate) type PeerTx = UnboundedSender<WriterRequest>;
#[derive(Debug, Default)]
#[derive(Debug)]
pub(crate) struct Peer {
pub addr: SocketAddr,
pub state: PeerStateNoMut,
pub stats: stats::atomic::PeerStats,
pub outgoing_address: Option<SocketAddr>,
@ -27,6 +28,7 @@ pub(crate) struct Peer {
impl Peer {
pub fn new_live_for_incoming_connection(
addr: SocketAddr,
peer_id: Id20,
tx: PeerTx,
counters: &PeerStates,
@ -36,6 +38,7 @@ impl Peer {
counter.inc(&state.0);
}
Self {
addr,
state,
stats: Default::default(),
outgoing_address: None,
@ -44,8 +47,10 @@ impl Peer {
pub fn new_with_outgoing_address(addr: SocketAddr) -> Self {
Self {
addr,
outgoing_address: Some(addr),
..Default::default()
stats: Default::default(),
state: Default::default(),
}
}