refactor(dcutr): remove direct::Handler

With recent refactorings in `libp2p-swarm`, there is no more need for `handler::direct` at all.

Resolves #4013.

Pull-Request: #4016.
This commit is contained in:
Thomas Coratger 2023-06-01 06:34:28 +02:00 committed by GitHub
parent 92c8cc451f
commit 87e863e8c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 125 deletions

View File

@ -70,7 +70,7 @@ pub enum Error {
pub struct Behaviour {
/// Queue of actions to return when polled.
queued_events: VecDeque<ToSwarm<Event, Either<handler::relayed::Command, Either<Void, Void>>>>,
queued_events: VecDeque<ToSwarm<Event, Either<handler::relayed::Command, Void>>>,
/// All direct (non-relayed) connections.
direct_connections: HashMap<PeerId, HashSet<ConnectionId>>,
@ -233,10 +233,7 @@ impl Behaviour {
}
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = Either<
handler::relayed::Handler,
Either<handler::direct::Handler, dummy::ConnectionHandler>,
>;
type ConnectionHandler = Either<handler::relayed::Handler, dummy::ConnectionHandler>;
type ToSwarm = Event;
fn handle_established_inbound_connection(
@ -262,7 +259,7 @@ impl NetworkBehaviour for Behaviour {
"state mismatch"
);
Ok(Either::Right(Either::Right(dummy::ConnectionHandler)))
Ok(Either::Right(dummy::ConnectionHandler))
}
fn handle_established_outbound_connection(
@ -293,12 +290,19 @@ impl NetworkBehaviour for Behaviour {
);
}
return Ok(Either::Right(Either::Left(
handler::direct::Handler::default(),
)));
self.queued_events.extend([
ToSwarm::NotifyHandler {
peer_id: peer,
handler: NotifyHandler::One(relayed_connection_id),
event: Either::Left(handler::relayed::Command::UpgradeFinishedDontKeepAlive),
},
ToSwarm::GenerateEvent(Event::DirectConnectionUpgradeSucceeded {
remote_peer_id: peer,
}),
]);
}
Ok(Either::Right(Either::Right(dummy::ConnectionHandler)))
Ok(Either::Right(dummy::ConnectionHandler))
}
fn on_connection_handler_event(
@ -383,21 +387,7 @@ impl NetworkBehaviour for Behaviour {
.or_default() += 1;
self.queued_events.push_back(ToSwarm::Dial { opts });
}
Either::Right(Either::Left(handler::direct::Event::DirectConnectionEstablished)) => {
self.queued_events.extend([
ToSwarm::NotifyHandler {
peer_id: event_source,
handler: NotifyHandler::One(relayed_connection_id),
event: Either::Left(
handler::relayed::Command::UpgradeFinishedDontKeepAlive,
),
},
ToSwarm::GenerateEvent(Event::DirectConnectionUpgradeSucceeded {
remote_peer_id: event_source,
}),
]);
}
Either::Right(Either::Right(never)) => void::unreachable(never),
Either::Right(never) => void::unreachable(never),
};
}

View File

@ -18,5 +18,4 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
pub(crate) mod direct;
pub(crate) mod relayed;

View File

@ -1,99 +0,0 @@
// Copyright 2021 Protocol Labs.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! [`ConnectionHandler`] handling direct connection upgraded through a relayed connection.
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_swarm::handler::ConnectionEvent;
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, StreamUpgradeError, SubstreamProtocol,
};
use std::task::{Context, Poll};
use void::Void;
#[derive(Debug)]
pub enum Event {
DirectConnectionEstablished,
}
#[derive(Default)]
pub struct Handler {
reported: bool,
}
impl ConnectionHandler for Handler {
type FromBehaviour = void::Void;
type ToBehaviour = Event;
type Error = StreamUpgradeError<std::io::Error>;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = DeniedUpgrade;
type OutboundOpenInfo = Void;
type InboundOpenInfo = ();
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
SubstreamProtocol::new(DeniedUpgrade, ())
}
fn on_behaviour_event(&mut self, _: Self::FromBehaviour) {}
fn connection_keep_alive(&self) -> KeepAlive {
KeepAlive::No
}
fn poll(
&mut self,
_: &mut Context<'_>,
) -> Poll<
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::ToBehaviour,
Self::Error,
>,
> {
if !self.reported {
self.reported = true;
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(
Event::DirectConnectionEstablished,
));
}
Poll::Pending
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(_)
| ConnectionEvent::FullyNegotiatedOutbound(_)
| ConnectionEvent::DialUpgradeError(_)
| ConnectionEvent::ListenUpgradeError(_)
| ConnectionEvent::AddressChange(_)
| ConnectionEvent::LocalProtocolsChange(_)
| ConnectionEvent::RemoteProtocolsChange(_) => {}
}
}
}