| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| use crate::behaviour::MAX_NUMBER_OF_UPGRADE_ATTEMPTS; |
| use crate::{protocol, PROTOCOL_NAME}; |
| use either::Either; |
| use futures::future; |
| use libp2p_core::multiaddr::Multiaddr; |
| use libp2p_core::upgrade::{DeniedUpgrade, ReadyUpgrade}; |
| use libp2p_core::ConnectedPoint; |
| use libp2p_swarm::handler::{ |
| ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, |
| ListenUpgradeError, |
| }; |
| use libp2p_swarm::{ |
| ConnectionHandler, ConnectionHandlerEvent, StreamProtocol, StreamUpgradeError, |
| SubstreamProtocol, |
| }; |
| use protocol::{inbound, outbound}; |
| use std::collections::VecDeque; |
| use std::io; |
| use std::task::{Context, Poll}; |
| use std::time::Duration; |
|
|
| #[derive(Debug)] |
| pub enum Command { |
| Connect, |
| } |
|
|
| #[derive(Debug)] |
| pub enum Event { |
| InboundConnectNegotiated { remote_addrs: Vec<Multiaddr> }, |
| OutboundConnectNegotiated { remote_addrs: Vec<Multiaddr> }, |
| InboundConnectFailed { error: inbound::Error }, |
| OutboundConnectFailed { error: outbound::Error }, |
| } |
|
|
| pub struct Handler { |
| endpoint: ConnectedPoint, |
| |
| queued_events: VecDeque< |
| ConnectionHandlerEvent< |
| <Self as ConnectionHandler>::OutboundProtocol, |
| <Self as ConnectionHandler>::OutboundOpenInfo, |
| <Self as ConnectionHandler>::ToBehaviour, |
| >, |
| >, |
|
|
| |
| inbound_stream: futures_bounded::FuturesSet<Result<Vec<Multiaddr>, inbound::Error>>, |
|
|
| |
| outbound_stream: futures_bounded::FuturesSet<Result<Vec<Multiaddr>, outbound::Error>>, |
|
|
| |
| holepunch_candidates: Vec<Multiaddr>, |
|
|
| attempts: u8, |
| } |
|
|
| impl Handler { |
| pub fn new(endpoint: ConnectedPoint, holepunch_candidates: Vec<Multiaddr>) -> Self { |
| Self { |
| endpoint, |
| queued_events: Default::default(), |
| inbound_stream: futures_bounded::FuturesSet::new(Duration::from_secs(10), 1), |
| outbound_stream: futures_bounded::FuturesSet::new(Duration::from_secs(10), 1), |
| holepunch_candidates, |
| attempts: 0, |
| } |
| } |
|
|
| fn on_fully_negotiated_inbound( |
| &mut self, |
| FullyNegotiatedInbound { |
| protocol: output, .. |
| }: FullyNegotiatedInbound< |
| <Self as ConnectionHandler>::InboundProtocol, |
| <Self as ConnectionHandler>::InboundOpenInfo, |
| >, |
| ) { |
| match output { |
| future::Either::Left(stream) => { |
| if self |
| .inbound_stream |
| .try_push(inbound::handshake( |
| stream, |
| self.holepunch_candidates.clone(), |
| )) |
| .is_err() |
| { |
| tracing::warn!( |
| "New inbound connect stream while still upgrading previous one. Replacing previous with new.", |
| ); |
| } |
| self.attempts += 1; |
| } |
| |
| future::Either::Right(output) => void::unreachable(output), |
| } |
| } |
|
|
| fn on_fully_negotiated_outbound( |
| &mut self, |
| FullyNegotiatedOutbound { |
| protocol: stream, .. |
| }: FullyNegotiatedOutbound< |
| <Self as ConnectionHandler>::OutboundProtocol, |
| <Self as ConnectionHandler>::OutboundOpenInfo, |
| >, |
| ) { |
| assert!( |
| self.endpoint.is_listener(), |
| "A connection dialer never initiates a connection upgrade." |
| ); |
| if self |
| .outbound_stream |
| .try_push(outbound::handshake( |
| stream, |
| self.holepunch_candidates.clone(), |
| )) |
| .is_err() |
| { |
| tracing::warn!( |
| "New outbound connect stream while still upgrading previous one. Replacing previous with new.", |
| ); |
| } |
| } |
|
|
| fn on_listen_upgrade_error( |
| &mut self, |
| ListenUpgradeError { error, .. }: ListenUpgradeError< |
| <Self as ConnectionHandler>::InboundOpenInfo, |
| <Self as ConnectionHandler>::InboundProtocol, |
| >, |
| ) { |
| void::unreachable(error.into_inner()); |
| } |
|
|
| fn on_dial_upgrade_error( |
| &mut self, |
| DialUpgradeError { error, .. }: DialUpgradeError< |
| <Self as ConnectionHandler>::OutboundOpenInfo, |
| <Self as ConnectionHandler>::OutboundProtocol, |
| >, |
| ) { |
| let error = match error { |
| StreamUpgradeError::Apply(v) => void::unreachable(v), |
| StreamUpgradeError::NegotiationFailed => outbound::Error::Unsupported, |
| StreamUpgradeError::Io(e) => outbound::Error::Io(e), |
| StreamUpgradeError::Timeout => outbound::Error::Io(io::ErrorKind::TimedOut.into()), |
| }; |
|
|
| self.queued_events |
| .push_back(ConnectionHandlerEvent::NotifyBehaviour( |
| Event::OutboundConnectFailed { error }, |
| )) |
| } |
| } |
|
|
| impl ConnectionHandler for Handler { |
| type FromBehaviour = Command; |
| type ToBehaviour = Event; |
| type InboundProtocol = Either<ReadyUpgrade<StreamProtocol>, DeniedUpgrade>; |
| type OutboundProtocol = ReadyUpgrade<StreamProtocol>; |
| type OutboundOpenInfo = (); |
| type InboundOpenInfo = (); |
|
|
| fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> { |
| match self.endpoint { |
| ConnectedPoint::Dialer { .. } => { |
| SubstreamProtocol::new(Either::Left(ReadyUpgrade::new(PROTOCOL_NAME)), ()) |
| } |
| ConnectedPoint::Listener { .. } => { |
| |
| |
| |
| |
| |
| SubstreamProtocol::new(Either::Right(DeniedUpgrade), ()) |
| } |
| } |
| } |
|
|
| fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { |
| match event { |
| Command::Connect => { |
| self.queued_events |
| .push_back(ConnectionHandlerEvent::OutboundSubstreamRequest { |
| protocol: SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ()), |
| }); |
| self.attempts += 1; |
| } |
| } |
| } |
|
|
| fn connection_keep_alive(&self) -> bool { |
| if self.attempts < MAX_NUMBER_OF_UPGRADE_ATTEMPTS { |
| return true; |
| } |
|
|
| false |
| } |
|
|
| #[tracing::instrument(level = "trace", name = "ConnectionHandler::poll", skip(self, cx))] |
| fn poll( |
| &mut self, |
| cx: &mut Context<'_>, |
| ) -> Poll< |
| ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>, |
| > { |
| |
| if let Some(event) = self.queued_events.pop_front() { |
| return Poll::Ready(event); |
| } |
|
|
| match self.inbound_stream.poll_unpin(cx) { |
| Poll::Ready(Ok(Ok(addresses))) => { |
| return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( |
| Event::InboundConnectNegotiated { |
| remote_addrs: addresses, |
| }, |
| )) |
| } |
| Poll::Ready(Ok(Err(error))) => { |
| return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( |
| Event::InboundConnectFailed { error }, |
| )) |
| } |
| Poll::Ready(Err(futures_bounded::Timeout { .. })) => { |
| return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( |
| Event::InboundConnectFailed { |
| error: inbound::Error::Io(io::ErrorKind::TimedOut.into()), |
| }, |
| )) |
| } |
| Poll::Pending => {} |
| } |
|
|
| match self.outbound_stream.poll_unpin(cx) { |
| Poll::Ready(Ok(Ok(addresses))) => { |
| return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( |
| Event::OutboundConnectNegotiated { |
| remote_addrs: addresses, |
| }, |
| )) |
| } |
| Poll::Ready(Ok(Err(error))) => { |
| return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( |
| Event::OutboundConnectFailed { error }, |
| )) |
| } |
| Poll::Ready(Err(futures_bounded::Timeout { .. })) => { |
| return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( |
| Event::OutboundConnectFailed { |
| error: outbound::Error::Io(io::ErrorKind::TimedOut.into()), |
| }, |
| )) |
| } |
| Poll::Pending => {} |
| } |
|
|
| Poll::Pending |
| } |
|
|
| fn on_connection_event( |
| &mut self, |
| event: ConnectionEvent< |
| Self::InboundProtocol, |
| Self::OutboundProtocol, |
| Self::InboundOpenInfo, |
| Self::OutboundOpenInfo, |
| >, |
| ) { |
| match event { |
| ConnectionEvent::FullyNegotiatedInbound(fully_negotiated_inbound) => { |
| self.on_fully_negotiated_inbound(fully_negotiated_inbound) |
| } |
| ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => { |
| self.on_fully_negotiated_outbound(fully_negotiated_outbound) |
| } |
| ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => { |
| self.on_listen_upgrade_error(listen_upgrade_error) |
| } |
| ConnectionEvent::DialUpgradeError(dial_upgrade_error) => { |
| self.on_dial_upgrade_error(dial_upgrade_error) |
| } |
| _ => {} |
| } |
| } |
| } |
|
|