Skip to content

Commit

Permalink
fix: dup outgoing conn (#21)
Browse files Browse the repository at this point in the history
* log ts for debug secure handshake

* fix: duplicate retry outgoing connection every tick

* chore: fix clippy
  • Loading branch information
marverlous811 authored Nov 18, 2024
1 parent 667d5eb commit 5e1d3b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ impl<SECURE: HandshakeProtocol> P2pNetwork<SECURE> {
Ok(P2pNetworkEvent::Continue)
}
InternalEvent::PeerConnectError(conn, peer, err) => {
self.neighbours.remove(&conn);
log::error!("[P2pNetwork] connection {conn} outgoing: {peer:?} error {err}");
Ok(P2pNetworkEvent::Continue)
}
Expand Down
20 changes: 16 additions & 4 deletions src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum PeerConnectionControl {
pub struct PeerConnection {
conn_id: ConnectionId,
peer_id: Option<PeerId>,
is_connected: bool,
}

impl PeerConnection {
Expand All @@ -58,7 +59,11 @@ impl PeerConnection {
Err(err) => internal_tx.send(InternalEvent::PeerConnectError(conn_id, None, err.into())).await.expect("should send to main"),
}
});
Self { conn_id, peer_id: None }
Self {
conn_id,
peer_id: None,
is_connected: false,
}
}

pub fn new_connecting<SECURE: HandshakeProtocol>(secure: Arc<SECURE>, local_id: PeerId, to_peer: PeerId, connecting: Connecting, internal_tx: Sender<InternalEvent>, ctx: SharedCtx) -> Self {
Expand Down Expand Up @@ -87,7 +92,11 @@ impl PeerConnection {
.expect("should send to main"),
}
});
Self { conn_id, peer_id: None }
Self {
conn_id,
peer_id: Some(to_peer),
is_connected: false,
}
}

pub fn conn_id(&self) -> ConnectionId {
Expand All @@ -99,11 +108,14 @@ impl PeerConnection {
}

pub fn set_connected(&mut self, peer_id: PeerId) {
self.peer_id = Some(peer_id);
if self.peer_id.is_none() {
self.peer_id = Some(peer_id);
}
self.is_connected = true
}

pub fn is_connected(&self) -> bool {
self.peer_id.is_some()
self.is_connected
}
}

Expand Down

0 comments on commit 5e1d3b4

Please sign in to comment.