mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-20 22:51:20 +00:00
minimal host interface
This commit is contained in:
parent
603fcf6557
commit
f6b53ec586
@ -4,4 +4,4 @@ version = "0.1.0"
|
|||||||
authors = ["Parity Technologies <admin@parity.io>"]
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["libp2p-transport"]
|
members = ["libp2p-transport", "libp2p-host"]
|
@ -2,7 +2,6 @@
|
|||||||
name = "libp2p-transport"
|
name = "libp2p-transport"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Parity Technologies <admin@parity.io>"]
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
multiaddr = "0.2.0"
|
multiaddr = "0.2.0"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
//! Rust libp2p implementation
|
//! Transport and I/O primitives for libp2p.
|
||||||
|
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
@ -21,10 +21,6 @@ pub type PeerId = String;
|
|||||||
/// A logical wire between us and a peer. We can read and write through this asynchronously.
|
/// A logical wire between us and a peer. We can read and write through this asynchronously.
|
||||||
///
|
///
|
||||||
/// You can have multiple `Socket`s between you and any given peer.
|
/// You can have multiple `Socket`s between you and any given peer.
|
||||||
//
|
|
||||||
// What do we call it? Go-libp2p calls them "Streams", but "Streams" in futures-rs specifically
|
|
||||||
// denote the "writing" half. "Connections" have a different meaning in the libp2p definition.
|
|
||||||
// `Duplex`?
|
|
||||||
pub trait Socket: AsyncRead + AsyncWrite {
|
pub trait Socket: AsyncRead + AsyncWrite {
|
||||||
/// Get the protocol ID this socket uses.
|
/// Get the protocol ID this socket uses.
|
||||||
fn protocol_id(&self) -> ProtocolId;
|
fn protocol_id(&self) -> ProtocolId;
|
||||||
@ -42,61 +38,25 @@ pub trait Conn {
|
|||||||
fn make_socket(&self, proto: ProtocolId) -> BoxFuture<Self::Socket, IoError>;
|
fn make_socket(&self, proto: ProtocolId) -> BoxFuture<Self::Socket, IoError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Produces a future for each incoming `Socket`.
|
|
||||||
pub trait Handler<S: Socket> {
|
|
||||||
type Future: IntoFuture<Item=(), Error=()>;
|
|
||||||
|
|
||||||
/// Handle the incoming socket, producing a future which should resolve
|
|
||||||
/// when the handler is finished.
|
|
||||||
fn handle(&self, socket: S) -> Self::Future;
|
|
||||||
fn boxed(self) -> BoxHandler<S> where
|
|
||||||
Self: Sized + Send + 'static,
|
|
||||||
<Self::Future as IntoFuture>::Future: Send + 'static
|
|
||||||
{
|
|
||||||
BoxHandler(Box::new(move |socket|
|
|
||||||
self.handle(socket).into_future().boxed()
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: Socket, F, U> Handler<S> for F
|
|
||||||
where F: Fn(S) -> U, U: IntoFuture<Item=(), Error=()>
|
|
||||||
{
|
|
||||||
type Future = U;
|
|
||||||
|
|
||||||
fn handle(&self, socket: S) -> U { (self)(socket) }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A boxed handler.
|
|
||||||
pub struct BoxHandler<S: Socket>(Box<Handler<S, Future=BoxFuture<(), ()>>>);
|
|
||||||
|
|
||||||
impl<S: Socket> Handler<S> for BoxHandler<S> {
|
|
||||||
type Future = BoxFuture<(), ()>;
|
|
||||||
|
|
||||||
fn handle(&self, socket: S) -> Self::Future {
|
|
||||||
self.0.handle(socket)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Multiplexes sockets onto handlers by protocol-id.
|
|
||||||
pub trait Mux: Sync {
|
|
||||||
/// The socket type this manages.
|
|
||||||
type Socket: Socket;
|
|
||||||
|
|
||||||
/// Attach an incoming socket.
|
|
||||||
fn push(&self, socket: Self::Socket);
|
|
||||||
|
|
||||||
/// Set the socket handler for a given protocol id.
|
|
||||||
fn set_handler(&self, proto: ProtocolId, handler: BoxHandler<Self::Socket>);
|
|
||||||
|
|
||||||
/// Remove the socket handler for the given protocol id, returning the old handler if it existed.
|
|
||||||
fn remove_handler(&self, proto: &ProtocolId) -> Option<BoxHandler<Self::Socket>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MultiAddr; // stub for multiaddr crate type.
|
pub struct MultiAddr; // stub for multiaddr crate type.
|
||||||
|
|
||||||
/// A transport is a stream producing incoming connections.
|
/// A transport is a stream producing incoming connections.
|
||||||
/// These are transports or wrappers around them.
|
/// These are transports or wrappers around them.
|
||||||
|
//
|
||||||
|
// Listening/Dialing hasn't really been sorted out yet.
|
||||||
|
// It's easy to do for simple multiaddrs, but for complex ones,
|
||||||
|
// particularly those with multiple hops, things get much fuzzier.
|
||||||
|
//
|
||||||
|
// One example which is difficult to make work is something like
|
||||||
|
// `ip4/1.2.3.4/tcp/8888/p2p-circuit/p2p/DestPeer`
|
||||||
|
//
|
||||||
|
// This address, when used for dialing, says "Connect to the peer DestPeer
|
||||||
|
// on any available address, through a relay node we will connect to via
|
||||||
|
// tcp on port 8888 over the ipv4 address 1.2.3.4"
|
||||||
|
//
|
||||||
|
// We'll need to require dialers to handle the whole address,
|
||||||
|
// and give them a closure or similar required to instantiate connections
|
||||||
|
// to different multi-addresses.
|
||||||
pub trait Transport {
|
pub trait Transport {
|
||||||
/// The raw connection.
|
/// The raw connection.
|
||||||
type RawConn: AsyncRead + AsyncWrite;
|
type RawConn: AsyncRead + AsyncWrite;
|
||||||
@ -113,6 +73,6 @@ pub trait Transport {
|
|||||||
|
|
||||||
/// Dial to the given multi-addr.
|
/// Dial to the given multi-addr.
|
||||||
/// Returns either a future which may resolve to a connection,
|
/// Returns either a future which may resolve to a connection,
|
||||||
/// or
|
/// or gives back the multiaddress.
|
||||||
fn dial(&mut self, addr: MultiAddr) -> Result<Self::Dial, MultiAddr>;
|
fn dial(&mut self, addr: MultiAddr) -> Result<Self::Dial, MultiAddr>;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user