Skip to content

Commit

Permalink
Replace the macro in libp2p-tcp with a generic, inner implementation
Browse files Browse the repository at this point in the history
To avoid code duplication, a generic inner implementation is used
that does all of the heavy lifting. To abstract over the individual
types, we create traits that are private to the module.

Consumers of the crate shouldn't be concerned with this because
a) none of the generic types are exported
b) there are no type parameters in public interfaces
  • Loading branch information
thomaseizinger committed Mar 3, 2020
1 parent 7cbb3cf commit 82ff15d
Show file tree
Hide file tree
Showing 20 changed files with 946 additions and 658 deletions.
2 changes: 1 addition & 1 deletion core/src/nodes/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use std::{collections::VecDeque, fmt, pin::Pin};
/// use futures::prelude::*;
/// use libp2p_core::nodes::listeners::{ListenersEvent, ListenersStream};
///
/// let mut listeners = ListenersStream::new(libp2p_tcp::TcpConfig::new());
/// let mut listeners = ListenersStream::new(libp2p_tcp::async_std::TcpConfig::new());
///
/// // Ask the `listeners` to start listening on the given multiaddress.
/// listeners.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions core/tests/network_dial_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn deny_incoming_connec() {
let mut swarm1: Network<_, _, _, NodeHandlerWrapperBuilder<TestHandler>, _, _> = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand All @@ -97,7 +97,7 @@ fn deny_incoming_connec() {
let mut swarm2 = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand Down Expand Up @@ -162,7 +162,7 @@ fn dial_self() {
let mut swarm = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand Down Expand Up @@ -239,7 +239,7 @@ fn dial_self_by_id() {
let mut swarm: Network<_, _, _, NodeHandlerWrapperBuilder<TestHandler>, _, _> = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand All @@ -258,7 +258,7 @@ fn multiple_addresses_err() {
let mut swarm = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand Down
4 changes: 2 additions & 2 deletions core/tests/network_simult.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn raw_swarm_simultaneous_connect() {
let mut swarm1 = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1Lazy)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand All @@ -109,7 +109,7 @@ fn raw_swarm_simultaneous_connect() {
let mut swarm2 = {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let transport = libp2p_tcp::TcpConfig::new()
let transport = libp2p_tcp::async_std::TcpConfig::new()
.upgrade(upgrade::Version::V1Lazy)
.authenticate(libp2p_secio::SecioConfig::new(local_key))
.multiplex(libp2p_mplex::MplexConfig::new())
Expand Down
2 changes: 1 addition & 1 deletion examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use libp2p::{
pnet::{PnetConfig, PreSharedKey},
secio::SecioConfig,
swarm::NetworkBehaviourEventProcess,
tcp::TcpConfig,
tcp::async_std::TcpConfig,
yamux::Config as YamuxConfig,
Multiaddr, NetworkBehaviour, PeerId, Swarm, Transport,
};
Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/tests/async_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.

use libp2p_core::{muxing, upgrade, Transport};
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use futures::{prelude::*, channel::oneshot};
use std::sync::Arc;

Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/tests/two_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.

use libp2p_core::{muxing, upgrade, Transport};
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use futures::{channel::oneshot, prelude::*};
use std::sync::Arc;

Expand Down
2 changes: 1 addition & 1 deletion protocols/deflate/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use futures::{future, prelude::*};
use libp2p_core::{transport::Transport, upgrade};
use libp2p_deflate::DeflateConfig;
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use quickcheck::{QuickCheck, RngCore, TestResult};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ mod tests {
Transport,
upgrade
};
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use libp2p_secio::SecioConfig;
use libp2p_swarm::{Swarm, SwarmEvent};
use libp2p_mplex::MplexConfig;
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ fn parse_proto_msg(msg: impl AsRef<[u8]>) -> Result<(IdentifyInfo, Multiaddr), i
#[cfg(test)]
mod tests {
use crate::protocol::{IdentifyInfo, RemoteInfo, IdentifyProtocolConfig};
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use futures::{prelude::*, channel::oneshot};
use libp2p_core::{
identity,
Expand Down
2 changes: 1 addition & 1 deletion protocols/noise/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//!
//! ```
//! use libp2p_core::{identity, Transport, upgrade};
//! use libp2p_tcp::TcpConfig;
//! use libp2p_tcp::async_std::TcpConfig;
//! use libp2p_noise::{Keypair, X25519, NoiseConfig};
//!
//! # fn main() {
Expand Down
2 changes: 1 addition & 1 deletion protocols/noise/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use libp2p_core::identity;
use libp2p_core::upgrade::{self, Negotiated, apply_inbound, apply_outbound};
use libp2p_core::transport::{Transport, ListenerEvent};
use libp2p_noise::{Keypair, X25519, NoiseConfig, RemoteIdentity, NoiseError, NoiseOutput};
use libp2p_tcp::{TcpConfig, TcpTransStream};
use libp2p_tcp::{async_std::TcpConfig, async_std::TcpTransStream};
use log::info;
use quickcheck::QuickCheck;
use std::{convert::TryInto, io};
Expand Down
2 changes: 1 addition & 1 deletion protocols/ping/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use libp2p_core::{
use libp2p_ping::*;
use libp2p_secio::SecioConfig;
use libp2p_swarm::Swarm;
use libp2p_tcp::TcpConfig;
use libp2p_tcp::async_std::TcpConfig;
use futures::{prelude::*, channel::mpsc};
use std::{io, time::Duration};

Expand Down
2 changes: 1 addition & 1 deletion protocols/secio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! use libp2p_core::{PeerId, Multiaddr, identity, upgrade};
//! use libp2p_core::transport::Transport;
//! use libp2p_mplex::MplexConfig;
//! use libp2p_tcp::TcpConfig;
//! use libp2p_tcp::async_std::TcpConfig;
//!
//! // Create a local peer identity.
//! let local_keys = identity::Keypair::generate_ed25519();
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
//! Example (Dialing a TCP/IP multi-address):
//!
//! ```rust
//! use libp2p::{Multiaddr, Transport, tcp::TcpConfig};
//! use libp2p::{Multiaddr, Transport, tcp::async_std::TcpConfig};
//! let tcp = TcpConfig::new();
//! let addr: Multiaddr = "/ip4/98.97.96.95/tcp/20500".parse().expect("invalid multiaddr");
//! let _conn = tcp.dial(addr);
Expand Down Expand Up @@ -86,7 +86,7 @@
//!
//! ```rust
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "libp2p-secio"))] {
//! use libp2p::{Transport, tcp::TcpConfig, secio::SecioConfig, identity::Keypair, yamux};
//! use libp2p::{Transport, tcp::async_std::TcpConfig, secio::SecioConfig, identity::Keypair, yamux};
//! let tcp = TcpConfig::new();
//! let secio = SecioConfig::new(Keypair::generate_ed25519());
//! let yamux = yamux::Config::default();
Expand Down Expand Up @@ -141,7 +141,7 @@
//! [`Keypair`]: identity::Keypair
//! [`PublicKey`]: identity::PublicKey
//! [`Future`]: futures::Future
//! [`TcpConfig`]: tcp::TcpConfig
//! [`TcpConfig`]: tcp::async_std::TcpConfig
//! [`NetworkBehaviour`]: swarm::NetworkBehaviour
//! [`StreamMuxer`]: core::muxing::StreamMuxer
//! [`Yamux`]: yamux::Yamux
Expand Down Expand Up @@ -282,9 +282,9 @@ struct CommonTransport {
}

#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "libp2p-websocket"))]
type InnerImplementation = core::transport::OrTransport<dns::DnsConfig<tcp::TcpConfig>, websocket::WsConfig<dns::DnsConfig<tcp::TcpConfig>>>;
type InnerImplementation = core::transport::OrTransport<dns::DnsConfig<tcp::async_std::TcpConfig>, websocket::WsConfig<dns::DnsConfig<tcp::async_std::TcpConfig>>>;
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), not(feature = "libp2p-websocket")))]
type InnerImplementation = dns::DnsConfig<tcp::TcpConfig>;
type InnerImplementation = dns::DnsConfig<tcp::async_std::TcpConfig>;
#[cfg(any(target_os = "emscripten", target_os = "unknown"))]
type InnerImplementation = core::transport::dummy::DummyTransport;

Expand All @@ -297,7 +297,7 @@ impl CommonTransport {
/// Initializes the `CommonTransport`.
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
pub fn new() -> io::Result<CommonTransport> {
let tcp = tcp::TcpConfig::new().nodelay(true);
let tcp = tcp::async_std::TcpConfig::new().nodelay(true);
let transport = dns::DnsConfig::new(tcp)?;
#[cfg(feature = "libp2p-websocket")]
let transport = {
Expand Down
1 change: 1 addition & 0 deletions transports/tcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ipnet = "2.0.0"
libp2p-core = { version = "0.16.0", path = "../../core" }
log = "0.4.1"
tokio = { version = "0.2", default-features = false, features = ["tcp"], optional = true }
async-trait = "0.1"

[features]
default = ["async-std"]
Loading

0 comments on commit 82ff15d

Please sign in to comment.