mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 17:51:35 +00:00
core/src/transport: Poll Transport directly, remove Transport::Listener (#2652)
Remove the concept of individual `Transport::Listener` streams from `Transport`. Instead the `Transport` is polled directly via `Transport::poll`. The `Transport` is now responsible for driving its listeners.
This commit is contained in:
@ -20,7 +20,7 @@
|
||||
|
||||
use crate::{
|
||||
core::{
|
||||
transport::{ListenerEvent, TransportError},
|
||||
transport::{TransportError, TransportEvent},
|
||||
Transport,
|
||||
},
|
||||
Multiaddr,
|
||||
@ -31,6 +31,7 @@ use futures::{
|
||||
prelude::*,
|
||||
ready,
|
||||
};
|
||||
use libp2p_core::transport::ListenerId;
|
||||
use std::{
|
||||
convert::TryFrom as _,
|
||||
io,
|
||||
@ -45,7 +46,9 @@ use std::{
|
||||
/// Wraps around a `Transport` and counts the number of bytes that go through all the opened
|
||||
/// connections.
|
||||
#[derive(Clone)]
|
||||
#[pin_project::pin_project]
|
||||
pub struct BandwidthLogging<TInner> {
|
||||
#[pin]
|
||||
inner: TInner,
|
||||
sinks: Arc<BandwidthSinks>,
|
||||
}
|
||||
@ -73,18 +76,32 @@ where
|
||||
{
|
||||
type Output = BandwidthConnecLogging<TInner::Output>;
|
||||
type Error = TInner::Error;
|
||||
type Listener = BandwidthListener<TInner::Listener>;
|
||||
type ListenerUpgrade = BandwidthFuture<TInner::ListenerUpgrade>;
|
||||
type Dial = BandwidthFuture<TInner::Dial>;
|
||||
|
||||
fn listen_on(
|
||||
&mut self,
|
||||
addr: Multiaddr,
|
||||
) -> Result<Self::Listener, TransportError<Self::Error>> {
|
||||
let sinks = self.sinks.clone();
|
||||
self.inner
|
||||
.listen_on(addr)
|
||||
.map(move |inner| BandwidthListener { inner, sinks })
|
||||
fn poll(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>> {
|
||||
let this = self.project();
|
||||
match this.inner.poll(cx) {
|
||||
Poll::Ready(event) => {
|
||||
let event = event.map_upgrade({
|
||||
let sinks = this.sinks.clone();
|
||||
|inner| BandwidthFuture { inner, sinks }
|
||||
});
|
||||
Poll::Ready(event)
|
||||
}
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
}
|
||||
|
||||
fn listen_on(&mut self, addr: Multiaddr) -> Result<ListenerId, TransportError<Self::Error>> {
|
||||
self.inner.listen_on(addr)
|
||||
}
|
||||
|
||||
fn remove_listener(&mut self, id: ListenerId) -> bool {
|
||||
self.inner.remove_listener(id)
|
||||
}
|
||||
|
||||
fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
|
||||
@ -109,39 +126,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps around a `Stream` that produces connections. Wraps each connection around a bandwidth
|
||||
/// counter.
|
||||
#[pin_project::pin_project]
|
||||
pub struct BandwidthListener<TInner> {
|
||||
#[pin]
|
||||
inner: TInner,
|
||||
sinks: Arc<BandwidthSinks>,
|
||||
}
|
||||
|
||||
impl<TInner, TConn, TErr> Stream for BandwidthListener<TInner>
|
||||
where
|
||||
TInner: TryStream<Ok = ListenerEvent<TConn, TErr>, Error = TErr>,
|
||||
{
|
||||
type Item = Result<ListenerEvent<BandwidthFuture<TConn>, TErr>, TErr>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let this = self.project();
|
||||
|
||||
let event = if let Some(event) = ready!(this.inner.try_poll_next(cx)?) {
|
||||
event
|
||||
} else {
|
||||
return Poll::Ready(None);
|
||||
};
|
||||
|
||||
let event = event.map({
|
||||
let sinks = this.sinks.clone();
|
||||
|inner| BandwidthFuture { inner, sinks }
|
||||
});
|
||||
|
||||
Poll::Ready(Some(Ok(event)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps around a `Future` that produces a connection. Wraps the connection around a bandwidth
|
||||
/// counter.
|
||||
#[pin_project::pin_project]
|
||||
|
Reference in New Issue
Block a user