Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm: Add FromFn ConnectionHandler #2852

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2c580d9
Add `ReadyUpgrade`
thomaseizinger Aug 24, 2022
b2d1c05
Add basic `FromFn` `ConnectionHandler`
thomaseizinger Aug 24, 2022
230fabb
Add `TState` abstraction
thomaseizinger Aug 28, 2022
58fe45b
Publicly expose types
thomaseizinger Aug 28, 2022
ff1db42
Fix docs
thomaseizinger Aug 28, 2022
1b175d2
Implement limit for max inbound streams and pending dials
thomaseizinger Aug 30, 2022
69fa94f
Implement std::error::Error for `OpenError`
thomaseizinger Aug 30, 2022
54e6437
Avoid dial terminology for streams
thomaseizinger Sep 5, 2022
635730a
Remove `idle_waker`
thomaseizinger Sep 5, 2022
b37fa5c
Make `TState` configurable
thomaseizinger Sep 5, 2022
d4079bf
Finish local work before producing new work
thomaseizinger Sep 5, 2022
bc361ec
Introduce `FromFnProto`
thomaseizinger Sep 5, 2022
712180b
Expose `remote_peer_id` and `connected_point` to closures
thomaseizinger Sep 5, 2022
f9f8e75
Merge branch 'master' into from-fn-connection-handler
thomaseizinger Nov 2, 2022
5d7f0bd
Implement `Shared` abstraction to automatically share state
thomaseizinger Nov 2, 2022
8ee59fd
Don't allow ConnectionHandlers to modify the state
thomaseizinger Nov 2, 2022
7fb4387
Implement test
thomaseizinger Nov 2, 2022
ac5c236
Remove unnecessary waker
thomaseizinger Nov 2, 2022
3e81e72
Separate clone-able registration data
thomaseizinger Nov 2, 2022
49c6eb1
WIP: Migrate `libp2p-rendezvous` to `from_fn`
thomaseizinger Nov 2, 2022
411c0d7
fixup! WIP: Migrate `libp2p-rendezvous` to `from_fn`
thomaseizinger Nov 11, 2022
5e185d8
fixup! WIP: Migrate `libp2p-rendezvous` to `from_fn`
thomaseizinger Nov 11, 2022
bdbeadc
Enforce the use of `Shared`
thomaseizinger Nov 11, 2022
660c0a3
Share state between connection handlers
thomaseizinger Nov 11, 2022
f5a3c50
Introduce builder pattern
thomaseizinger Nov 11, 2022
df74f14
Update docs
thomaseizinger Nov 11, 2022
f84388f
Make inbound streams impossible when no handler is configured
thomaseizinger Nov 11, 2022
6d1add5
Allow for streaming protocols
thomaseizinger Nov 11, 2022
913caf3
Pass `ConnectedPoint` by value
thomaseizinger Nov 11, 2022
e9cd5fc
Allow regular async functions to be used as handlers
thomaseizinger Nov 11, 2022
5801576
Flatten errors
thomaseizinger Nov 12, 2022
c15433f
Implement `libp2p-ping` with `from_fn` abstraction
thomaseizinger Nov 12, 2022
a418d2f
Merge branch 'master' into from-fn-connection-handler
thomaseizinger Nov 16, 2022
628e9ab
Fully migrate rendezvous
thomaseizinger Nov 16, 2022
7bdd953
Minimise diff
thomaseizinger Nov 16, 2022
4485c48
Merge branch 'master' into from-fn-connection-handler
thomaseizinger Nov 18, 2022
6f00703
Update to new behaviour interface
thomaseizinger Nov 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mod from_fn;
mod map;
mod optional;
mod pending;
mod ready;
mod select;
mod transfer;

Expand All @@ -79,6 +80,7 @@ pub use self::{
map::{MapInboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgrade, MapOutboundUpgradeErr},
optional::OptionalUpgrade,
pending::PendingUpgrade,
ready::ReadyUpgrade,
select::SelectUpgrade,
transfer::{read_length_prefixed, read_varint, write_length_prefixed, write_varint},
};
Expand Down
75 changes: 75 additions & 0 deletions core/src/upgrade/ready.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2022 Protocol Labs.
// Copyright 2017-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.

use crate::upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeInfo};
use futures::future;
use std::iter;
use void::Void;

/// Implementation of [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] that directly yields the substream.
#[derive(Debug, Copy, Clone)]
pub struct ReadyUpgrade<P> {
protocol_name: P,
}

impl<P> ReadyUpgrade<P> {
pub fn new(protocol_name: P) -> Self {
Self { protocol_name }
}
}

impl<P> UpgradeInfo for ReadyUpgrade<P>
where
P: ProtocolName + Clone,
{
type Info = P;
type InfoIter = iter::Once<P>;

fn protocol_info(&self) -> Self::InfoIter {
iter::once(self.protocol_name.clone())
}
}

impl<C, P> InboundUpgrade<C> for ReadyUpgrade<P>
where
P: ProtocolName + Clone,
{
type Output = C;
type Error = Void;
type Future = future::Ready<Result<Self::Output, Self::Error>>;

fn upgrade_inbound(self, stream: C, _: Self::Info) -> Self::Future {
future::ready(Ok(stream))
}
}

impl<C, P> OutboundUpgrade<C> for ReadyUpgrade<P>
where
P: ProtocolName + Clone,
{
type Output = C;
type Error = Void;
type Future = future::Ready<Result<Self::Output, Self::Error>>;

fn upgrade_outbound(self, stream: C, _: Self::Info) -> Self::Future {
future::ready(Ok(stream))
}
}
1 change: 1 addition & 0 deletions swarm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

mod dummy;
pub mod either;
pub mod from_fn;
mod map_in;
mod map_out;
pub mod multi;
Expand Down
Loading