Skip to content

Commit

Permalink
move transport into tokio dir and feature gate it
Browse files Browse the repository at this point in the history
  • Loading branch information
melekes committed Oct 17, 2022
1 parent 05a9b00 commit f7c8ab5
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 43 deletions.
10 changes: 7 additions & 3 deletions transports/webrtc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ serde = { version = "1.0", features = ["derive"] }
stun = "0.4"
thiserror = "1"
tinytemplate = "1.2"
tokio = { version = "1.18", features = ["net"]}
tokio-util = { version = "0.7", features = ["compat"] }
webrtc = "0.5.0"

tokio = { version = "1.18", features = ["net"], optional = true}
tokio-util = { version = "0.7", features = ["compat"], optional = true }
webrtc = { version = "0.5.0", optional = true }

[features]
tokio = ["dep:tokio", "dep:tokio-util", "dep:webrtc"]

[build-dependencies]
prost-build = "0.11"
Expand Down
14 changes: 2 additions & 12 deletions transports/webrtc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,11 @@
//! is to make the hash a part of the remote's multiaddr. On the server side, we turn
//! certificate verification off.

mod connection;
mod error;
mod fingerprint;
mod req_res_chan;
mod sdp;
mod substream;
mod transport;
mod udp_mux;
mod upgrade;
mod message_proto {
#![allow(clippy::derive_partial_eq_without_eq)]

include!(concat!(env!("OUT_DIR"), "/webrtc.pb.rs"));
}

pub use connection::Connection;
pub use error::Error;
pub use transport::Transport;
#[cfg(feature = "tokio")]
pub mod tokio;
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::{
task::{Context, Poll},
};

use crate::{error::Error, substream, substream::Substream};
use crate::tokio::{error::Error, substream, substream::Substream};

/// Maximum number of unprocessed data channels.
/// See [`Connection::poll_inbound`].
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

use multibase::Base;
use multihash::{Code, Hasher, Multihash, MultihashDigest};
use std::fmt;
use webrtc::dtls_transport::dtls_fingerprint::RTCDtlsFingerprint;

use std::fmt;

const SHA256: &str = "sha-256";

/// A certificate fingerprint that is assumed to be created using the SHA256 hash algorithm.
Expand Down
33 changes: 33 additions & 0 deletions transports/webrtc/src/tokio/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 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.

mod connection;
mod error;
mod fingerprint;
mod req_res_chan;
mod sdp;
mod substream;
mod transport;
mod udp_mux;
mod upgrade;

pub use connection::Connection;
pub use error::Error;
pub use transport::Transport;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use futures::{
SinkExt,
};
use futures_lite::StreamExt;

use std::{
io,
task::{Context, Poll},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
// DEALINGS IN THE SOFTWARE.

use serde::Serialize;
use std::net::SocketAddr;
use tinytemplate::TinyTemplate;

use std::net::IpAddr;
use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;

use crate::fingerprint::Fingerprint;
use std::net::{IpAddr, SocketAddr};

use crate::tokio::fingerprint::Fingerprint;

/// Creates the SDP answer used by the client.
pub fn answer(addr: SocketAddr, server_fingerprint: &Fingerprint) -> RTCSessionDescription {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ use std::{
};

use crate::message_proto::{message::Flag, Message};
use crate::substream::drop_listener::GracefullyClosed;
use crate::substream::framed_dc::FramedDC;
use crate::substream::state::{Closing, State};
use crate::tokio::{
substream::drop_listener::GracefullyClosed,
substream::framed_dc::FramedDC,
substream::state::{Closing, State},
};

mod drop_listener;
mod framed_dc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::message_proto::{message::Flag, Message};
use crate::substream::framed_dc::FramedDC;
use futures::channel::oneshot;
use futures::channel::oneshot::Canceled;
use futures::{FutureExt, SinkExt};

use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

use crate::message_proto::{message::Flag, Message};
use crate::tokio::substream::framed_dc::FramedDC;

#[must_use]
pub struct DropListener {
state: State,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::message_proto::Message;
use crate::substream::MAX_MSG_LEN;
use asynchronous_codec::Framed;
use std::sync::Arc;
use tokio_util::compat::Compat;
use tokio_util::compat::TokioAsyncReadCompatExt;
use webrtc::data::data_channel::{DataChannel, PollDataChannel};

use std::sync::Arc;

use crate::message_proto::Message;
use crate::tokio::substream::MAX_MSG_LEN;

pub type FramedDC = Framed<Compat<PollDataChannel>, prost_codec::Codec<Message>>;

pub fn new(data_channel: Arc<DataChannel>) -> FramedDC {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::message_proto::message::Flag;
use bytes::Bytes;

use std::io;

use crate::message_proto::message::Flag;

#[derive(Debug, Copy, Clone)]
pub enum State {
Open,
Expand Down Expand Up @@ -167,7 +169,7 @@ impl State {

/// Acts as a "barrier" for [`futures::AsyncRead::poll_read`].
pub(crate) fn read_barrier(&self) -> io::Result<()> {
use crate::substream::State::{Open, ReadClosed, WriteClosed};
use crate::tokio::substream::State::{Open, ReadClosed, WriteClosed};
use State::*;

let kind = match self {
Expand All @@ -190,7 +192,7 @@ impl State {

/// Acts as a "barrier" for [`futures::AsyncWrite::poll_write`].
pub(crate) fn write_barrier(&self) -> io::Result<()> {
use crate::substream::State::{Open, ReadClosed, WriteClosed};
use crate::tokio::substream::State::{Open, ReadClosed, WriteClosed};
use State::*;

let kind = match self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ use libp2p_core::{
transport::{ListenerId, TransportError, TransportEvent},
PeerId,
};
use rand::distributions::DistString;
use webrtc::peer_connection::certificate::RTCCertificate;
use webrtc::peer_connection::configuration::RTCConfiguration;

use rand::distributions::DistString;
use std::net::IpAddr;
use std::{
net::SocketAddr,
pin::Pin,
task::{Context, Poll},
};

use crate::{
use crate::tokio::{
connection::Connection,
error::Error,
fingerprint::Fingerprint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use std::{
task::{Context, Poll},
};

use crate::req_res_chan;
use crate::tokio::req_res_chan;

const RECEIVE_MTU: usize = 8192;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ use webrtc::peer_connection::RTCPeerConnection;

use std::{net::SocketAddr, sync::Arc, time::Duration};

use crate::error::Error;
use crate::fingerprint::Fingerprint;
use crate::substream::Substream;
use crate::{sdp, Connection};
use crate::tokio::{error::Error, fingerprint::Fingerprint, sdp, substream::Substream, Connection};

/// Creates a new outbound WebRTC connection.
pub async fn outbound(
Expand Down Expand Up @@ -219,7 +216,7 @@ async fn create_substream_for_noise_handshake(
let (tx, rx) = oneshot::channel::<Arc<DataChannel>>();

// Wait until the data channel is opened and detach it.
crate::connection::register_data_channel_open_handler(data_channel, tx).await;
crate::tokio::connection::register_data_channel_open_handler(data_channel, tx).await;

let channel = match futures::future::select(rx, Delay::new(Duration::from_secs(10))).await {
Either::Left((Ok(channel), _)) => channel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::fingerprint::Fingerprint;
use crate::Error;
use futures::{AsyncRead, AsyncWrite, AsyncWriteExt};
use libp2p_core::{identity, InboundUpgrade, OutboundUpgrade, PeerId, UpgradeInfo};
use libp2p_noise::{Keypair, NoiseConfig, X25519Spec};

use crate::tokio::fingerprint::Fingerprint;
use crate::tokio::Error;

pub async fn inbound<T>(
id_keys: identity::Keypair,
stream: T,
Expand Down
2 changes: 1 addition & 1 deletion transports/webrtc/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use libp2p::request_response::{
RequestResponseEvent, RequestResponseMessage,
};
use libp2p::swarm::{Swarm, SwarmBuilder, SwarmEvent};
use libp2p::webrtc;
use libp2p::webrtc::tokio as webrtc;
use rand::RngCore;

use std::{io, iter};
Expand Down

0 comments on commit f7c8ab5

Please sign in to comment.