libp2p_swarm/handler/
pending.rs

1// Copyright 2022 Protocol Labs.
2// Copyright 2018 Parity Technologies (UK) Ltd.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22use crate::handler::{
23    ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, FullyNegotiatedInbound,
24    FullyNegotiatedOutbound, KeepAlive, SubstreamProtocol,
25};
26use libp2p_core::upgrade::PendingUpgrade;
27use std::task::{Context, Poll};
28use void::Void;
29
30/// Implementation of [`ConnectionHandler`] that returns a pending upgrade.
31#[derive(Clone, Debug)]
32pub struct PendingConnectionHandler {
33    protocol_name: String,
34}
35
36impl PendingConnectionHandler {
37    pub fn new(protocol_name: String) -> Self {
38        PendingConnectionHandler { protocol_name }
39    }
40}
41
42impl ConnectionHandler for PendingConnectionHandler {
43    type FromBehaviour = Void;
44    type ToBehaviour = Void;
45    type Error = Void;
46    type InboundProtocol = PendingUpgrade<String>;
47    type OutboundProtocol = PendingUpgrade<String>;
48    type OutboundOpenInfo = Void;
49    type InboundOpenInfo = ();
50
51    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
52        SubstreamProtocol::new(PendingUpgrade::new(self.protocol_name.clone()), ())
53    }
54
55    fn on_behaviour_event(&mut self, v: Self::FromBehaviour) {
56        void::unreachable(v)
57    }
58
59    fn connection_keep_alive(&self) -> KeepAlive {
60        KeepAlive::No
61    }
62
63    #[allow(deprecated)]
64    fn poll(
65        &mut self,
66        _: &mut Context<'_>,
67    ) -> Poll<
68        ConnectionHandlerEvent<
69            Self::OutboundProtocol,
70            Self::OutboundOpenInfo,
71            Self::ToBehaviour,
72            Self::Error,
73        >,
74    > {
75        Poll::Pending
76    }
77
78    fn on_connection_event(
79        &mut self,
80        event: ConnectionEvent<
81            Self::InboundProtocol,
82            Self::OutboundProtocol,
83            Self::InboundOpenInfo,
84            Self::OutboundOpenInfo,
85        >,
86    ) {
87        match event {
88            ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
89                protocol, ..
90            }) => void::unreachable(protocol),
91            ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
92                protocol,
93                info: _info,
94            }) => {
95                void::unreachable(protocol);
96                #[allow(unreachable_code, clippy::used_underscore_binding)]
97                {
98                    void::unreachable(_info);
99                }
100            }
101            ConnectionEvent::AddressChange(_)
102            | ConnectionEvent::DialUpgradeError(_)
103            | ConnectionEvent::ListenUpgradeError(_)
104            | ConnectionEvent::LocalProtocolsChange(_)
105            | ConnectionEvent::RemoteProtocolsChange(_) => {}
106        }
107    }
108}