2018-11-26 14:01:08 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2020-02-07 16:29:30 +01:00
|
|
|
use crate::upgrade::SendWrapper;
|
2019-07-04 14:47:59 +02:00
|
|
|
use crate::protocols_handler::{
|
|
|
|
KeepAlive,
|
|
|
|
ProtocolsHandler,
|
|
|
|
IntoProtocolsHandler,
|
|
|
|
ProtocolsHandlerEvent,
|
|
|
|
ProtocolsHandlerUpgrErr
|
|
|
|
};
|
2020-02-07 16:29:30 +01:00
|
|
|
|
2019-07-04 14:47:59 +02:00
|
|
|
use futures::prelude::*;
|
2020-10-09 19:37:17 +02:00
|
|
|
use futures::stream::FuturesUnordered;
|
2019-07-04 14:47:59 +02:00
|
|
|
use libp2p_core::{
|
2020-06-30 17:10:53 +02:00
|
|
|
Multiaddr,
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
Connected,
|
|
|
|
connection::{
|
|
|
|
ConnectionHandler,
|
|
|
|
ConnectionHandlerEvent,
|
|
|
|
IntoConnectionHandler,
|
|
|
|
Substream,
|
|
|
|
SubstreamEndpoint,
|
|
|
|
},
|
2020-02-07 16:29:30 +01:00
|
|
|
muxing::StreamMuxerBox,
|
2020-10-09 19:37:17 +02:00
|
|
|
upgrade::{self, InboundUpgradeApply, OutboundUpgradeApply, UpgradeError}
|
2018-11-26 14:01:08 +01:00
|
|
|
};
|
2019-09-16 11:08:44 +02:00
|
|
|
use std::{error, fmt, pin::Pin, task::Context, task::Poll, time::Duration};
|
|
|
|
use wasm_timer::{Delay, Instant};
|
2018-11-26 14:01:08 +01:00
|
|
|
|
|
|
|
/// Prototype for a `NodeHandlerWrapper`.
|
2019-01-14 14:22:25 +01:00
|
|
|
pub struct NodeHandlerWrapperBuilder<TIntoProtoHandler> {
|
2018-11-26 14:01:08 +01:00
|
|
|
/// The underlying handler.
|
2019-01-14 14:22:25 +01:00
|
|
|
handler: TIntoProtoHandler,
|
2020-11-25 14:26:49 +01:00
|
|
|
/// The substream upgrade protocol override, if any.
|
|
|
|
substream_upgrade_protocol_override: Option<upgrade::Version>,
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 14:22:25 +01:00
|
|
|
impl<TIntoProtoHandler> NodeHandlerWrapperBuilder<TIntoProtoHandler>
|
2018-11-26 14:01:08 +01:00
|
|
|
where
|
2019-01-14 14:22:25 +01:00
|
|
|
TIntoProtoHandler: IntoProtocolsHandler
|
2018-11-26 14:01:08 +01:00
|
|
|
{
|
|
|
|
/// Builds a `NodeHandlerWrapperBuilder`.
|
2019-04-16 15:57:29 +02:00
|
|
|
pub(crate) fn new(handler: TIntoProtoHandler) -> Self {
|
2018-11-26 14:01:08 +01:00
|
|
|
NodeHandlerWrapperBuilder {
|
|
|
|
handler,
|
2020-11-25 14:26:49 +01:00
|
|
|
substream_upgrade_protocol_override: None,
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:26:49 +01:00
|
|
|
|
|
|
|
pub(crate) fn with_substream_upgrade_protocol_override(
|
|
|
|
mut self,
|
|
|
|
version: Option<upgrade::Version>
|
|
|
|
) -> Self {
|
|
|
|
self.substream_upgrade_protocol_override = version;
|
|
|
|
self
|
|
|
|
}
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-31 01:51:27 +11:00
|
|
|
impl<TIntoProtoHandler, TProtoHandler> IntoConnectionHandler
|
2019-05-10 11:05:22 +02:00
|
|
|
for NodeHandlerWrapperBuilder<TIntoProtoHandler>
|
2019-01-14 14:22:25 +01:00
|
|
|
where
|
|
|
|
TIntoProtoHandler: IntoProtocolsHandler<Handler = TProtoHandler>,
|
|
|
|
TProtoHandler: ProtocolsHandler,
|
|
|
|
{
|
|
|
|
type Handler = NodeHandlerWrapper<TIntoProtoHandler::Handler>;
|
|
|
|
|
2020-10-31 01:51:27 +11:00
|
|
|
fn into_handler(self, connected: &Connected) -> Self::Handler {
|
2019-01-14 14:22:25 +01:00
|
|
|
NodeHandlerWrapper {
|
2020-10-31 01:51:27 +11:00
|
|
|
handler: self.handler.into_handler(&connected.peer_id, &connected.endpoint),
|
2020-10-09 19:37:17 +02:00
|
|
|
negotiating_in: Default::default(),
|
|
|
|
negotiating_out: Default::default(),
|
2019-01-14 14:22:25 +01:00
|
|
|
queued_dial_upgrades: Vec::new(),
|
|
|
|
unique_dial_upgrade_id: 0,
|
2019-04-20 16:00:21 +02:00
|
|
|
shutdown: Shutdown::None,
|
2020-11-25 14:26:49 +01:00
|
|
|
substream_upgrade_protocol_override: self.substream_upgrade_protocol_override,
|
2019-01-14 14:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
// A `ConnectionHandler` for an underlying `ProtocolsHandler`.
|
2018-11-26 14:01:08 +01:00
|
|
|
/// Wraps around an implementation of `ProtocolsHandler`, and implements `NodeHandler`.
|
|
|
|
// TODO: add a caching system for protocols that are supported or not
|
|
|
|
pub struct NodeHandlerWrapper<TProtoHandler>
|
|
|
|
where
|
|
|
|
TProtoHandler: ProtocolsHandler,
|
|
|
|
{
|
|
|
|
/// The underlying handler.
|
|
|
|
handler: TProtoHandler,
|
|
|
|
/// Futures that upgrade incoming substreams.
|
2020-10-09 19:37:17 +02:00
|
|
|
negotiating_in: FuturesUnordered<SubstreamUpgrade<
|
2020-08-23 16:57:20 +02:00
|
|
|
TProtoHandler::InboundOpenInfo,
|
|
|
|
InboundUpgradeApply<Substream<StreamMuxerBox>, SendWrapper<TProtoHandler::InboundProtocol>>,
|
2020-10-09 19:37:17 +02:00
|
|
|
>>,
|
|
|
|
/// Futures that upgrade outgoing substreams.
|
|
|
|
negotiating_out: FuturesUnordered<SubstreamUpgrade<
|
2018-11-26 14:01:08 +01:00
|
|
|
TProtoHandler::OutboundOpenInfo,
|
2020-02-07 16:29:30 +01:00
|
|
|
OutboundUpgradeApply<Substream<StreamMuxerBox>, SendWrapper<TProtoHandler::OutboundProtocol>>,
|
2020-10-09 19:37:17 +02:00
|
|
|
>>,
|
2018-11-26 14:01:08 +01:00
|
|
|
/// For each outbound substream request, how to upgrade it. The first element of the tuple
|
|
|
|
/// is the unique identifier (see `unique_dial_upgrade_id`).
|
2020-02-07 16:29:30 +01:00
|
|
|
queued_dial_upgrades: Vec<(u64, (upgrade::Version, SendWrapper<TProtoHandler::OutboundProtocol>))>,
|
2018-11-26 14:01:08 +01:00
|
|
|
/// Unique identifier assigned to each queued dial upgrade.
|
|
|
|
unique_dial_upgrade_id: u64,
|
2019-04-20 16:00:21 +02:00
|
|
|
/// The currently planned connection & handler shutdown.
|
|
|
|
shutdown: Shutdown,
|
2020-11-25 14:26:49 +01:00
|
|
|
/// The substream upgrade protocol override, if any.
|
|
|
|
substream_upgrade_protocol_override: Option<upgrade::Version>,
|
2019-04-20 16:00:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:37:17 +02:00
|
|
|
struct SubstreamUpgrade<UserData, Upgrade> {
|
|
|
|
user_data: Option<UserData>,
|
|
|
|
timeout: Delay,
|
|
|
|
upgrade: Upgrade,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<UserData, Upgrade> Unpin for SubstreamUpgrade<UserData, Upgrade> {}
|
|
|
|
|
|
|
|
impl<UserData, Upgrade, UpgradeOutput, TUpgradeError> Future for SubstreamUpgrade<UserData, Upgrade>
|
|
|
|
where
|
|
|
|
Upgrade: Future<Output = Result<UpgradeOutput, UpgradeError<TUpgradeError>>> + Unpin,
|
|
|
|
{
|
|
|
|
type Output = (UserData, Result<UpgradeOutput, ProtocolsHandlerUpgrErr<TUpgradeError>>);
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
match self.timeout.poll_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(_)) => return Poll::Ready((
|
|
|
|
self.user_data.take().expect("Future not to be polled again once ready."),
|
|
|
|
Err(ProtocolsHandlerUpgrErr::Timeout)),
|
|
|
|
),
|
|
|
|
Poll::Ready(Err(_)) => return Poll::Ready((
|
|
|
|
self.user_data.take().expect("Future not to be polled again once ready."),
|
|
|
|
Err(ProtocolsHandlerUpgrErr::Timer)),
|
|
|
|
),
|
|
|
|
Poll::Pending => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.upgrade.poll_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(upgrade)) => Poll::Ready((
|
|
|
|
self.user_data.take().expect("Future not to be polled again once ready."),
|
|
|
|
Ok(upgrade),
|
|
|
|
)),
|
|
|
|
Poll::Ready(Err(err)) => Poll::Ready((
|
|
|
|
self.user_data.take().expect("Future not to be polled again once ready."),
|
|
|
|
Err(ProtocolsHandlerUpgrErr::Upgrade(err)),
|
|
|
|
)),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-20 16:00:21 +02:00
|
|
|
/// The options for a planned connection & handler shutdown.
|
|
|
|
///
|
|
|
|
/// A shutdown is planned anew based on the the return value of
|
|
|
|
/// [`ProtocolsHandler::connection_keep_alive`] of the underlying handler
|
|
|
|
/// after every invocation of [`ProtocolsHandler::poll`].
|
|
|
|
///
|
|
|
|
/// A planned shutdown is always postponed for as long as there are ingoing
|
|
|
|
/// or outgoing substreams being negotiated, i.e. it is a graceful, "idle"
|
|
|
|
/// shutdown.
|
|
|
|
enum Shutdown {
|
|
|
|
/// No shutdown is planned.
|
|
|
|
None,
|
|
|
|
/// A shut down is planned as soon as possible.
|
|
|
|
Asap,
|
|
|
|
/// A shut down is planned for when a `Delay` has elapsed.
|
2019-09-16 11:08:44 +02:00
|
|
|
Later(Delay, Instant)
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 17:19:50 +01:00
|
|
|
/// Error generated by the `NodeHandlerWrapper`.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum NodeHandlerWrapperError<TErr> {
|
2020-03-31 15:41:13 +02:00
|
|
|
/// The connection handler encountered an error.
|
2019-03-11 17:19:50 +01:00
|
|
|
Handler(TErr),
|
2020-03-31 15:41:13 +02:00
|
|
|
/// The connection keep-alive timeout expired.
|
|
|
|
KeepAliveTimeout,
|
2019-03-11 17:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<TErr> From<TErr> for NodeHandlerWrapperError<TErr> {
|
|
|
|
fn from(err: TErr) -> NodeHandlerWrapperError<TErr> {
|
|
|
|
NodeHandlerWrapperError::Handler(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TErr> fmt::Display for NodeHandlerWrapperError<TErr>
|
|
|
|
where
|
|
|
|
TErr: fmt::Display
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
NodeHandlerWrapperError::Handler(err) => write!(f, "{}", err),
|
2020-03-31 15:41:13 +02:00
|
|
|
NodeHandlerWrapperError::KeepAliveTimeout =>
|
|
|
|
write!(f, "Connection closed due to expired keep-alive timeout."),
|
2019-03-11 17:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TErr> error::Error for NodeHandlerWrapperError<TErr>
|
|
|
|
where
|
|
|
|
TErr: error::Error + 'static
|
|
|
|
{
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
NodeHandlerWrapperError::Handler(err) => Some(err),
|
2020-03-31 15:41:13 +02:00
|
|
|
NodeHandlerWrapperError::KeepAliveTimeout => None,
|
2019-03-11 17:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
impl<TProtoHandler> ConnectionHandler for NodeHandlerWrapper<TProtoHandler>
|
2018-11-26 14:01:08 +01:00
|
|
|
where
|
|
|
|
TProtoHandler: ProtocolsHandler,
|
|
|
|
{
|
|
|
|
type InEvent = TProtoHandler::InEvent;
|
|
|
|
type OutEvent = TProtoHandler::OutEvent;
|
2019-03-11 17:19:50 +01:00
|
|
|
type Error = NodeHandlerWrapperError<TProtoHandler::Error>;
|
2020-02-07 16:29:30 +01:00
|
|
|
type Substream = Substream<StreamMuxerBox>;
|
2018-11-26 14:01:08 +01:00
|
|
|
// The first element of the tuple is the unique upgrade identifier
|
|
|
|
// (see `unique_dial_upgrade_id`).
|
2019-04-16 15:57:29 +02:00
|
|
|
type OutboundOpenInfo = (u64, TProtoHandler::OutboundOpenInfo, Duration);
|
2018-11-26 14:01:08 +01:00
|
|
|
|
|
|
|
fn inject_substream(
|
|
|
|
&mut self,
|
|
|
|
substream: Self::Substream,
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
endpoint: SubstreamEndpoint<Self::OutboundOpenInfo>,
|
2018-11-26 14:01:08 +01:00
|
|
|
) {
|
|
|
|
match endpoint {
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
SubstreamEndpoint::Listener => {
|
2018-11-26 14:01:08 +01:00
|
|
|
let protocol = self.handler.listen_protocol();
|
2019-04-16 15:57:29 +02:00
|
|
|
let timeout = protocol.timeout().clone();
|
2020-10-09 19:37:17 +02:00
|
|
|
let (_, upgrade, user_data) = protocol.into_upgrade();
|
2020-08-23 16:57:20 +02:00
|
|
|
let upgrade = upgrade::apply_inbound(substream, SendWrapper(upgrade));
|
2019-09-16 11:08:44 +02:00
|
|
|
let timeout = Delay::new(timeout);
|
2020-10-09 19:37:17 +02:00
|
|
|
self.negotiating_in.push(SubstreamUpgrade {
|
|
|
|
user_data: Some(user_data),
|
|
|
|
timeout,
|
|
|
|
upgrade,
|
|
|
|
});
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
SubstreamEndpoint::Dialer((upgrade_id, user_data, timeout)) => {
|
2018-11-26 14:01:08 +01:00
|
|
|
let pos = match self
|
|
|
|
.queued_dial_upgrades
|
|
|
|
.iter()
|
|
|
|
.position(|(id, _)| id == &upgrade_id)
|
|
|
|
{
|
|
|
|
Some(p) => p,
|
|
|
|
None => {
|
|
|
|
debug_assert!(false, "Received an upgrade with an invalid upgrade ID");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-25 14:26:49 +01:00
|
|
|
let (_, (mut version, upgrade)) = self.queued_dial_upgrades.remove(pos);
|
|
|
|
if let Some(v) = self.substream_upgrade_protocol_override {
|
|
|
|
if v != version {
|
|
|
|
log::debug!("Substream upgrade protocol override: {:?} -> {:?}", version, v);
|
|
|
|
version = v;
|
|
|
|
}
|
|
|
|
}
|
2019-10-10 11:31:44 +02:00
|
|
|
let upgrade = upgrade::apply_outbound(substream, upgrade, version);
|
2019-09-16 11:08:44 +02:00
|
|
|
let timeout = Delay::new(timeout);
|
2020-10-09 19:37:17 +02:00
|
|
|
self.negotiating_out.push(SubstreamUpgrade {
|
|
|
|
user_data: Some(user_data),
|
|
|
|
timeout,
|
|
|
|
upgrade,
|
|
|
|
});
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_event(&mut self, event: Self::InEvent) {
|
|
|
|
self.handler.inject_event(event);
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:10:53 +02:00
|
|
|
fn inject_address_change(&mut self, new_address: &Multiaddr) {
|
|
|
|
self.handler.inject_address_change(new_address);
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:27:33 +00:00
|
|
|
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
Result<ConnectionHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>
|
|
|
|
> {
|
2020-10-09 19:37:17 +02:00
|
|
|
while let Poll::Ready(Some((user_data, res))) = self.negotiating_in.poll_next_unpin(cx) {
|
|
|
|
match res {
|
|
|
|
Ok(upgrade) => self.handler.inject_fully_negotiated_inbound(upgrade, user_data),
|
|
|
|
Err(err) => self.handler.inject_listen_upgrade_error(user_data, err),
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:37:17 +02:00
|
|
|
while let Poll::Ready(Some((user_data, res))) = self.negotiating_out.poll_next_unpin(cx) {
|
|
|
|
match res {
|
|
|
|
Ok(upgrade) => self.handler.inject_fully_negotiated_outbound(upgrade, user_data),
|
|
|
|
Err(err) => self.handler.inject_dial_upgrade_error(user_data, err),
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
// Poll the handler at the end so that we see the consequences of the method
|
|
|
|
// calls on `self.handler`.
|
2019-09-16 11:08:44 +02:00
|
|
|
let poll_result = self.handler.poll(cx);
|
2019-01-04 12:02:39 +01:00
|
|
|
|
2019-04-20 16:00:21 +02:00
|
|
|
// Ask the handler whether it wants the connection (and the handler itself)
|
|
|
|
// to be kept alive, which determines the planned shutdown, if any.
|
2019-04-21 15:48:50 +02:00
|
|
|
match (&mut self.shutdown, self.handler.connection_keep_alive()) {
|
2019-09-16 11:08:44 +02:00
|
|
|
(Shutdown::Later(timer, deadline), KeepAlive::Until(t)) =>
|
|
|
|
if *deadline != t {
|
|
|
|
*deadline = t;
|
|
|
|
timer.reset_at(t)
|
2019-04-21 15:48:50 +02:00
|
|
|
},
|
2019-09-16 11:08:44 +02:00
|
|
|
(_, KeepAlive::Until(t)) => self.shutdown = Shutdown::Later(Delay::new_at(t), t),
|
2019-04-23 11:58:49 +02:00
|
|
|
(_, KeepAlive::No) => self.shutdown = Shutdown::Asap,
|
|
|
|
(_, KeepAlive::Yes) => self.shutdown = Shutdown::None
|
2019-03-11 17:19:50 +01:00
|
|
|
};
|
2018-11-26 14:01:08 +01:00
|
|
|
|
2019-03-11 17:19:50 +01:00
|
|
|
match poll_result {
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(ProtocolsHandlerEvent::Custom(event)) => {
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
return Poll::Ready(Ok(ConnectionHandlerEvent::Custom(event)));
|
2019-03-11 17:19:50 +01:00
|
|
|
}
|
2020-08-23 16:57:20 +02:00
|
|
|
Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol }) => {
|
2019-03-11 17:19:50 +01:00
|
|
|
let id = self.unique_dial_upgrade_id;
|
2019-04-16 15:57:29 +02:00
|
|
|
let timeout = protocol.timeout().clone();
|
2019-03-11 17:19:50 +01:00
|
|
|
self.unique_dial_upgrade_id += 1;
|
2020-08-23 16:57:20 +02:00
|
|
|
let (version, upgrade, info) = protocol.into_upgrade();
|
2020-02-07 16:29:30 +01:00
|
|
|
self.queued_dial_upgrades.push((id, (version, SendWrapper(upgrade))));
|
2019-09-16 11:08:44 +02:00
|
|
|
return Poll::Ready(Ok(
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
ConnectionHandlerEvent::OutboundSubstreamRequest((id, info, timeout)),
|
2019-03-11 17:19:50 +01:00
|
|
|
));
|
|
|
|
}
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(ProtocolsHandlerEvent::Close(err)) => return Poll::Ready(Err(err.into())),
|
|
|
|
Poll::Pending => (),
|
2019-03-11 17:19:50 +01:00
|
|
|
};
|
2019-01-25 15:56:32 +01:00
|
|
|
|
2019-04-20 16:00:21 +02:00
|
|
|
// Check if the connection (and handler) should be shut down.
|
|
|
|
// As long as we're still negotiating substreams, shutdown is always postponed.
|
|
|
|
if self.negotiating_in.is_empty() && self.negotiating_out.is_empty() {
|
|
|
|
match self.shutdown {
|
|
|
|
Shutdown::None => {},
|
2020-03-31 15:41:13 +02:00
|
|
|
Shutdown::Asap => return Poll::Ready(Err(NodeHandlerWrapperError::KeepAliveTimeout)),
|
2019-09-16 11:08:44 +02:00
|
|
|
Shutdown::Later(ref mut delay, _) => match Future::poll(Pin::new(delay), cx) {
|
2020-03-31 15:41:13 +02:00
|
|
|
Poll::Ready(_) => return Poll::Ready(Err(NodeHandlerWrapperError::KeepAliveTimeout)),
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending => {}
|
2019-01-04 12:02:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|