#![doc(html_logo_url = "https://susyp2p.io/img/logo_small.png")]
#![doc(html_favicon_url = "https://susyp2p.io/img/favicon.png")]
pub use bytes;
pub use futures;
#[doc(inline)]
pub use multiaddr;
#[doc(inline)]
pub use multihash;
pub use tokio_io;
pub use tokio_codec;
#[doc(inline)]
pub use susyp2p_core as core;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[doc(inline)]
pub use susyp2p_dns as dns;
#[doc(inline)]
pub use susyp2p_identify as identify;
#[doc(inline)]
pub use susyp2p_kad as kad;
#[doc(inline)]
pub use susyp2p_floodsub as floodsub;
#[doc(inline)]
pub use susyp2p_mplex as mplex;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[doc(inline)]
pub use susyp2p_mdns as mdns;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[doc(inline)]
pub use susyp2p_noise as noise;
#[doc(inline)]
pub use susyp2p_ping as ping;
#[doc(inline)]
pub use susyp2p_plaintext as plaintext;
#[doc(inline)]
pub use susyp2p_ratelimit as ratelimit;
#[doc(inline)]
pub use susyp2p_secio as secio;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[doc(inline)]
pub use susyp2p_tcp as tcp;
#[doc(inline)]
pub use susyp2p_uds as uds;
#[cfg(feature = "susyp2p-websocket")]
#[doc(inline)]
pub use susyp2p_websocket as websocket;
#[doc(inline)]
pub use susyp2p_yamux as yamux;
mod transport_ext;
pub mod bandwidth;
pub mod simple;
pub use self::core::{
identity,
Transport, PeerId, Swarm,
transport::TransportError,
upgrade::{InboundUpgrade, InboundUpgradeExt, OutboundUpgrade, OutboundUpgradeExt}
};
pub use susyp2p_core_derive::NetworkBehaviour;
pub use self::multiaddr::{Multiaddr, multiaddr as build_multiaddr};
pub use self::simple::SimpleProtocol;
pub use self::transport_ext::TransportExt;
use futures::prelude::*;
use std::{error, time::Duration};
pub fn build_development_transport(keypair: identity::Keypair)
-> impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send> + Send + Sync), Error = impl error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone
{
build_tcp_ws_secio_mplex_yamux(keypair)
}
pub fn build_tcp_ws_secio_mplex_yamux(keypair: identity::Keypair)
-> impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send> + Send + Sync), Error = impl error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone
{
CommonTransport::new()
.with_upgrade(secio::SecioConfig::new(keypair))
.and_then(move |out, endpoint| {
let peer_id = PeerId::from(out.remote_key);
let peer_id2 = peer_id.clone();
let upgrade = core::upgrade::SelectUpgrade::new(yamux::Config::default(), mplex::MplexConfig::new())
.map_inbound(move |muxer| (peer_id, muxer))
.map_outbound(move |muxer| (peer_id2, muxer));
core::upgrade::apply(out.stream, upgrade, endpoint)
.map(|(id, muxer)| (id, core::muxing::StreamMuxerBox::new(muxer)))
})
.with_timeout(Duration::from_secs(20))
}
#[derive(Debug, Clone)]
struct CommonTransport {
inner: CommonTransportInner
}
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "susyp2p-websocket"))]
type InnerImplementation = core::transport::OrTransport<dns::DnsConfig<tcp::TcpConfig>, websocket::WsConfig<dns::DnsConfig<tcp::TcpConfig>>>;
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), not(feature = "susyp2p-websocket")))]
type InnerImplementation = dns::DnsConfig<tcp::TcpConfig>;
#[cfg(all(any(target_os = "emscripten", target_os = "unknown"), feature = "susyp2p-websocket"))]
type InnerImplementation = websocket::BrowserWsConfig;
#[cfg(all(any(target_os = "emscripten", target_os = "unknown"), not(feature = "susyp2p-websocket")))]
type InnerImplementation = core::transport::dummy::DummyTransport;
#[derive(Debug, Clone)]
struct CommonTransportInner {
inner: InnerImplementation,
}
impl CommonTransport {
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
pub fn new() -> CommonTransport {
let tcp = tcp::TcpConfig::new().nodelay(true);
let transport = dns::DnsConfig::new(tcp);
#[cfg(feature = "susyp2p-websocket")]
let transport = {
let trans_clone = transport.clone();
transport.or_transport(websocket::WsConfig::new(trans_clone))
};
CommonTransport {
inner: CommonTransportInner { inner: transport }
}
}
#[cfg(all(any(target_os = "emscripten", target_os = "unknown"), feature = "susyp2p-websocket"))]
pub fn new() -> CommonTransport {
let inner = websocket::BrowserWsConfig::new();
CommonTransport {
inner: CommonTransportInner { inner }
}
}
#[cfg(all(any(target_os = "emscripten", target_os = "unknown"), not(feature = "susyp2p-websocket")))]
pub fn new() -> CommonTransport {
let inner = core::transport::dummy::DummyTransport::new();
CommonTransport {
inner: CommonTransportInner { inner }
}
}
}
impl Transport for CommonTransport {
type Output = <InnerImplementation as Transport>::Output;
type Error = <InnerImplementation as Transport>::Error;
type Listener = <InnerImplementation as Transport>::Listener;
type ListenerUpgrade = <InnerImplementation as Transport>::ListenerUpgrade;
type Dial = <InnerImplementation as Transport>::Dial;
fn listen_on(self, addr: Multiaddr) -> Result<Self::Listener, TransportError<Self::Error>> {
self.inner.inner.listen_on(addr)
}
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
self.inner.inner.dial(addr)
}
}