2017-11-07 18:25:10 +01:00
|
|
|
// Copyright 2017 Parity Technologies (UK) Ltd.
|
2018-03-07 16:20:55 +01:00
|
|
|
//
|
|
|
|
// 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
|
2017-11-08 10:11:58 +01:00
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
2018-03-07 16:20:55 +01:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
2017-11-08 10:11:58 +01:00
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
2018-03-07 16:20:55 +01:00
|
|
|
// 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
|
2017-11-08 10:11:58 +01:00
|
|
|
// DEALINGS IN THE SOFTWARE.
|
2017-11-07 18:25:10 +01:00
|
|
|
|
|
|
|
//! # Multistream-select
|
|
|
|
//!
|
2017-12-01 12:12:34 +01:00
|
|
|
//! This crate implements the `multistream-select` protocol, which is the protocol used by libp2p
|
|
|
|
//! to negotiate which protocol to use with the remote.
|
|
|
|
//!
|
|
|
|
//! > **Note**: This crate is used by the internals of *libp2p*, and it is not required to
|
|
|
|
//! > understand it in order to use *libp2p*.
|
|
|
|
//!
|
|
|
|
//! Whenever a new connection or a new multiplexed substream is opened, libp2p uses
|
|
|
|
//! `multistream-select` to negotiate with the remote which protocol to use. After a protocol has
|
2018-11-03 02:40:00 +11:00
|
|
|
//! been successfully negotiated, the stream (i.e. the connection or the multiplexed substream)
|
2017-12-01 12:12:34 +01:00
|
|
|
//! immediately stops using `multistream-select` and starts using the negotiated protocol.
|
|
|
|
//!
|
|
|
|
//! ## Protocol explanation
|
|
|
|
//!
|
|
|
|
//! The dialer has two options available: either request the list of protocols that the listener
|
|
|
|
//! supports, or suggest a protocol. If a protocol is suggested, the listener can either accept (by
|
|
|
|
//! answering with the same protocol name) or refuse the choice (by answering "not available").
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2017-12-01 12:12:34 +01:00
|
|
|
//! ## Examples
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2017-12-01 12:12:34 +01:00
|
|
|
//! For a dialer:
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2017-12-01 12:12:34 +01:00
|
|
|
//! ```no_run
|
|
|
|
//! # fn main() {
|
|
|
|
//! use bytes::Bytes;
|
|
|
|
//! use multistream_select::dialer_select_proto;
|
|
|
|
//! use futures::{Future, Sink, Stream};
|
2018-07-16 12:15:27 +02:00
|
|
|
//! use tokio_tcp::TcpStream;
|
2018-10-25 05:26:37 -04:00
|
|
|
//! use tokio::runtime::current_thread::Runtime;
|
2017-12-01 12:12:34 +01:00
|
|
|
//!
|
|
|
|
//! #[derive(Debug, Copy, Clone)]
|
|
|
|
//! enum MyProto { Echo, Hello }
|
|
|
|
//!
|
2018-07-16 12:15:27 +02:00
|
|
|
//! let client = TcpStream::connect(&"127.0.0.1:10333".parse().unwrap())
|
2017-12-01 12:12:34 +01:00
|
|
|
//! .from_err()
|
|
|
|
//! .and_then(move |connec| {
|
|
|
|
//! let protos = vec![
|
|
|
|
//! (Bytes::from("/echo/1.0.0"), <Bytes as PartialEq>::eq, MyProto::Echo),
|
|
|
|
//! (Bytes::from("/hello/2.5.0"), <Bytes as PartialEq>::eq, MyProto::Hello),
|
|
|
|
//! ]
|
|
|
|
//! .into_iter();
|
|
|
|
//! dialer_select_proto(connec, protos).map(|r| r.0)
|
|
|
|
//! });
|
|
|
|
//!
|
2018-10-25 05:26:37 -04:00
|
|
|
//! let mut rt = Runtime::new().unwrap();
|
|
|
|
//! let negotiated_protocol: MyProto = rt.block_on(client)
|
2018-07-16 12:15:27 +02:00
|
|
|
//! .expect("failed to find a protocol");
|
2017-12-01 12:12:34 +01:00
|
|
|
//! println!("negotiated: {:?}", negotiated_protocol);
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-11-07 18:25:10 +01:00
|
|
|
|
2017-11-05 12:21:34 +01:00
|
|
|
mod dialer_select;
|
|
|
|
mod error;
|
2017-11-10 11:10:10 +01:00
|
|
|
mod length_delimited;
|
2017-11-05 12:21:34 +01:00
|
|
|
mod listener_select;
|
2017-11-07 10:26:45 +01:00
|
|
|
mod tests;
|
2017-11-05 12:21:34 +01:00
|
|
|
|
|
|
|
pub mod protocol;
|
|
|
|
|
2018-08-30 23:25:16 +02:00
|
|
|
pub use self::dialer_select::{dialer_select_proto, DialerSelectFuture};
|
2017-11-05 12:21:34 +01:00
|
|
|
pub use self::error::ProtocolChoiceError;
|
2018-08-30 23:25:16 +02:00
|
|
|
pub use self::listener_select::{listener_select_proto, ListenerSelectFuture};
|