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
17pub 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 #[allow(unreachable_patterns)]
53 libp2p_core::util::unreachable(event)
54 }
55
56 fn poll(&mut self, _: &mut Context<'_>) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
57 Poll::Pending
58 }
59
60 fn on_swarm_event(&mut self, _event: FromSwarm) {}
61}
62
63#[derive(Clone)]
66pub struct ConnectionHandler;
67
68impl crate::handler::ConnectionHandler for ConnectionHandler {
69 type FromBehaviour = Infallible;
70 type ToBehaviour = Infallible;
71 type InboundProtocol = DeniedUpgrade;
72 type OutboundProtocol = DeniedUpgrade;
73 type InboundOpenInfo = ();
74 type OutboundOpenInfo = ();
75
76 fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
77 SubstreamProtocol::new(DeniedUpgrade, ())
78 }
79
80 fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
81 #[allow(unreachable_patterns)]
83 libp2p_core::util::unreachable(event)
84 }
85
86 fn poll(
87 &mut self,
88 _: &mut Context<'_>,
89 ) -> Poll<ConnectionHandlerEvent<Self::OutboundProtocol, (), Self::ToBehaviour>> {
90 Poll::Pending
91 }
92
93 fn on_connection_event(
94 &mut self,
95 event: ConnectionEvent<Self::InboundProtocol, Self::OutboundProtocol>,
96 ) {
97 match event {
98 #[allow(unreachable_patterns)]
100 ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
101 protocol, ..
102 }) => libp2p_core::util::unreachable(protocol),
103 #[allow(unreachable_patterns)]
105 ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
106 protocol, ..
107 }) => libp2p_core::util::unreachable(protocol),
108 #[allow(unreachable_patterns)]
110 ConnectionEvent::DialUpgradeError(DialUpgradeError { info: _, error }) => match error {
111 #[allow(unreachable_patterns)]
113 StreamUpgradeError::Timeout => unreachable!(),
114 StreamUpgradeError::Apply(e) => libp2p_core::util::unreachable(e),
115 StreamUpgradeError::NegotiationFailed | StreamUpgradeError::Io(_) => {
116 unreachable!("Denied upgrade does not support any protocols")
117 }
118 },
119 #[allow(unreachable_patterns)]
121 ConnectionEvent::AddressChange(_)
122 | ConnectionEvent::ListenUpgradeError(_)
123 | ConnectionEvent::LocalProtocolsChange(_)
124 | ConnectionEvent::RemoteProtocolsChange(_) => {}
125 }
126 }
127}