Skip to content

Commit

Permalink
feat(swarm): implement Copy and Clone for FromSwarm
Browse files Browse the repository at this point in the history
We can make `FromSwarm` implement `Copy` and `Close` which makes it much easier to

a) generate code in `libp2p-swarm-derive`
b) manually wrap a `NetworkBehaviour`

Previously, we couldn't do this because `ConnectionClosed` would have a `handler` field that cannot be cloned / copied.

Related: libp2p#4076.
Related: libp2p#4581.

Pull-Request: libp2p#4825.
  • Loading branch information
thomaseizinger committed Dec 3, 2023
1 parent 17c03ae commit 5f9f70d
Showing 1 changed file with 17 additions and 73 deletions.
90 changes: 17 additions & 73 deletions src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ use libp2p_request_response::{
self as request_response, InboundRequestId, OutboundRequestId, ProtocolSupport, ResponseChannel,
};
use libp2p_swarm::{
behaviour::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredListenAddr,
ExternalAddrExpired, FromSwarm,
},
ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, NewExternalAddrCandidate,
THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, THandler, THandlerInEvent,
THandlerOutEvent, ToSwarm,
};
use std::{
collections::{HashMap, HashSet, VecDeque},
Expand Down Expand Up @@ -363,18 +360,10 @@ impl Behaviour {
ConnectionClosed {
peer_id,
connection_id,
endpoint,
remaining_established,
..
}: ConnectionClosed,
) {
self.inner
.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
connection_id,
endpoint,
remaining_established,
}));

if remaining_established == 0 {
self.connected.remove(&peer_id);
} else {
Expand All @@ -386,20 +375,7 @@ impl Behaviour {
}
}

fn on_dial_failure(
&mut self,
DialFailure {
peer_id,
connection_id,
error,
}: DialFailure,
) {
self.inner
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id,
connection_id,
error,
}));
fn on_dial_failure(&mut self, DialFailure { peer_id, error, .. }: DialFailure) {
if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) {
self.pending_actions
.push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event)));
Expand Down Expand Up @@ -542,57 +518,25 @@ impl NetworkBehaviour for Behaviour {

fn on_swarm_event(&mut self, event: FromSwarm) {
self.listen_addresses.on_swarm_event(&event);
self.inner.on_swarm_event(event);

match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.inner
.on_swarm_event(FromSwarm::ConnectionEstablished(connection_established));
self.on_connection_established(connection_established)
}
FromSwarm::ConnectionClosed(connection_closed) => {
self.on_connection_closed(connection_closed)
}
FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure),
FromSwarm::AddressChange(address_change) => {
self.inner
.on_swarm_event(FromSwarm::AddressChange(address_change));
self.on_address_change(address_change)
}
listen_addr @ FromSwarm::NewListenAddr(_) => {
self.inner.on_swarm_event(listen_addr);
FromSwarm::ConnectionEstablished(e) => self.on_connection_established(e),
FromSwarm::ConnectionClosed(e) => self.on_connection_closed(e),
FromSwarm::DialFailure(e) => self.on_dial_failure(e),
FromSwarm::AddressChange(e) => self.on_address_change(e),
FromSwarm::NewListenAddr(_) => {
self.as_client().on_new_address();
}
FromSwarm::ExpiredListenAddr(ExpiredListenAddr { listener_id, addr }) => {
self.inner
.on_swarm_event(FromSwarm::ExpiredListenAddr(ExpiredListenAddr {
listener_id,
addr,
}));
self.as_client().on_expired_address(addr);
}
FromSwarm::ExternalAddrExpired(ExternalAddrExpired { addr }) => {
self.inner
.on_swarm_event(FromSwarm::ExternalAddrExpired(ExternalAddrExpired { addr }));
self.as_client().on_expired_address(addr);
}
FromSwarm::NewExternalAddrCandidate(NewExternalAddrCandidate { addr }) => {
self.inner
.on_swarm_event(FromSwarm::NewExternalAddrCandidate(
NewExternalAddrCandidate { addr },
));
self.probe_address(addr.to_owned());
}
listen_failure @ FromSwarm::ListenFailure(_) => {
self.inner.on_swarm_event(listen_failure)
FromSwarm::ExpiredListenAddr(e) => {
self.as_client().on_expired_address(e.addr);
}
new_listener @ FromSwarm::NewListener(_) => self.inner.on_swarm_event(new_listener),
listener_error @ FromSwarm::ListenerError(_) => {
self.inner.on_swarm_event(listener_error)
FromSwarm::ExternalAddrExpired(e) => {
self.as_client().on_expired_address(e.addr);
}
listener_closed @ FromSwarm::ListenerClosed(_) => {
self.inner.on_swarm_event(listener_closed)
FromSwarm::NewExternalAddrCandidate(e) => {
self.probe_address(e.addr.to_owned());
}
confirmed @ FromSwarm::ExternalAddrConfirmed(_) => self.inner.on_swarm_event(confirmed),
_ => {}
}
}
Expand Down

0 comments on commit 5f9f70d

Please sign in to comment.