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, 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 InboundProtocol = PendingUpgrade<String>;
46    type OutboundProtocol = PendingUpgrade<String>;
47    type OutboundOpenInfo = Void;
48    type InboundOpenInfo = ();
49
50    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
51        SubstreamProtocol::new(PendingUpgrade::new(self.protocol_name.clone()), ())
52    }
53
54    fn on_behaviour_event(&mut self, v: Self::FromBehaviour) {
55        void::unreachable(v)
56    }
57
58    fn poll(
59        &mut self,
60        _: &mut Context<'_>,
61    ) -> Poll<
62        ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
63    > {
64        Poll::Pending
65    }
66
67    fn on_connection_event(
68        &mut self,
69        event: ConnectionEvent<
70            Self::InboundProtocol,
71            Self::OutboundProtocol,
72            Self::InboundOpenInfo,
73            Self::OutboundOpenInfo,
74        >,
75    ) {
76        match event {
77            ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
78                protocol, ..
79            }) => void::unreachable(protocol),
80            ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
81                protocol,
82                info: _info,
83            }) => {
84                void::unreachable(protocol);
85                #[allow(unreachable_code, clippy::used_underscore_binding)]
86                {
87                    void::unreachable(_info);
88                }
89            }
90            ConnectionEvent::AddressChange(_)
91            | ConnectionEvent::DialUpgradeError(_)
92            | ConnectionEvent::ListenUpgradeError(_)
93            | ConnectionEvent::LocalProtocolsChange(_)
94            | ConnectionEvent::RemoteProtocolsChange(_) => {}
95        }
96    }
97}