libp2p_swarm/
dummy.rs

1use std::{
2    convert::Infallible,
3    task::{Context, Poll},
4};
5
6use libp2p_core::{transport::PortUse, upgrade::DeniedUpgrade, Endpoint, Multiaddr};
7use libp2p_identity::PeerId;
8
9use crate::{
10    behaviour::{FromSwarm, NetworkBehaviour, ToSwarm},
11    connection::ConnectionId,
12    handler::{ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound},
13    ConnectionDenied, ConnectionHandlerEvent, StreamUpgradeError, SubstreamProtocol, THandler,
14    THandlerInEvent, THandlerOutEvent,
15};
16
17/// Implementation of [`NetworkBehaviour`] that doesn't do anything.
18pub struct Behaviour;
19
20impl NetworkBehaviour for Behaviour {
21    type ConnectionHandler = ConnectionHandler;
22    type ToSwarm = Infallible;
23
24    fn handle_established_inbound_connection(
25        &mut self,
26        _: ConnectionId,
27        _: PeerId,
28        _: &Multiaddr,
29        _: &Multiaddr,
30    ) -> Result<THandler<Self>, ConnectionDenied> {
31        Ok(ConnectionHandler)
32    }
33
34    fn handle_established_outbound_connection(
35        &mut self,
36        _: ConnectionId,
37        _: PeerId,
38        _: &Multiaddr,
39        _: Endpoint,
40        _: PortUse,
41    ) -> Result<THandler<Self>, ConnectionDenied> {
42        Ok(ConnectionHandler)
43    }
44
45    fn on_connection_handler_event(
46        &mut self,
47        _: PeerId,
48        _: ConnectionId,
49        event: THandlerOutEvent<Self>,
50    ) {
51        libp2p_core::util::unreachable(event)
52    }
53
54    fn poll(&mut self, _: &mut Context<'_>) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
55        Poll::Pending
56    }
57
58    fn on_swarm_event(&mut self, _event: FromSwarm) {}
59}
60
61/// An implementation of [`ConnectionHandler`] that neither handles any protocols nor does it keep
62/// the connection alive.
63#[derive(Clone)]
64pub struct ConnectionHandler;
65
66impl crate::handler::ConnectionHandler for ConnectionHandler {
67    type FromBehaviour = Infallible;
68    type ToBehaviour = Infallible;
69    type InboundProtocol = DeniedUpgrade;
70    type OutboundProtocol = DeniedUpgrade;
71    type InboundOpenInfo = ();
72    type OutboundOpenInfo = ();
73
74    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
75        SubstreamProtocol::new(DeniedUpgrade, ())
76    }
77
78    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
79        libp2p_core::util::unreachable(event)
80    }
81
82    fn poll(
83        &mut self,
84        _: &mut Context<'_>,
85    ) -> Poll<ConnectionHandlerEvent<Self::OutboundProtocol, (), Self::ToBehaviour>> {
86        Poll::Pending
87    }
88
89    fn on_connection_event(
90        &mut self,
91        event: ConnectionEvent<Self::InboundProtocol, Self::OutboundProtocol>,
92    ) {
93        match event {
94            ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
95                protocol, ..
96            }) => libp2p_core::util::unreachable(protocol),
97            ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
98                protocol, ..
99            }) => libp2p_core::util::unreachable(protocol),
100            ConnectionEvent::DialUpgradeError(DialUpgradeError { info: _, error }) => match error {
101                StreamUpgradeError::Timeout => unreachable!(),
102                StreamUpgradeError::Apply(e) => libp2p_core::util::unreachable(e),
103                StreamUpgradeError::NegotiationFailed | StreamUpgradeError::Io(_) => {
104                    unreachable!("Denied upgrade does not support any protocols")
105                }
106            },
107            ConnectionEvent::AddressChange(_)
108            | ConnectionEvent::ListenUpgradeError(_)
109            | ConnectionEvent::LocalProtocolsChange(_)
110            | ConnectionEvent::RemoteProtocolsChange(_) => {}
111        }
112    }
113}