mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-22 06:11:34 +00:00
swarm/: Rename ProtocolsHandler to ConnectionHandler (#2527)
A `ProtocolsHandler`, now `ConnectionHandler`, handels a connection, not a protocol. Thus the name `CONNECTIONHandler` is more appropriate. Next to the rename of `ProtocolsHandler` this commit renames the `mod protocols_handler` to `mod handler`. Finally all combinators (e.g. `ProtocolsHandlerSelect`) are renamed appropriately.
This commit is contained in:
@ -31,7 +31,7 @@ use libp2p_core::multiaddr::Multiaddr;
|
||||
use libp2p_core::PeerId;
|
||||
use libp2p_swarm::{
|
||||
dial_opts::{self, DialOpts},
|
||||
DialError, IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
|
||||
DialError, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
|
||||
PollParameters,
|
||||
};
|
||||
use std::collections::{hash_map::Entry, HashMap, HashSet, VecDeque};
|
||||
@ -154,10 +154,10 @@ impl Relay {
|
||||
}
|
||||
|
||||
impl NetworkBehaviour for Relay {
|
||||
type ProtocolsHandler = RelayHandlerProto;
|
||||
type ConnectionHandler = RelayHandlerProto;
|
||||
type OutEvent = ();
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
fn new_handler(&mut self) -> Self::ConnectionHandler {
|
||||
RelayHandlerProto {
|
||||
config: RelayHandlerConfig {
|
||||
connection_idle_timeout: self.config.connection_idle_timeout,
|
||||
@ -301,7 +301,7 @@ impl NetworkBehaviour for Relay {
|
||||
fn inject_dial_failure(
|
||||
&mut self,
|
||||
peer_id: Option<PeerId>,
|
||||
_: Self::ProtocolsHandler,
|
||||
_: Self::ConnectionHandler,
|
||||
error: &DialError,
|
||||
) {
|
||||
if let DialError::DialPeerConditionFalse(
|
||||
@ -352,7 +352,7 @@ impl NetworkBehaviour for Relay {
|
||||
peer: &PeerId,
|
||||
connection: &ConnectionId,
|
||||
_: &ConnectedPoint,
|
||||
_: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
|
||||
_: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
|
||||
remaining_established: usize,
|
||||
) {
|
||||
// Remove connection from the set of connections for the given peer. In case the set is
|
||||
@ -570,7 +570,7 @@ impl NetworkBehaviour for Relay {
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
poll_parameters: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
|
||||
if !self.outbox_to_listeners.is_empty() {
|
||||
let relay_peer_id = self.outbox_to_listeners[0].0;
|
||||
|
||||
|
@ -29,8 +29,8 @@ use libp2p_core::connection::ConnectionId;
|
||||
use libp2p_core::either::{EitherError, EitherOutput};
|
||||
use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId};
|
||||
use libp2p_swarm::{
|
||||
IntoProtocolsHandler, KeepAlive, NegotiatedSubstream, ProtocolsHandler, ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr, SubstreamProtocol,
|
||||
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
|
||||
KeepAlive, NegotiatedSubstream, SubstreamProtocol,
|
||||
};
|
||||
use log::warn;
|
||||
use std::fmt;
|
||||
@ -46,7 +46,7 @@ pub struct RelayHandlerProto {
|
||||
pub config: RelayHandlerConfig,
|
||||
}
|
||||
|
||||
impl IntoProtocolsHandler for RelayHandlerProto {
|
||||
impl IntoConnectionHandler for RelayHandlerProto {
|
||||
type Handler = RelayHandler;
|
||||
|
||||
fn into_handler(self, remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
|
||||
@ -57,7 +57,7 @@ impl IntoProtocolsHandler for RelayHandlerProto {
|
||||
)
|
||||
}
|
||||
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
|
||||
protocol::RelayListen::new()
|
||||
}
|
||||
}
|
||||
@ -118,7 +118,7 @@ pub struct RelayHandler {
|
||||
keep_alive: KeepAlive,
|
||||
/// A pending fatal error that results in the connection being closed.
|
||||
pending_error: Option<
|
||||
ProtocolsHandlerUpgrErr<
|
||||
ConnectionHandlerUpgrErr<
|
||||
EitherError<
|
||||
protocol::RelayListenError,
|
||||
EitherError<protocol::OutgoingRelayReqError, protocol::OutgoingDstReqError>,
|
||||
@ -347,10 +347,10 @@ impl RelayHandler {
|
||||
}
|
||||
}
|
||||
|
||||
impl ProtocolsHandler for RelayHandler {
|
||||
impl ConnectionHandler for RelayHandler {
|
||||
type InEvent = RelayHandlerIn;
|
||||
type OutEvent = RelayHandlerEvent;
|
||||
type Error = ProtocolsHandlerUpgrErr<
|
||||
type Error = ConnectionHandlerUpgrErr<
|
||||
EitherError<
|
||||
protocol::RelayListenError,
|
||||
EitherError<protocol::OutgoingRelayReqError, protocol::OutgoingDstReqError>,
|
||||
@ -478,22 +478,22 @@ impl ProtocolsHandler for RelayHandler {
|
||||
fn inject_listen_upgrade_error(
|
||||
&mut self,
|
||||
_: Self::InboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<protocol::RelayListenError>,
|
||||
error: ConnectionHandlerUpgrErr<protocol::RelayListenError>,
|
||||
) {
|
||||
match error {
|
||||
ProtocolsHandlerUpgrErr::Timeout | ProtocolsHandlerUpgrErr::Timer => {}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Timeout | ConnectionHandlerUpgrErr::Timer => {}
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)) => {}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(upgrade::NegotiationError::ProtocolError(e)),
|
||||
));
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => {
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::A(error)),
|
||||
))
|
||||
}
|
||||
@ -503,7 +503,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
fn inject_dial_upgrade_error(
|
||||
&mut self,
|
||||
open_info: Self::OutboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<
|
||||
error: ConnectionHandlerUpgrErr<
|
||||
EitherError<protocol::OutgoingRelayReqError, protocol::OutgoingDstReqError>,
|
||||
>,
|
||||
) {
|
||||
@ -513,20 +513,20 @@ impl ProtocolsHandler for RelayHandler {
|
||||
request_id,
|
||||
} => {
|
||||
match error {
|
||||
ProtocolsHandlerUpgrErr::Timeout | ProtocolsHandlerUpgrErr::Timer => {}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Timeout | ConnectionHandlerUpgrErr::Timer => {}
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)) => {}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
),
|
||||
));
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
EitherError::A(error),
|
||||
)) => match error {
|
||||
protocol::OutgoingRelayReqError::Decode(_)
|
||||
@ -536,7 +536,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
| protocol::OutgoingRelayReqError::UnexpectedSrcPeerWithStatusType
|
||||
| protocol::OutgoingRelayReqError::UnexpectedDstPeerWithStatusType
|
||||
| protocol::OutgoingRelayReqError::ExpectedStatusType(_) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::B(EitherError::A(error))),
|
||||
));
|
||||
}
|
||||
@ -550,7 +550,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
circuit_relay::Status::HopSrcAddrTooLong
|
||||
| circuit_relay::Status::HopSrcMultiaddrInvalid
|
||||
| circuit_relay::Status::MalformedMessage => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::B(
|
||||
EitherError::A(error),
|
||||
)),
|
||||
@ -574,7 +574,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
}
|
||||
}
|
||||
},
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
EitherError::B(_),
|
||||
)) => {
|
||||
unreachable!("Can not receive an OutgoingDstReqError when dialing a relay.")
|
||||
@ -593,28 +593,28 @@ impl ProtocolsHandler for RelayHandler {
|
||||
..
|
||||
} => {
|
||||
let err_code = match error {
|
||||
ProtocolsHandlerUpgrErr::Timeout | ProtocolsHandlerUpgrErr::Timer => {
|
||||
ConnectionHandlerUpgrErr::Timeout | ConnectionHandlerUpgrErr::Timer => {
|
||||
circuit_relay::Status::HopCantOpenDstStream
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)) => circuit_relay::Status::HopCantSpeakRelay,
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
),
|
||||
));
|
||||
circuit_relay::Status::HopCantSpeakRelay
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
EitherError::A(_),
|
||||
)) => unreachable!(
|
||||
"Can not receive an OutgoingRelayReqError when dialing a destination."
|
||||
),
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
EitherError::B(error),
|
||||
)) => {
|
||||
match error {
|
||||
@ -625,7 +625,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
| protocol::OutgoingDstReqError::UnexpectedSrcPeerWithStatusType
|
||||
| protocol::OutgoingDstReqError::UnexpectedDstPeerWithStatusType
|
||||
| protocol::OutgoingDstReqError::ExpectedStatusType(_) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::B(EitherError::B(
|
||||
error,
|
||||
))),
|
||||
@ -651,7 +651,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
| circuit_relay::Status::HopSrcAddrTooLong
|
||||
| circuit_relay::Status::HopSrcMultiaddrInvalid => {
|
||||
self.pending_error =
|
||||
Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::B(
|
||||
EitherError::B(error),
|
||||
)),
|
||||
@ -663,7 +663,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
| circuit_relay::Status::StopDstMultiaddrInvalid
|
||||
| circuit_relay::Status::MalformedMessage => {
|
||||
self.pending_error =
|
||||
Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::B(
|
||||
EitherError::B(error),
|
||||
)),
|
||||
@ -699,7 +699,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<
|
||||
ConnectionHandlerEvent<
|
||||
Self::OutboundProtocol,
|
||||
Self::OutboundOpenInfo,
|
||||
Self::OutEvent,
|
||||
@ -709,7 +709,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
// Check for a pending (fatal) error.
|
||||
if let Some(err) = self.pending_error.take() {
|
||||
// The handler will not be polled again by the `Swarm`.
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Close(err));
|
||||
return Poll::Ready(ConnectionHandlerEvent::Close(err));
|
||||
}
|
||||
|
||||
// Request the remote to act as a relay.
|
||||
@ -721,7 +721,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
dst_addr,
|
||||
} = self.outgoing_relay_reqs.remove(0);
|
||||
self.outgoing_relay_reqs.shrink_to_fit();
|
||||
return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(
|
||||
upgrade::EitherUpgrade::A(protocol::OutgoingRelayReq::new(
|
||||
src_peer_id,
|
||||
@ -746,7 +746,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
incoming_relay_req,
|
||||
} = self.outgoing_dst_reqs.remove(0);
|
||||
self.outgoing_dst_reqs.shrink_to_fit();
|
||||
return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(
|
||||
upgrade::EitherUpgrade::B(protocol::OutgoingDstReq::new(
|
||||
src_peer_id,
|
||||
@ -772,7 +772,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
relay_peer_id: self.remote_peer_id,
|
||||
relay_addr: self.remote_address.clone(),
|
||||
};
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(event));
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(event));
|
||||
}
|
||||
Poll::Ready(Some(Err(e))) => {
|
||||
log::debug!("Failed to accept destination future: {:?}", e);
|
||||
@ -796,7 +796,7 @@ impl ProtocolsHandler for RelayHandler {
|
||||
// Report the queued events.
|
||||
if !self.queued_events.is_empty() {
|
||||
let event = self.queued_events.remove(0);
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(event));
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(event));
|
||||
}
|
||||
|
||||
while let Poll::Ready(Some(Err(Canceled))) =
|
||||
|
@ -35,10 +35,10 @@ use futures::stream::StreamExt;
|
||||
use libp2p_core::connection::{ConnectedPoint, ConnectionId};
|
||||
use libp2p_core::{Multiaddr, PeerId};
|
||||
use libp2p_swarm::dial_opts::DialOpts;
|
||||
use libp2p_swarm::protocols_handler::DummyProtocolsHandler;
|
||||
use libp2p_swarm::handler::DummyConnectionHandler;
|
||||
use libp2p_swarm::{
|
||||
NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
|
||||
ProtocolsHandlerUpgrErr,
|
||||
ConnectionHandlerUpgrErr, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction,
|
||||
NotifyHandler, PollParameters,
|
||||
};
|
||||
use std::collections::{hash_map, HashMap, VecDeque};
|
||||
use std::io::{Error, IoSlice};
|
||||
@ -60,7 +60,7 @@ pub enum Event {
|
||||
relay_peer_id: PeerId,
|
||||
/// Indicates whether the request replaces an existing reservation.
|
||||
renewal: bool,
|
||||
error: ProtocolsHandlerUpgrErr<outbound_hop::ReservationFailedReason>,
|
||||
error: ConnectionHandlerUpgrErr<outbound_hop::ReservationFailedReason>,
|
||||
},
|
||||
OutboundCircuitEstablished {
|
||||
relay_peer_id: PeerId,
|
||||
@ -68,7 +68,7 @@ pub enum Event {
|
||||
},
|
||||
OutboundCircuitReqFailed {
|
||||
relay_peer_id: PeerId,
|
||||
error: ProtocolsHandlerUpgrErr<outbound_hop::CircuitFailedReason>,
|
||||
error: ConnectionHandlerUpgrErr<outbound_hop::CircuitFailedReason>,
|
||||
},
|
||||
/// An inbound circuit has been established.
|
||||
InboundCircuitEstablished {
|
||||
@ -77,7 +77,7 @@ pub enum Event {
|
||||
},
|
||||
InboundCircuitReqFailed {
|
||||
relay_peer_id: PeerId,
|
||||
error: ProtocolsHandlerUpgrErr<void::Void>,
|
||||
error: ConnectionHandlerUpgrErr<void::Void>,
|
||||
},
|
||||
/// An inbound circuit request has been denied.
|
||||
InboundCircuitReqDenied { src_peer_id: PeerId },
|
||||
@ -116,10 +116,10 @@ impl Client {
|
||||
}
|
||||
|
||||
impl NetworkBehaviour for Client {
|
||||
type ProtocolsHandler = handler::Prototype;
|
||||
type ConnectionHandler = handler::Prototype;
|
||||
type OutEvent = Event;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
fn new_handler(&mut self) -> Self::ConnectionHandler {
|
||||
handler::Prototype::new(self.local_peer_id, None)
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ impl NetworkBehaviour for Client {
|
||||
peer_id: &PeerId,
|
||||
connection_id: &ConnectionId,
|
||||
endpoint: &ConnectedPoint,
|
||||
_handler: Either<handler::Handler, DummyProtocolsHandler>,
|
||||
_handler: Either<handler::Handler, DummyConnectionHandler>,
|
||||
_remaining_established: usize,
|
||||
) {
|
||||
if !endpoint.is_relayed() {
|
||||
@ -231,7 +231,7 @@ impl NetworkBehaviour for Client {
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
_poll_parameters: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
|
||||
if let Some(event) = self.queued_actions.pop_front() {
|
||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
||||
}
|
||||
|
@ -31,12 +31,12 @@ use instant::Instant;
|
||||
use libp2p_core::either::EitherError;
|
||||
use libp2p_core::multiaddr::Protocol;
|
||||
use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId};
|
||||
use libp2p_swarm::protocols_handler::{
|
||||
DummyProtocolsHandler, InboundUpgradeSend, OutboundUpgradeSend, SendWrapper,
|
||||
use libp2p_swarm::handler::{
|
||||
DummyConnectionHandler, InboundUpgradeSend, OutboundUpgradeSend, SendWrapper,
|
||||
};
|
||||
use libp2p_swarm::{
|
||||
IntoProtocolsHandler, KeepAlive, NegotiatedSubstream, ProtocolsHandler, ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr, SubstreamProtocol,
|
||||
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
|
||||
KeepAlive, NegotiatedSubstream, SubstreamProtocol,
|
||||
};
|
||||
use log::debug;
|
||||
use std::collections::VecDeque;
|
||||
@ -79,12 +79,12 @@ pub enum Event {
|
||||
ReservationReqFailed {
|
||||
/// Indicates whether the request replaces an existing reservation.
|
||||
renewal: bool,
|
||||
error: ProtocolsHandlerUpgrErr<outbound_hop::ReservationFailedReason>,
|
||||
error: ConnectionHandlerUpgrErr<outbound_hop::ReservationFailedReason>,
|
||||
},
|
||||
/// An outbound circuit has been established.
|
||||
OutboundCircuitEstablished { limit: Option<protocol::Limit> },
|
||||
OutboundCircuitReqFailed {
|
||||
error: ProtocolsHandlerUpgrErr<outbound_hop::CircuitFailedReason>,
|
||||
error: ConnectionHandlerUpgrErr<outbound_hop::CircuitFailedReason>,
|
||||
},
|
||||
/// An inbound circuit has been established.
|
||||
InboundCircuitEstablished {
|
||||
@ -93,7 +93,7 @@ pub enum Event {
|
||||
},
|
||||
/// An inbound circuit request has failed.
|
||||
InboundCircuitReqFailed {
|
||||
error: ProtocolsHandlerUpgrErr<void::Void>,
|
||||
error: ConnectionHandlerUpgrErr<void::Void>,
|
||||
},
|
||||
/// An inbound circuit request has been denied.
|
||||
InboundCircuitReqDenied { src_peer_id: PeerId },
|
||||
@ -119,8 +119,8 @@ impl Prototype {
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoProtocolsHandler for Prototype {
|
||||
type Handler = Either<Handler, DummyProtocolsHandler>;
|
||||
impl IntoConnectionHandler for Prototype {
|
||||
type Handler = Either<Handler, DummyConnectionHandler>;
|
||||
|
||||
fn into_handler(self, remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
|
||||
if endpoint.is_relayed() {
|
||||
@ -133,7 +133,7 @@ impl IntoProtocolsHandler for Prototype {
|
||||
}
|
||||
|
||||
// Deny all substreams on relayed connection.
|
||||
Either::Right(DummyProtocolsHandler::default())
|
||||
Either::Right(DummyConnectionHandler::default())
|
||||
} else {
|
||||
let mut handler = Handler {
|
||||
remote_peer_id: *remote_peer_id,
|
||||
@ -156,7 +156,7 @@ impl IntoProtocolsHandler for Prototype {
|
||||
}
|
||||
}
|
||||
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
|
||||
upgrade::EitherUpgrade::A(SendWrapper(inbound_stop::Upgrade {}))
|
||||
}
|
||||
}
|
||||
@ -167,7 +167,7 @@ pub struct Handler {
|
||||
remote_addr: Multiaddr,
|
||||
/// A pending fatal error that results in the connection being closed.
|
||||
pending_error: Option<
|
||||
ProtocolsHandlerUpgrErr<
|
||||
ConnectionHandlerUpgrErr<
|
||||
EitherError<inbound_stop::FatalUpgradeError, outbound_hop::FatalUpgradeError>,
|
||||
>,
|
||||
>,
|
||||
@ -176,11 +176,11 @@ pub struct Handler {
|
||||
|
||||
/// Queue of events to return when polled.
|
||||
queued_events: VecDeque<
|
||||
ProtocolsHandlerEvent<
|
||||
<Self as ProtocolsHandler>::OutboundProtocol,
|
||||
<Self as ProtocolsHandler>::OutboundOpenInfo,
|
||||
<Self as ProtocolsHandler>::OutEvent,
|
||||
<Self as ProtocolsHandler>::Error,
|
||||
ConnectionHandlerEvent<
|
||||
<Self as ConnectionHandler>::OutboundProtocol,
|
||||
<Self as ConnectionHandler>::OutboundOpenInfo,
|
||||
<Self as ConnectionHandler>::OutEvent,
|
||||
<Self as ConnectionHandler>::Error,
|
||||
>,
|
||||
>,
|
||||
|
||||
@ -201,14 +201,14 @@ pub struct Handler {
|
||||
/// Futures that try to send errors to the transport.
|
||||
///
|
||||
/// We may drop errors if this handler ends up in a terminal state (by returning
|
||||
/// [`ProtocolsHandlerEvent::Close`]).
|
||||
/// [`ConnectionHandlerEvent::Close`]).
|
||||
send_error_futs: FuturesUnordered<BoxFuture<'static, ()>>,
|
||||
}
|
||||
|
||||
impl ProtocolsHandler for Handler {
|
||||
impl ConnectionHandler for Handler {
|
||||
type InEvent = In;
|
||||
type OutEvent = Event;
|
||||
type Error = ProtocolsHandlerUpgrErr<
|
||||
type Error = ConnectionHandlerUpgrErr<
|
||||
EitherError<inbound_stop::FatalUpgradeError, outbound_hop::FatalUpgradeError>,
|
||||
>;
|
||||
type InboundProtocol = inbound_stop::Upgrade;
|
||||
@ -242,7 +242,7 @@ impl ProtocolsHandler for Handler {
|
||||
relay_addr: self.remote_addr.clone(),
|
||||
});
|
||||
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::InboundCircuitEstablished {
|
||||
src_peer_id: self.remote_peer_id,
|
||||
limit,
|
||||
@ -285,7 +285,7 @@ impl ProtocolsHandler for Handler {
|
||||
);
|
||||
|
||||
self.queued_events
|
||||
.push_back(ProtocolsHandlerEvent::Custom(event));
|
||||
.push_back(ConnectionHandlerEvent::Custom(event));
|
||||
}
|
||||
|
||||
// Outbound circuit
|
||||
@ -305,7 +305,7 @@ impl ProtocolsHandler for Handler {
|
||||
))) {
|
||||
Ok(()) => {
|
||||
self.alive_lend_out_substreams.push(rx);
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::OutboundCircuitEstablished { limit },
|
||||
));
|
||||
}
|
||||
@ -325,7 +325,7 @@ impl ProtocolsHandler for Handler {
|
||||
match event {
|
||||
In::Reserve { to_listener } => {
|
||||
self.queued_events
|
||||
.push_back(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
.push_back(ConnectionHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(
|
||||
outbound_hop::Upgrade::Reserve,
|
||||
OutboundOpenInfo::Reserve { to_listener },
|
||||
@ -337,7 +337,7 @@ impl ProtocolsHandler for Handler {
|
||||
dst_peer_id,
|
||||
} => {
|
||||
self.queued_events
|
||||
.push_back(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
.push_back(ConnectionHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(
|
||||
outbound_hop::Upgrade::Connect { dst_peer_id },
|
||||
OutboundOpenInfo::Connect { send_back },
|
||||
@ -350,35 +350,35 @@ impl ProtocolsHandler for Handler {
|
||||
fn inject_listen_upgrade_error(
|
||||
&mut self,
|
||||
_: Self::InboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
|
||||
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
|
||||
) {
|
||||
let non_fatal_error = match error {
|
||||
ProtocolsHandlerUpgrErr::Timeout => ProtocolsHandlerUpgrErr::Timeout,
|
||||
ProtocolsHandlerUpgrErr::Timer => ProtocolsHandlerUpgrErr::Timer,
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Timeout => ConnectionHandlerUpgrErr::Timeout,
|
||||
ConnectionHandlerUpgrErr::Timer => ConnectionHandlerUpgrErr::Timer,
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)) => ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
)) => ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)),
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(upgrade::NegotiationError::ProtocolError(e)),
|
||||
));
|
||||
return;
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
inbound_stop::UpgradeError::Fatal(error),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::A(error)),
|
||||
));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::InboundCircuitReqFailed {
|
||||
error: non_fatal_error,
|
||||
},
|
||||
@ -388,38 +388,38 @@ impl ProtocolsHandler for Handler {
|
||||
fn inject_dial_upgrade_error(
|
||||
&mut self,
|
||||
open_info: Self::OutboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
|
||||
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
|
||||
) {
|
||||
match open_info {
|
||||
OutboundOpenInfo::Reserve { mut to_listener } => {
|
||||
let non_fatal_error = match error {
|
||||
ProtocolsHandlerUpgrErr::Timeout => ProtocolsHandlerUpgrErr::Timeout,
|
||||
ProtocolsHandlerUpgrErr::Timer => ProtocolsHandlerUpgrErr::Timer,
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Timeout => ConnectionHandlerUpgrErr::Timeout,
|
||||
ConnectionHandlerUpgrErr::Timer => ConnectionHandlerUpgrErr::Timer,
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)) => ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
)) => ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)),
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
),
|
||||
));
|
||||
return;
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => {
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => {
|
||||
match error {
|
||||
outbound_hop::UpgradeError::Fatal(error) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::B(error)),
|
||||
));
|
||||
return;
|
||||
}
|
||||
outbound_hop::UpgradeError::ReservationFailed(error) => {
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
error,
|
||||
))
|
||||
}
|
||||
@ -447,7 +447,7 @@ impl ProtocolsHandler for Handler {
|
||||
}
|
||||
|
||||
let renewal = self.reservation.failed();
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::ReservationReqFailed {
|
||||
renewal,
|
||||
error: non_fatal_error,
|
||||
@ -456,33 +456,33 @@ impl ProtocolsHandler for Handler {
|
||||
}
|
||||
OutboundOpenInfo::Connect { send_back } => {
|
||||
let non_fatal_error = match error {
|
||||
ProtocolsHandlerUpgrErr::Timeout => ProtocolsHandlerUpgrErr::Timeout,
|
||||
ProtocolsHandlerUpgrErr::Timer => ProtocolsHandlerUpgrErr::Timer,
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Timeout => ConnectionHandlerUpgrErr::Timeout,
|
||||
ConnectionHandlerUpgrErr::Timer => ConnectionHandlerUpgrErr::Timer,
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)) => ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
)) => ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)),
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
),
|
||||
));
|
||||
return;
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => {
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => {
|
||||
match error {
|
||||
outbound_hop::UpgradeError::Fatal(error) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::B(error)),
|
||||
));
|
||||
return;
|
||||
}
|
||||
outbound_hop::UpgradeError::CircuitFailed(error) => {
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
error,
|
||||
))
|
||||
}
|
||||
@ -497,7 +497,7 @@ impl ProtocolsHandler for Handler {
|
||||
|
||||
let _ = send_back.send(Err(()));
|
||||
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::OutboundCircuitReqFailed {
|
||||
error: non_fatal_error,
|
||||
},
|
||||
@ -514,7 +514,7 @@ impl ProtocolsHandler for Handler {
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<
|
||||
ConnectionHandlerEvent<
|
||||
Self::OutboundProtocol,
|
||||
Self::OutboundOpenInfo,
|
||||
Self::OutEvent,
|
||||
@ -524,7 +524,7 @@ impl ProtocolsHandler for Handler {
|
||||
// Check for a pending (fatal) error.
|
||||
if let Some(err) = self.pending_error.take() {
|
||||
// The handler will not be polled again by the `Swarm`.
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Close(err));
|
||||
return Poll::Ready(ConnectionHandlerEvent::Close(err));
|
||||
}
|
||||
|
||||
// Return queued events.
|
||||
@ -533,7 +533,7 @@ impl ProtocolsHandler for Handler {
|
||||
}
|
||||
|
||||
if let Poll::Ready(Some(protocol)) = self.reservation.poll(cx) {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol });
|
||||
return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol });
|
||||
}
|
||||
|
||||
// Deny incoming circuit requests.
|
||||
@ -541,12 +541,12 @@ impl ProtocolsHandler for Handler {
|
||||
{
|
||||
match result {
|
||||
Ok(()) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::InboundCircuitReqDenied { src_peer_id },
|
||||
))
|
||||
}
|
||||
Err(error) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::InboundCircuitReqDenyFailed { src_peer_id, error },
|
||||
))
|
||||
}
|
||||
|
@ -30,10 +30,10 @@ use instant::Instant;
|
||||
use libp2p_core::connection::{ConnectedPoint, ConnectionId};
|
||||
use libp2p_core::multiaddr::Protocol;
|
||||
use libp2p_core::PeerId;
|
||||
use libp2p_swarm::protocols_handler::DummyProtocolsHandler;
|
||||
use libp2p_swarm::handler::DummyConnectionHandler;
|
||||
use libp2p_swarm::{
|
||||
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
|
||||
ProtocolsHandlerUpgrErr,
|
||||
ConnectionHandlerUpgrErr, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
|
||||
PollParameters,
|
||||
};
|
||||
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
|
||||
use std::num::NonZeroU32;
|
||||
@ -151,7 +151,7 @@ pub enum Event {
|
||||
ReservationTimedOut { src_peer_id: PeerId },
|
||||
CircuitReqReceiveFailed {
|
||||
src_peer_id: PeerId,
|
||||
error: ProtocolsHandlerUpgrErr<void::Void>,
|
||||
error: ConnectionHandlerUpgrErr<void::Void>,
|
||||
},
|
||||
/// An inbound circuit request has been denied.
|
||||
CircuitReqDenied {
|
||||
@ -173,7 +173,7 @@ pub enum Event {
|
||||
CircuitReqOutboundConnectFailed {
|
||||
src_peer_id: PeerId,
|
||||
dst_peer_id: PeerId,
|
||||
error: ProtocolsHandlerUpgrErr<outbound_stop::CircuitFailedReason>,
|
||||
error: ConnectionHandlerUpgrErr<outbound_stop::CircuitFailedReason>,
|
||||
},
|
||||
/// Accepting an inbound circuit request failed.
|
||||
CircuitReqAcceptFailed {
|
||||
@ -216,10 +216,10 @@ impl Relay {
|
||||
}
|
||||
|
||||
impl NetworkBehaviour for Relay {
|
||||
type ProtocolsHandler = handler::Prototype;
|
||||
type ConnectionHandler = handler::Prototype;
|
||||
type OutEvent = Event;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
fn new_handler(&mut self) -> Self::ConnectionHandler {
|
||||
handler::Prototype {
|
||||
config: handler::Config {
|
||||
reservation_duration: self.config.reservation_duration,
|
||||
@ -234,7 +234,7 @@ impl NetworkBehaviour for Relay {
|
||||
peer: &PeerId,
|
||||
connection: &ConnectionId,
|
||||
_: &ConnectedPoint,
|
||||
_handler: Either<handler::Handler, DummyProtocolsHandler>,
|
||||
_handler: Either<handler::Handler, DummyConnectionHandler>,
|
||||
_remaining_established: usize,
|
||||
) {
|
||||
if let hash_map::Entry::Occupied(mut peer) = self.reservations.entry(*peer) {
|
||||
@ -283,7 +283,7 @@ impl NetworkBehaviour for Relay {
|
||||
|
||||
assert!(
|
||||
!endpoint.is_relayed(),
|
||||
"`DummyProtocolsHandler` handles relayed connections. It \
|
||||
"`DummyConnectionHandler` handles relayed connections. It \
|
||||
denies all inbound substreams."
|
||||
);
|
||||
|
||||
@ -410,7 +410,7 @@ impl NetworkBehaviour for Relay {
|
||||
|
||||
assert!(
|
||||
!endpoint.is_relayed(),
|
||||
"`DummyProtocolsHandler` handles relayed connections. It \
|
||||
"`DummyConnectionHandler` handles relayed connections. It \
|
||||
denies all inbound substreams."
|
||||
);
|
||||
|
||||
@ -622,7 +622,7 @@ impl NetworkBehaviour for Relay {
|
||||
&mut self,
|
||||
_cx: &mut Context<'_>,
|
||||
poll_parameters: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
|
||||
if let Some(action) = self.queued_actions.pop_front() {
|
||||
return Poll::Ready(action.build(poll_parameters));
|
||||
}
|
||||
|
@ -33,11 +33,11 @@ use instant::Instant;
|
||||
use libp2p_core::connection::ConnectionId;
|
||||
use libp2p_core::either::EitherError;
|
||||
use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId};
|
||||
use libp2p_swarm::protocols_handler::{DummyProtocolsHandler, SendWrapper};
|
||||
use libp2p_swarm::protocols_handler::{InboundUpgradeSend, OutboundUpgradeSend};
|
||||
use libp2p_swarm::handler::{DummyConnectionHandler, SendWrapper};
|
||||
use libp2p_swarm::handler::{InboundUpgradeSend, OutboundUpgradeSend};
|
||||
use libp2p_swarm::{
|
||||
IntoProtocolsHandler, KeepAlive, NegotiatedSubstream, ProtocolsHandler, ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr, SubstreamProtocol,
|
||||
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
|
||||
KeepAlive, NegotiatedSubstream, SubstreamProtocol,
|
||||
};
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt;
|
||||
@ -167,7 +167,7 @@ pub enum Event {
|
||||
},
|
||||
/// Receiving an inbound circuit request failed.
|
||||
CircuitReqReceiveFailed {
|
||||
error: ProtocolsHandlerUpgrErr<void::Void>,
|
||||
error: ConnectionHandlerUpgrErr<void::Void>,
|
||||
},
|
||||
/// An inbound circuit request has been denied.
|
||||
CircuitReqDenied {
|
||||
@ -209,7 +209,7 @@ pub enum Event {
|
||||
src_connection_id: ConnectionId,
|
||||
inbound_circuit_req: inbound_hop::CircuitReq,
|
||||
status: Status,
|
||||
error: ProtocolsHandlerUpgrErr<outbound_stop::CircuitFailedReason>,
|
||||
error: ConnectionHandlerUpgrErr<outbound_stop::CircuitFailedReason>,
|
||||
},
|
||||
/// An inbound circuit has closed.
|
||||
CircuitClosed {
|
||||
@ -341,13 +341,13 @@ pub struct Prototype {
|
||||
pub config: Config,
|
||||
}
|
||||
|
||||
impl IntoProtocolsHandler for Prototype {
|
||||
type Handler = Either<Handler, DummyProtocolsHandler>;
|
||||
impl IntoConnectionHandler for Prototype {
|
||||
type Handler = Either<Handler, DummyConnectionHandler>;
|
||||
|
||||
fn into_handler(self, _remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
|
||||
if endpoint.is_relayed() {
|
||||
// Deny all substreams on relayed connection.
|
||||
Either::Right(DummyProtocolsHandler::default())
|
||||
Either::Right(DummyConnectionHandler::default())
|
||||
} else {
|
||||
Either::Left(Handler {
|
||||
endpoint: endpoint.clone(),
|
||||
@ -366,7 +366,7 @@ impl IntoProtocolsHandler for Prototype {
|
||||
}
|
||||
}
|
||||
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
|
||||
upgrade::EitherUpgrade::A(SendWrapper(inbound_hop::Upgrade {
|
||||
reservation_duration: self.config.reservation_duration,
|
||||
max_circuit_duration: self.config.max_circuit_duration,
|
||||
@ -375,7 +375,7 @@ impl IntoProtocolsHandler for Prototype {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`ProtocolsHandler`] that manages substreams for a relay on a single
|
||||
/// [`ConnectionHandler`] that manages substreams for a relay on a single
|
||||
/// connection with a peer.
|
||||
pub struct Handler {
|
||||
endpoint: ConnectedPoint,
|
||||
@ -385,17 +385,17 @@ pub struct Handler {
|
||||
|
||||
/// Queue of events to return when polled.
|
||||
queued_events: VecDeque<
|
||||
ProtocolsHandlerEvent<
|
||||
<Self as ProtocolsHandler>::OutboundProtocol,
|
||||
<Self as ProtocolsHandler>::OutboundOpenInfo,
|
||||
<Self as ProtocolsHandler>::OutEvent,
|
||||
<Self as ProtocolsHandler>::Error,
|
||||
ConnectionHandlerEvent<
|
||||
<Self as ConnectionHandler>::OutboundProtocol,
|
||||
<Self as ConnectionHandler>::OutboundOpenInfo,
|
||||
<Self as ConnectionHandler>::OutEvent,
|
||||
<Self as ConnectionHandler>::Error,
|
||||
>,
|
||||
>,
|
||||
|
||||
/// A pending fatal error that results in the connection being closed.
|
||||
pending_error: Option<
|
||||
ProtocolsHandlerUpgrErr<
|
||||
ConnectionHandlerUpgrErr<
|
||||
EitherError<inbound_hop::FatalUpgradeError, outbound_stop::FatalUpgradeError>,
|
||||
>,
|
||||
>,
|
||||
@ -429,10 +429,10 @@ pub struct Handler {
|
||||
|
||||
type Futures<T> = FuturesUnordered<BoxFuture<'static, T>>;
|
||||
|
||||
impl ProtocolsHandler for Handler {
|
||||
impl ConnectionHandler for Handler {
|
||||
type InEvent = In;
|
||||
type OutEvent = Event;
|
||||
type Error = ProtocolsHandlerUpgrErr<
|
||||
type Error = ConnectionHandlerUpgrErr<
|
||||
EitherError<inbound_hop::FatalUpgradeError, outbound_stop::FatalUpgradeError>,
|
||||
>;
|
||||
type InboundProtocol = inbound_hop::Upgrade;
|
||||
@ -458,7 +458,7 @@ impl ProtocolsHandler for Handler {
|
||||
) {
|
||||
match request {
|
||||
inbound_hop::Req::Reserve(inbound_reservation_req) => {
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::ReservationReqReceived {
|
||||
inbound_reservation_req,
|
||||
endpoint: self.endpoint.clone(),
|
||||
@ -467,7 +467,7 @@ impl ProtocolsHandler for Handler {
|
||||
));
|
||||
}
|
||||
inbound_hop::Req::Connect(inbound_circuit_req) => {
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::CircuitReqReceived {
|
||||
inbound_circuit_req,
|
||||
endpoint: self.endpoint.clone(),
|
||||
@ -493,7 +493,7 @@ impl ProtocolsHandler for Handler {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
self.alive_lend_out_substreams.push(rx);
|
||||
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::OutboundConnectNegotiated {
|
||||
circuit_id,
|
||||
src_peer_id,
|
||||
@ -530,7 +530,7 @@ impl ProtocolsHandler for Handler {
|
||||
src_connection_id,
|
||||
} => {
|
||||
self.queued_events
|
||||
.push_back(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
.push_back(ConnectionHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(
|
||||
outbound_stop::Upgrade {
|
||||
relay_peer_id,
|
||||
@ -589,35 +589,35 @@ impl ProtocolsHandler for Handler {
|
||||
fn inject_listen_upgrade_error(
|
||||
&mut self,
|
||||
_: Self::InboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
|
||||
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
|
||||
) {
|
||||
let non_fatal_error = match error {
|
||||
ProtocolsHandlerUpgrErr::Timeout => ProtocolsHandlerUpgrErr::Timeout,
|
||||
ProtocolsHandlerUpgrErr::Timer => ProtocolsHandlerUpgrErr::Timer,
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Timeout => ConnectionHandlerUpgrErr::Timeout,
|
||||
ConnectionHandlerUpgrErr::Timer => ConnectionHandlerUpgrErr::Timer,
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)) => ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
)) => ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)),
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(upgrade::NegotiationError::ProtocolError(e)),
|
||||
));
|
||||
return;
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(
|
||||
inbound_hop::UpgradeError::Fatal(error),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::A(error)),
|
||||
));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::CircuitReqReceiveFailed {
|
||||
error: non_fatal_error,
|
||||
},
|
||||
@ -627,36 +627,36 @@ impl ProtocolsHandler for Handler {
|
||||
fn inject_dial_upgrade_error(
|
||||
&mut self,
|
||||
open_info: Self::OutboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
|
||||
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
|
||||
) {
|
||||
let (non_fatal_error, status) = match error {
|
||||
ProtocolsHandlerUpgrErr::Timeout => {
|
||||
(ProtocolsHandlerUpgrErr::Timeout, Status::ConnectionFailed)
|
||||
ConnectionHandlerUpgrErr::Timeout => {
|
||||
(ConnectionHandlerUpgrErr::Timeout, Status::ConnectionFailed)
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Timer => {
|
||||
(ProtocolsHandlerUpgrErr::Timer, Status::ConnectionFailed)
|
||||
ConnectionHandlerUpgrErr::Timer => {
|
||||
(ConnectionHandlerUpgrErr::Timer, Status::ConnectionFailed)
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::Failed,
|
||||
)) => {
|
||||
// The remote has previously done a reservation. Doing a reservation but not
|
||||
// supporting the stop protocol is pointless, thus disconnecting.
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(upgrade::NegotiationError::Failed),
|
||||
));
|
||||
return;
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
|
||||
upgrade::NegotiationError::ProtocolError(e),
|
||||
)) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Select(upgrade::NegotiationError::ProtocolError(e)),
|
||||
));
|
||||
return;
|
||||
}
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => match error {
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => match error {
|
||||
outbound_stop::UpgradeError::Fatal(error) => {
|
||||
self.pending_error = Some(ProtocolsHandlerUpgrErr::Upgrade(
|
||||
self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade(
|
||||
upgrade::UpgradeError::Apply(EitherError::B(error)),
|
||||
));
|
||||
return;
|
||||
@ -671,7 +671,7 @@ impl ProtocolsHandler for Handler {
|
||||
}
|
||||
};
|
||||
(
|
||||
ProtocolsHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)),
|
||||
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)),
|
||||
status,
|
||||
)
|
||||
}
|
||||
@ -685,7 +685,7 @@ impl ProtocolsHandler for Handler {
|
||||
src_connection_id,
|
||||
} = open_info;
|
||||
|
||||
self.queued_events.push_back(ProtocolsHandlerEvent::Custom(
|
||||
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
|
||||
Event::OutboundConnectNegotiationFailed {
|
||||
circuit_id,
|
||||
src_peer_id,
|
||||
@ -705,7 +705,7 @@ impl ProtocolsHandler for Handler {
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<
|
||||
ConnectionHandlerEvent<
|
||||
Self::OutboundProtocol,
|
||||
Self::OutboundOpenInfo,
|
||||
Self::OutEvent,
|
||||
@ -715,7 +715,7 @@ impl ProtocolsHandler for Handler {
|
||||
// Check for a pending (fatal) error.
|
||||
if let Some(err) = self.pending_error.take() {
|
||||
// The handler will not be polled again by the `Swarm`.
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Close(err));
|
||||
return Poll::Ready(ConnectionHandlerEvent::Close(err));
|
||||
}
|
||||
|
||||
// Return queued events.
|
||||
@ -728,14 +728,14 @@ impl ProtocolsHandler for Handler {
|
||||
{
|
||||
match result {
|
||||
Ok(()) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Event::CircuitClosed {
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitClosed {
|
||||
circuit_id,
|
||||
dst_peer_id,
|
||||
error: None,
|
||||
}))
|
||||
}
|
||||
Err(e) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Event::CircuitClosed {
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitClosed {
|
||||
circuit_id,
|
||||
dst_peer_id,
|
||||
error: Some(e),
|
||||
@ -751,12 +751,12 @@ impl ProtocolsHandler for Handler {
|
||||
.active_reservation
|
||||
.replace(Delay::new(self.config.reservation_duration))
|
||||
.is_some();
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::ReservationReqAccepted { renewed },
|
||||
));
|
||||
}
|
||||
Err(error) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::ReservationReqAcceptFailed { error },
|
||||
));
|
||||
}
|
||||
@ -766,12 +766,12 @@ impl ProtocolsHandler for Handler {
|
||||
if let Poll::Ready(Some(result)) = self.reservation_deny_futures.poll_next_unpin(cx) {
|
||||
match result {
|
||||
Ok(()) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::ReservationReqDenied {},
|
||||
))
|
||||
}
|
||||
Err(error) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::ReservationReqDenyFailed { error },
|
||||
));
|
||||
}
|
||||
@ -819,13 +819,15 @@ impl ProtocolsHandler for Handler {
|
||||
|
||||
self.circuits.push(circuit);
|
||||
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Event::CircuitReqAccepted {
|
||||
circuit_id,
|
||||
dst_peer_id,
|
||||
}));
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::CircuitReqAccepted {
|
||||
circuit_id,
|
||||
dst_peer_id,
|
||||
},
|
||||
));
|
||||
}
|
||||
Err((circuit_id, dst_peer_id, error)) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::CircuitReqAcceptFailed {
|
||||
circuit_id,
|
||||
dst_peer_id,
|
||||
@ -841,13 +843,13 @@ impl ProtocolsHandler for Handler {
|
||||
{
|
||||
match result {
|
||||
Ok(()) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Event::CircuitReqDenied {
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitReqDenied {
|
||||
circuit_id,
|
||||
dst_peer_id,
|
||||
}));
|
||||
}
|
||||
Err(error) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::CircuitReqDenyFailed {
|
||||
circuit_id,
|
||||
dst_peer_id,
|
||||
@ -868,7 +870,9 @@ impl ProtocolsHandler for Handler {
|
||||
.map(|fut| fut.poll_unpin(cx))
|
||||
{
|
||||
self.active_reservation = None;
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Event::ReservationTimedOut {}));
|
||||
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
||||
Event::ReservationTimedOut {},
|
||||
));
|
||||
}
|
||||
|
||||
if self.reservation_accept_futures.is_empty()
|
||||
|
Reference in New Issue
Block a user