libp2p_swarm/
dummy.rs

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