Skip to content

Commit

Permalink
review: address Thomas review
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed Jul 5, 2023
1 parent 6671af3 commit 7edc88b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
1 change: 1 addition & 0 deletions libp2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub use libp2p_tls as tls;
#[doc(inline)]
pub use libp2p_uds as uds;
#[cfg(feature = "upnp")]
#[cfg(not(target_arch = "wasm32"))]
#[doc(inline)]
pub use libp2p_upnp as upnp;
#[cfg(feature = "wasm-ext")]
Expand Down
67 changes: 34 additions & 33 deletions protocols/upnp/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,35 @@ const MAPPING_DURATION: u64 = 3600;
const MAPPING_TIMEOUT: u64 = MAPPING_DURATION / 2;

/// Map a port on the gateway.
fn map_port<P: Provider + 'static>(
async fn map_port<P: Provider + 'static>(
gateway: Arc<P::Gateway>,
mapping: Mapping,
permanent: bool,
) -> BoxFuture<'static, Event> {
) -> Event {
let duration = if permanent { 0 } else { MAPPING_DURATION };

P::add_port(
match P::add_port(
gateway,
mapping.protocol,
mapping.internal_addr,
Duration::from_secs(duration),
)
.map(move |result| match result {
.await
{
Ok(()) => Event::Mapped(mapping),
Err(err) => Event::MapFailure(mapping, err),
})
.boxed()
}
}

/// Remove a port mapping on the gateway.
fn remove_port_mapping<P: Provider + 'static>(
async fn remove_port_mapping<P: Provider + 'static>(
gateway: Arc<P::Gateway>,
mapping: Mapping,
) -> BoxFuture<'static, Event> {
P::remove_port(gateway, mapping.protocol, mapping.internal_addr.port())
.map(move |result| match result {
Ok(()) => Event::Removed(mapping),
Err(err) => Event::RemovalFailure(mapping, err),
})
.boxed()
) -> Event {
match P::remove_port(gateway, mapping.protocol, mapping.internal_addr.port()).await {
Ok(()) => Event::Removed(mapping),
Err(err) => Event::RemovalFailure(mapping, err),
}
}

/// A [`Provider::Gateway`] event.
Expand Down Expand Up @@ -260,11 +258,10 @@ where
multiaddr: multiaddr.clone(),
};

self.pending_events.push(map_port::<P>(
gateway.clone(),
mapping.clone(),
self.config.permanent,
));
self.pending_events.push(
map_port::<P>(gateway.clone(), mapping.clone(), self.config.permanent)
.boxed(),
);

self.mappings.insert(mapping, MappingState::Pending);
}
Expand All @@ -281,8 +278,9 @@ where
}) => {
if let GatewayState::Available((gateway, _external_addr)) = &self.state {
if let Some((mapping, _state)) = self.mappings.remove_entry(&listener_id) {
self.pending_events
.push(remove_port_mapping::<P>(gateway.clone(), mapping.clone()));
self.pending_events.push(
remove_port_mapping::<P>(gateway.clone(), mapping.clone()).boxed(),
);
self.mappings.insert(mapping, MappingState::Pending);
}
}
Expand Down Expand Up @@ -435,8 +433,9 @@ where
mapping.internal_addr,
mapping.protocol
);
self.pending_events
.push(remove_port_mapping::<P>(gateway.clone(), mapping));
self.pending_events.push(
remove_port_mapping::<P>(gateway.clone(), mapping).boxed(),
);
}
}
}
Expand All @@ -445,20 +444,22 @@ where
for (mapping, state) in self.mappings.iter_mut() {
match state {
MappingState::Inactive => {
self.pending_events.push(map_port::<P>(
gateway.clone(),
mapping.clone(),
self.config.permanent,
));
self.pending_events.push(
map_port::<P>(
gateway.clone(),
mapping.clone(),
self.config.permanent,
)
.boxed(),
);
*state = MappingState::Pending;
}
MappingState::Active(timeout) => {
if Pin::new(timeout).poll(cx).is_ready() {
self.pending_events.push(map_port::<P>(
gateway.clone(),
mapping.clone(),
false,
));
self.pending_events.push(
map_port::<P>(gateway.clone(), mapping.clone(), false)
.boxed(),
);
}
}
MappingState::Pending | MappingState::Permanent => {}
Expand Down
2 changes: 1 addition & 1 deletion protocols/upnp/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl fmt::Display for Protocol {
#[async_trait]
pub trait Provider {
/// The gateway of obtained from [`Provider::search_gateway`].
type Gateway;
type Gateway: Send + Sync;

/// Search for the gateway endpoint on the local network.
async fn search_gateway(config: Config) -> Result<(Self::Gateway, Ipv4Addr), Box<dyn Error>>;
Expand Down

0 comments on commit 7edc88b

Please sign in to comment.