Max Inden 7fc342e6c0
{core,swarm}: Remove Network abstraction (#2492)
This commit removes the `Network` abstraction, thus managing `Listeners`
and the connection `Pool` in `Swarm` directly. This is done under the
assumption that noone uses the `Network` abstraction directly, but
instead everyone always uses it through `Swarm`. Both `Listeners` and
`Pool` are moved from `libp2p-core` into `libp2p-swarm`. Given that they
are no longer exposed via `Network`, they can be treated as an
implementation detail of `libp2p-swarm` and `Swarm`.

This change does not include any behavioural changes.

This change has the followin benefits:

- Removal of `NetworkEvent`, which was mostly an isomorphism of
  `SwarmEvent`.
- Removal of the never-directly-used `Network` abstraction.
- Removal of now obsolete verbose `Peer` (`core/src/network/peer.rs`)
  construct.
- Removal of `libp2p-core` `DialOpts`, which is a direct mapping of
  `libp2p-swarm` `DialOpts`.
- Allowing breaking changes to the connection handling and `Swarm` API
  interface without a breaking change in `libp2p-core` and thus a
  without a breaking change in `/transport` protocols.

This change enables the following potential future changes:

- Removal of `NodeHandler` and `ConnectionHandler`. Thus allowing to
  rename `ProtocolsHandler` into `ConnectionHandler`.
- Moving `NetworkBehaviour` and `ProtocolsHandler` into `libp2p-core`,
  having `libp2p-xxx` protocol crates only depend on `libp2p-core` and
  thus allowing general breaking changes to `Swarm` without breaking all
  `libp2p-xxx` crates.
2022-02-13 21:57:38 +01:00

48 lines
1.1 KiB
Rust

#![allow(dead_code)]
use futures::prelude::*;
use libp2p_core::muxing::StreamMuxer;
use std::{pin::Pin, task::Context, task::Poll};
pub struct CloseMuxer<M> {
state: CloseMuxerState<M>,
}
impl<M> CloseMuxer<M> {
pub fn new(m: M) -> CloseMuxer<M> {
CloseMuxer {
state: CloseMuxerState::Close(m),
}
}
}
pub enum CloseMuxerState<M> {
Close(M),
Done,
}
impl<M> Future for CloseMuxer<M>
where
M: StreamMuxer,
M::Error: From<std::io::Error>,
{
type Output = Result<M, M::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
match std::mem::replace(&mut self.state, CloseMuxerState::Done) {
CloseMuxerState::Close(muxer) => {
if !muxer.close(cx)?.is_ready() {
self.state = CloseMuxerState::Close(muxer);
return Poll::Pending;
}
return Poll::Ready(Ok(muxer));
}
CloseMuxerState::Done => panic!(),
}
}
}
}
impl<M> Unpin for CloseMuxer<M> {}