mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 17:21:34 +00:00
Simplify trait bounds on NetworkBehaviour (#1405)
* Simplify trait bounds requirements * More work * Moar * Finish * Fix final tests * More simplification * Use separate traits for Inbound/Outbound * Update gossipsub and remove warnings * Add documentation to swarm * Remove BoxSubstream * Fix tests not compiling * Fix stack overflow * Address concerns * For some reason my IDE ignored libp2p-kad
This commit is contained in:
@ -18,6 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::NegotiatedSubstream;
|
||||
use crate::protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
@ -25,33 +26,25 @@ use crate::protocols_handler::{
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::{Negotiated, upgrade::{InboundUpgrade, OutboundUpgrade, DeniedUpgrade}};
|
||||
use std::{marker::PhantomData, task::Context, task::Poll};
|
||||
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, DeniedUpgrade};
|
||||
use std::task::{Context, Poll};
|
||||
use void::Void;
|
||||
|
||||
/// Implementation of `ProtocolsHandler` that doesn't handle anything.
|
||||
pub struct DummyProtocolsHandler<TSubstream> {
|
||||
marker: PhantomData<TSubstream>,
|
||||
pub struct DummyProtocolsHandler {
|
||||
}
|
||||
|
||||
impl<TSubstream> Default for DummyProtocolsHandler<TSubstream> {
|
||||
#[inline]
|
||||
impl Default for DummyProtocolsHandler {
|
||||
fn default() -> Self {
|
||||
DummyProtocolsHandler {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> ProtocolsHandler for DummyProtocolsHandler<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
impl ProtocolsHandler for DummyProtocolsHandler {
|
||||
type InEvent = Void;
|
||||
type OutEvent = Void;
|
||||
type Error = Void;
|
||||
type Substream = TSubstream;
|
||||
type InboundProtocol = DeniedUpgrade;
|
||||
type OutboundProtocol = DeniedUpgrade;
|
||||
type OutboundOpenInfo = Void;
|
||||
@ -64,14 +57,14 @@ where
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
_: <Self::InboundProtocol as InboundUpgrade<Negotiated<TSubstream>>>::Output
|
||||
_: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output
|
||||
) {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
_: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<TSubstream>>>::Output,
|
||||
_: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
|
||||
_: Self::OutboundOpenInfo
|
||||
) {
|
||||
}
|
||||
@ -80,7 +73,7 @@ where
|
||||
fn inject_event(&mut self, _: Self::InEvent) {}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Error>) {}
|
||||
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, _: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error>) {}
|
||||
|
||||
#[inline]
|
||||
fn connection_keep_alive(&self) -> KeepAlive { KeepAlive::No }
|
||||
|
@ -18,6 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
|
||||
use crate::protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
@ -25,7 +26,7 @@ use crate::protocols_handler::{
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr
|
||||
};
|
||||
use libp2p_core::{Negotiated, upgrade::{InboundUpgrade, OutboundUpgrade}};
|
||||
|
||||
use std::{marker::PhantomData, task::Context, task::Poll};
|
||||
|
||||
/// Wrapper around a protocol handler that turns the input event into something else.
|
||||
@ -51,11 +52,12 @@ impl<TProtoHandler, TMap, TNewIn> ProtocolsHandler for MapInEvent<TProtoHandler,
|
||||
where
|
||||
TProtoHandler: ProtocolsHandler,
|
||||
TMap: Fn(TNewIn) -> Option<TProtoHandler::InEvent>,
|
||||
TNewIn: Send + 'static,
|
||||
TMap: Send + 'static,
|
||||
{
|
||||
type InEvent = TNewIn;
|
||||
type OutEvent = TProtoHandler::OutEvent;
|
||||
type Error = TProtoHandler::Error;
|
||||
type Substream = TProtoHandler::Substream;
|
||||
type InboundProtocol = TProtoHandler::InboundProtocol;
|
||||
type OutboundProtocol = TProtoHandler::OutboundProtocol;
|
||||
type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
|
||||
@ -68,7 +70,7 @@ where
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
protocol: <Self::InboundProtocol as InboundUpgrade<Negotiated<Self::Substream>>>::Output
|
||||
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output
|
||||
) {
|
||||
self.inner.inject_fully_negotiated_inbound(protocol)
|
||||
}
|
||||
@ -76,7 +78,7 @@ where
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
protocol: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Output,
|
||||
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
|
||||
info: Self::OutboundOpenInfo
|
||||
) {
|
||||
self.inner.inject_fully_negotiated_outbound(protocol, info)
|
||||
@ -90,7 +92,7 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Error>) {
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>) {
|
||||
self.inner.inject_dial_upgrade_error(info, error)
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
|
||||
use crate::protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
@ -25,7 +26,7 @@ use crate::protocols_handler::{
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr
|
||||
};
|
||||
use libp2p_core::{Negotiated, upgrade::{InboundUpgrade, OutboundUpgrade}};
|
||||
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
/// Wrapper around a protocol handler that turns the output event into something else.
|
||||
@ -49,11 +50,12 @@ impl<TProtoHandler, TMap, TNewOut> ProtocolsHandler for MapOutEvent<TProtoHandle
|
||||
where
|
||||
TProtoHandler: ProtocolsHandler,
|
||||
TMap: FnMut(TProtoHandler::OutEvent) -> TNewOut,
|
||||
TNewOut: Send + 'static,
|
||||
TMap: Send + 'static,
|
||||
{
|
||||
type InEvent = TProtoHandler::InEvent;
|
||||
type OutEvent = TNewOut;
|
||||
type Error = TProtoHandler::Error;
|
||||
type Substream = TProtoHandler::Substream;
|
||||
type InboundProtocol = TProtoHandler::InboundProtocol;
|
||||
type OutboundProtocol = TProtoHandler::OutboundProtocol;
|
||||
type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
|
||||
@ -66,7 +68,7 @@ where
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
protocol: <Self::InboundProtocol as InboundUpgrade<Negotiated<Self::Substream>>>::Output
|
||||
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output
|
||||
) {
|
||||
self.inner.inject_fully_negotiated_inbound(protocol)
|
||||
}
|
||||
@ -74,7 +76,7 @@ where
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
protocol: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Output,
|
||||
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
|
||||
info: Self::OutboundOpenInfo
|
||||
) {
|
||||
self.inner.inject_fully_negotiated_outbound(protocol, info)
|
||||
@ -86,7 +88,7 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Error>) {
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>) {
|
||||
self.inner.inject_dial_upgrade_error(info, error)
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::upgrade::SendWrapper;
|
||||
use crate::protocols_handler::{
|
||||
KeepAlive,
|
||||
ProtocolsHandler,
|
||||
@ -25,10 +26,13 @@ use crate::protocols_handler::{
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr
|
||||
};
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::{
|
||||
ConnectedPoint,
|
||||
PeerId,
|
||||
muxing::StreamMuxerBox,
|
||||
nodes::Substream,
|
||||
nodes::collection::ConnectionInfo,
|
||||
nodes::handled_node::{IntoNodeHandler, NodeHandler, NodeHandlerEndpoint, NodeHandlerEvent},
|
||||
upgrade::{self, InboundUpgradeApply, OutboundUpgradeApply}
|
||||
@ -102,17 +106,17 @@ where
|
||||
handler: TProtoHandler,
|
||||
/// Futures that upgrade incoming substreams.
|
||||
negotiating_in:
|
||||
Vec<(InboundUpgradeApply<TProtoHandler::Substream, TProtoHandler::InboundProtocol>, Delay)>,
|
||||
Vec<(InboundUpgradeApply<Substream<StreamMuxerBox>, SendWrapper<TProtoHandler::InboundProtocol>>, Delay)>,
|
||||
/// Futures that upgrade outgoing substreams. The first element of the tuple is the userdata
|
||||
/// to pass back once successfully opened.
|
||||
negotiating_out: Vec<(
|
||||
TProtoHandler::OutboundOpenInfo,
|
||||
OutboundUpgradeApply<TProtoHandler::Substream, TProtoHandler::OutboundProtocol>,
|
||||
OutboundUpgradeApply<Substream<StreamMuxerBox>, SendWrapper<TProtoHandler::OutboundProtocol>>,
|
||||
Delay,
|
||||
)>,
|
||||
/// For each outbound substream request, how to upgrade it. The first element of the tuple
|
||||
/// is the unique identifier (see `unique_dial_upgrade_id`).
|
||||
queued_dial_upgrades: Vec<(u64, (upgrade::Version, TProtoHandler::OutboundProtocol))>,
|
||||
queued_dial_upgrades: Vec<(u64, (upgrade::Version, SendWrapper<TProtoHandler::OutboundProtocol>))>,
|
||||
/// Unique identifier assigned to each queued dial upgrade.
|
||||
unique_dial_upgrade_id: u64,
|
||||
/// The currently planned connection & handler shutdown.
|
||||
@ -184,7 +188,7 @@ where
|
||||
type InEvent = TProtoHandler::InEvent;
|
||||
type OutEvent = TProtoHandler::OutEvent;
|
||||
type Error = NodeHandlerWrapperError<TProtoHandler::Error>;
|
||||
type Substream = TProtoHandler::Substream;
|
||||
type Substream = Substream<StreamMuxerBox>;
|
||||
// The first element of the tuple is the unique upgrade identifier
|
||||
// (see `unique_dial_upgrade_id`).
|
||||
type OutboundOpenInfo = (u64, TProtoHandler::OutboundOpenInfo, Duration);
|
||||
@ -198,7 +202,7 @@ where
|
||||
NodeHandlerEndpoint::Listener => {
|
||||
let protocol = self.handler.listen_protocol();
|
||||
let timeout = protocol.timeout().clone();
|
||||
let upgrade = upgrade::apply_inbound(substream, protocol.into_upgrade().1);
|
||||
let upgrade = upgrade::apply_inbound(substream, SendWrapper(protocol.into_upgrade().1));
|
||||
let timeout = Delay::new(timeout);
|
||||
self.negotiating_in.push((upgrade, timeout));
|
||||
}
|
||||
@ -305,7 +309,8 @@ where
|
||||
let id = self.unique_dial_upgrade_id;
|
||||
let timeout = protocol.timeout().clone();
|
||||
self.unique_dial_upgrade_id += 1;
|
||||
self.queued_dial_upgrades.push((id, protocol.into_upgrade()));
|
||||
let (version, upgrade) = protocol.into_upgrade();
|
||||
self.queued_dial_upgrades.push((id, (version, SendWrapper(upgrade))));
|
||||
return Poll::Ready(Ok(
|
||||
NodeHandlerEvent::OutboundSubstreamRequest((id, info, timeout)),
|
||||
));
|
||||
|
@ -18,6 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
|
||||
use crate::protocols_handler::{
|
||||
KeepAlive,
|
||||
ProtocolsHandler,
|
||||
@ -25,25 +26,24 @@ use crate::protocols_handler::{
|
||||
ProtocolsHandlerUpgrErr,
|
||||
SubstreamProtocol
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::{Negotiated, upgrade::{InboundUpgrade, OutboundUpgrade}};
|
||||
|
||||
use smallvec::SmallVec;
|
||||
use std::{error, marker::PhantomData, task::Context, task::Poll, time::Duration};
|
||||
use std::{error, task::Context, task::Poll, time::Duration};
|
||||
use wasm_timer::Instant;
|
||||
|
||||
/// Implementation of `ProtocolsHandler` that opens a new substream for each individual message.
|
||||
///
|
||||
/// This struct is meant to be a helper for other implementations to use.
|
||||
// TODO: Debug
|
||||
pub struct OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
pub struct OneShotHandler<TInProto, TOutProto, TOutEvent>
|
||||
where
|
||||
TOutProto: OutboundUpgrade<Negotiated<TSubstream>>,
|
||||
TOutProto: OutboundUpgradeSend,
|
||||
{
|
||||
/// The upgrade for inbound substreams.
|
||||
listen_protocol: SubstreamProtocol<TInProto>,
|
||||
/// If `Some`, something bad happened and we should shut down the handler with an error.
|
||||
pending_error:
|
||||
Option<ProtocolsHandlerUpgrErr<<TOutProto as OutboundUpgrade<Negotiated<TSubstream>>>::Error>>,
|
||||
Option<ProtocolsHandlerUpgrErr<<TOutProto as OutboundUpgradeSend>::Error>>,
|
||||
/// Queue of events to produce in `poll()`.
|
||||
events_out: SmallVec<[TOutEvent; 4]>,
|
||||
/// Queue of outbound substreams to open.
|
||||
@ -56,14 +56,12 @@ where
|
||||
keep_alive: KeepAlive,
|
||||
/// After the given duration has elapsed, an inactive connection will shutdown.
|
||||
inactive_timeout: Duration,
|
||||
/// Pin the `TSubstream` generic.
|
||||
marker: PhantomData<TSubstream>,
|
||||
}
|
||||
|
||||
impl<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
impl<TInProto, TOutProto, TOutEvent>
|
||||
OneShotHandler<TInProto, TOutProto, TOutEvent>
|
||||
where
|
||||
TOutProto: OutboundUpgrade<Negotiated<TSubstream>>,
|
||||
TOutProto: OutboundUpgradeSend,
|
||||
{
|
||||
/// Creates a `OneShotHandler`.
|
||||
#[inline]
|
||||
@ -80,7 +78,6 @@ where
|
||||
max_dial_negotiated: 8,
|
||||
keep_alive: KeepAlive::Yes,
|
||||
inactive_timeout,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,11 +113,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TInProto, TOutProto, TOutEvent> Default
|
||||
for OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
impl<TInProto, TOutProto, TOutEvent> Default
|
||||
for OneShotHandler<TInProto, TOutProto, TOutEvent>
|
||||
where
|
||||
TOutProto: OutboundUpgrade<Negotiated<TSubstream>>,
|
||||
TInProto: InboundUpgrade<Negotiated<TSubstream>> + Default,
|
||||
TOutProto: OutboundUpgradeSend,
|
||||
TInProto: InboundUpgradeSend + Default,
|
||||
{
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
@ -128,23 +125,22 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TInProto, TOutProto, TOutEvent> ProtocolsHandler
|
||||
for OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
||||
impl<TInProto, TOutProto, TOutEvent> ProtocolsHandler
|
||||
for OneShotHandler<TInProto, TOutProto, TOutEvent>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
TInProto: InboundUpgrade<Negotiated<TSubstream>>,
|
||||
TOutProto: OutboundUpgrade<Negotiated<TSubstream>>,
|
||||
TInProto: InboundUpgradeSend + Send + 'static,
|
||||
TOutProto: OutboundUpgradeSend,
|
||||
TInProto::Output: Into<TOutEvent>,
|
||||
TOutProto::Output: Into<TOutEvent>,
|
||||
TOutProto::Error: error::Error + 'static,
|
||||
TOutProto::Error: error::Error + Send + 'static,
|
||||
SubstreamProtocol<TInProto>: Clone,
|
||||
TOutEvent: Send + 'static,
|
||||
{
|
||||
type InEvent = TOutProto;
|
||||
type OutEvent = TOutEvent;
|
||||
type Error = ProtocolsHandlerUpgrErr<
|
||||
<Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Error,
|
||||
<Self::OutboundProtocol as OutboundUpgradeSend>::Error,
|
||||
>;
|
||||
type Substream = TSubstream;
|
||||
type InboundProtocol = TInProto;
|
||||
type OutboundProtocol = TOutProto;
|
||||
type OutboundOpenInfo = ();
|
||||
@ -157,7 +153,7 @@ where
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
out: <Self::InboundProtocol as InboundUpgrade<Negotiated<Self::Substream>>>::Output,
|
||||
out: <Self::InboundProtocol as InboundUpgradeSend>::Output,
|
||||
) {
|
||||
// If we're shutting down the connection for inactivity, reset the timeout.
|
||||
if !self.keep_alive.is_yes() {
|
||||
@ -170,7 +166,7 @@ where
|
||||
#[inline]
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
out: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Output,
|
||||
out: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
|
||||
_: Self::OutboundOpenInfo,
|
||||
) {
|
||||
self.dial_negotiated -= 1;
|
||||
@ -192,7 +188,7 @@ where
|
||||
&mut self,
|
||||
_: Self::OutboundOpenInfo,
|
||||
error: ProtocolsHandlerUpgrErr<
|
||||
<Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Error,
|
||||
<Self::OutboundProtocol as OutboundUpgradeSend>::Error,
|
||||
>,
|
||||
) {
|
||||
if self.pending_error.is_none() {
|
||||
|
@ -18,6 +18,7 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::upgrade::{SendWrapper, InboundUpgradeSend, OutboundUpgradeSend};
|
||||
use crate::protocols_handler::{
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
@ -26,13 +27,12 @@ use crate::protocols_handler::{
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr,
|
||||
};
|
||||
use futures::prelude::*;
|
||||
|
||||
use libp2p_core::{
|
||||
ConnectedPoint,
|
||||
Negotiated,
|
||||
PeerId,
|
||||
either::{EitherError, EitherOutput},
|
||||
upgrade::{InboundUpgrade, OutboundUpgrade, EitherUpgrade, SelectUpgrade, UpgradeError}
|
||||
upgrade::{EitherUpgrade, SelectUpgrade, UpgradeError}
|
||||
};
|
||||
use std::{cmp, task::Context, task::Poll};
|
||||
|
||||
@ -56,17 +56,10 @@ impl<TProto1, TProto2> IntoProtocolsHandlerSelect<TProto1, TProto2> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TProto1, TProto2, TSubstream> IntoProtocolsHandler for IntoProtocolsHandlerSelect<TProto1, TProto2>
|
||||
impl<TProto1, TProto2> IntoProtocolsHandler for IntoProtocolsHandlerSelect<TProto1, TProto2>
|
||||
where
|
||||
TProto1: IntoProtocolsHandler,
|
||||
TProto2: IntoProtocolsHandler,
|
||||
TProto1::Handler: ProtocolsHandler<Substream = TSubstream>,
|
||||
TProto2::Handler: ProtocolsHandler<Substream = TSubstream>,
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
<TProto1::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<Negotiated<TSubstream>>,
|
||||
<TProto2::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<Negotiated<TSubstream>>,
|
||||
<TProto1::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<Negotiated<TSubstream>>,
|
||||
<TProto2::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<Negotiated<TSubstream>>
|
||||
{
|
||||
type Handler = ProtocolsHandlerSelect<TProto1::Handler, TProto2::Handler>;
|
||||
|
||||
@ -78,7 +71,7 @@ where
|
||||
}
|
||||
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
|
||||
SelectUpgrade::new(self.proto1.inbound_protocol(), self.proto2.inbound_protocol())
|
||||
SelectUpgrade::new(SendWrapper(self.proto1.inbound_protocol()), SendWrapper(self.proto2.inbound_protocol()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -102,23 +95,16 @@ impl<TProto1, TProto2> ProtocolsHandlerSelect<TProto1, TProto2> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream, TProto1, TProto2>
|
||||
ProtocolsHandler for ProtocolsHandlerSelect<TProto1, TProto2>
|
||||
impl<TProto1, TProto2> ProtocolsHandler for ProtocolsHandlerSelect<TProto1, TProto2>
|
||||
where
|
||||
TProto1: ProtocolsHandler<Substream = TSubstream>,
|
||||
TProto2: ProtocolsHandler<Substream = TSubstream>,
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
TProto1::InboundProtocol: InboundUpgrade<Negotiated<TSubstream>>,
|
||||
TProto2::InboundProtocol: InboundUpgrade<Negotiated<TSubstream>>,
|
||||
TProto1::OutboundProtocol: OutboundUpgrade<Negotiated<TSubstream>>,
|
||||
TProto2::OutboundProtocol: OutboundUpgrade<Negotiated<TSubstream>>
|
||||
TProto1: ProtocolsHandler,
|
||||
TProto2: ProtocolsHandler,
|
||||
{
|
||||
type InEvent = EitherOutput<TProto1::InEvent, TProto2::InEvent>;
|
||||
type OutEvent = EitherOutput<TProto1::OutEvent, TProto2::OutEvent>;
|
||||
type Error = EitherError<TProto1::Error, TProto2::Error>;
|
||||
type Substream = TSubstream;
|
||||
type InboundProtocol = SelectUpgrade<<TProto1 as ProtocolsHandler>::InboundProtocol, <TProto2 as ProtocolsHandler>::InboundProtocol>;
|
||||
type OutboundProtocol = EitherUpgrade<TProto1::OutboundProtocol, TProto2::OutboundProtocol>;
|
||||
type InboundProtocol = SelectUpgrade<SendWrapper<<TProto1 as ProtocolsHandler>::InboundProtocol>, SendWrapper<<TProto2 as ProtocolsHandler>::InboundProtocol>>;
|
||||
type OutboundProtocol = EitherUpgrade<SendWrapper<TProto1::OutboundProtocol>, SendWrapper<TProto2::OutboundProtocol>>;
|
||||
type OutboundOpenInfo = EitherOutput<TProto1::OutboundOpenInfo, TProto2::OutboundOpenInfo>;
|
||||
|
||||
#[inline]
|
||||
@ -126,11 +112,11 @@ where
|
||||
let proto1 = self.proto1.listen_protocol();
|
||||
let proto2 = self.proto2.listen_protocol();
|
||||
let timeout = std::cmp::max(proto1.timeout(), proto2.timeout()).clone();
|
||||
let choice = SelectUpgrade::new(proto1.into_upgrade().1, proto2.into_upgrade().1);
|
||||
let choice = SelectUpgrade::new(SendWrapper(proto1.into_upgrade().1), SendWrapper(proto2.into_upgrade().1));
|
||||
SubstreamProtocol::new(choice).with_timeout(timeout)
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_outbound(&mut self, protocol: <Self::OutboundProtocol as OutboundUpgrade<Negotiated<TSubstream>>>::Output, endpoint: Self::OutboundOpenInfo) {
|
||||
fn inject_fully_negotiated_outbound(&mut self, protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output, endpoint: Self::OutboundOpenInfo) {
|
||||
match (protocol, endpoint) {
|
||||
(EitherOutput::First(protocol), EitherOutput::First(info)) =>
|
||||
self.proto1.inject_fully_negotiated_outbound(protocol, info),
|
||||
@ -143,7 +129,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_inbound(&mut self, protocol: <Self::InboundProtocol as InboundUpgrade<Negotiated<TSubstream>>>::Output) {
|
||||
fn inject_fully_negotiated_inbound(&mut self, protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output) {
|
||||
match protocol {
|
||||
EitherOutput::First(protocol) =>
|
||||
self.proto1.inject_fully_negotiated_inbound(protocol),
|
||||
@ -161,7 +147,7 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Negotiated<Self::Substream>>>::Error>) {
|
||||
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>) {
|
||||
match (info, error) {
|
||||
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Timer) => {
|
||||
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer)
|
||||
@ -215,7 +201,7 @@ where
|
||||
info,
|
||||
}) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: protocol.map_upgrade(EitherUpgrade::A),
|
||||
protocol: protocol.map_upgrade(|u| EitherUpgrade::A(SendWrapper(u))),
|
||||
info: EitherOutput::First(info),
|
||||
});
|
||||
},
|
||||
@ -234,7 +220,7 @@ where
|
||||
info,
|
||||
}) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: protocol.map_upgrade(EitherUpgrade::B),
|
||||
protocol: protocol.map_upgrade(|u| EitherUpgrade::B(SendWrapper(u))),
|
||||
info: EitherOutput::Second(info),
|
||||
});
|
||||
},
|
||||
|
Reference in New Issue
Block a user