|
| 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 | + |
| 22 | +use crate::handler::{ |
| 23 | + ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, |
| 24 | + SubstreamProtocol, |
| 25 | +}; |
| 26 | +use crate::NegotiatedSubstream; |
| 27 | +use libp2p_core::{ |
| 28 | + upgrade::{InboundUpgrade, OutboundUpgrade, PendingUpgrade}, |
| 29 | + Multiaddr, |
| 30 | +}; |
| 31 | +use std::task::{Context, Poll}; |
| 32 | +use void::Void; |
| 33 | + |
| 34 | +/// Implementation of [`ConnectionHandler`] that returns a pending upgrade. |
| 35 | +#[derive(Clone, Debug)] |
| 36 | +pub struct PendingConnectionHandler { |
| 37 | + protocol_name: String, |
| 38 | +} |
| 39 | + |
| 40 | +impl PendingConnectionHandler { |
| 41 | + pub fn new(protocol_name: String) -> Self { |
| 42 | + PendingConnectionHandler { protocol_name } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl ConnectionHandler for PendingConnectionHandler { |
| 47 | + type InEvent = Void; |
| 48 | + type OutEvent = Void; |
| 49 | + type Error = Void; |
| 50 | + type InboundProtocol = PendingUpgrade<String>; |
| 51 | + type OutboundProtocol = PendingUpgrade<String>; |
| 52 | + type OutboundOpenInfo = Void; |
| 53 | + type InboundOpenInfo = (); |
| 54 | + |
| 55 | + fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> { |
| 56 | + SubstreamProtocol::new(PendingUpgrade::new(self.protocol_name.clone()), ()) |
| 57 | + } |
| 58 | + |
| 59 | + fn inject_fully_negotiated_inbound( |
| 60 | + &mut self, |
| 61 | + protocol: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output, |
| 62 | + _: Self::InboundOpenInfo, |
| 63 | + ) { |
| 64 | + void::unreachable(protocol) |
| 65 | + } |
| 66 | + |
| 67 | + fn inject_fully_negotiated_outbound( |
| 68 | + &mut self, |
| 69 | + protocol: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output, |
| 70 | + _info: Self::OutboundOpenInfo, |
| 71 | + ) { |
| 72 | + void::unreachable(protocol); |
| 73 | + #[allow(unreachable_code)] |
| 74 | + { |
| 75 | + void::unreachable(_info); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + fn inject_event(&mut self, v: Self::InEvent) { |
| 80 | + void::unreachable(v) |
| 81 | + } |
| 82 | + |
| 83 | + fn inject_address_change(&mut self, _: &Multiaddr) {} |
| 84 | + |
| 85 | + fn inject_dial_upgrade_error( |
| 86 | + &mut self, |
| 87 | + _: Self::OutboundOpenInfo, |
| 88 | + _: ConnectionHandlerUpgrErr< |
| 89 | + <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error, |
| 90 | + >, |
| 91 | + ) { |
| 92 | + } |
| 93 | + |
| 94 | + fn inject_listen_upgrade_error( |
| 95 | + &mut self, |
| 96 | + _: Self::InboundOpenInfo, |
| 97 | + _: ConnectionHandlerUpgrErr< |
| 98 | + <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Error, |
| 99 | + >, |
| 100 | + ) { |
| 101 | + } |
| 102 | + |
| 103 | + fn connection_keep_alive(&self) -> KeepAlive { |
| 104 | + KeepAlive::No |
| 105 | + } |
| 106 | + |
| 107 | + fn poll( |
| 108 | + &mut self, |
| 109 | + _: &mut Context<'_>, |
| 110 | + ) -> Poll< |
| 111 | + ConnectionHandlerEvent< |
| 112 | + Self::OutboundProtocol, |
| 113 | + Self::OutboundOpenInfo, |
| 114 | + Self::OutEvent, |
| 115 | + Self::Error, |
| 116 | + >, |
| 117 | + > { |
| 118 | + Poll::Pending |
| 119 | + } |
| 120 | +} |
0 commit comments