Skip to content

Commit 5e6a6a9

Browse files
authored
Merge pull request #57 from tomaka/tcp-transport-readme
Add a README and documentation for libp2p-tcp-transport
2 parents e9d88cf + 0485883 commit 5e6a6a9

File tree

4 files changed

+76
-16
lines changed

4 files changed

+76
-16
lines changed

example/examples/echo-dialer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ use std::io::Error as IoError;
3636
use std::iter;
3737
use std::sync::Arc;
3838
use swarm::{Transport, ConnectionUpgrade};
39-
use tcp::Tcp;
39+
use tcp::TcpConfig;
4040
use tokio_core::reactor::Core;
4141
use tokio_io::{AsyncRead, AsyncWrite};
4242
use tokio_io::codec::length_delimited;
4343
use untrusted::Input;
4444

4545
fn main() {
4646
let mut core = Core::new().unwrap();
47-
let tcp = Tcp::new(core.handle()).unwrap();
47+
let tcp = TcpConfig::new(core.handle());
4848

4949
let with_secio = tcp
5050
.with_upgrade(swarm::PlainText)

example/examples/echo-server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ use std::io::Error as IoError;
3636
use std::iter;
3737
use std::sync::Arc;
3838
use swarm::{Transport, ConnectionUpgrade};
39-
use tcp::Tcp;
39+
use tcp::TcpConfig;
4040
use tokio_core::reactor::Core;
4141
use tokio_io::{AsyncRead, AsyncWrite};
4242
use tokio_io::codec::length_delimited;
4343
use untrusted::Input;
4444

4545
fn main() {
4646
let mut core = Core::new().unwrap();
47-
let tcp = Tcp::new(core.handle()).unwrap();
47+
let tcp = TcpConfig::new(core.handle());
4848

4949
let with_secio = tcp
5050
.with_upgrade(swarm::PlainText)

libp2p-tcp-transport/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# TCP transport
2+
3+
Implementation of the libp2p `Transport` trait for TCP/IP.
4+
5+
Uses [the *tokio* library](https://tokio.rs).
6+
7+
## Usage
8+
9+
Create [a tokio `Core`](https://docs.rs/tokio-core/0.1/tokio_core/reactor/struct.Core.html),
10+
then grab a handle by calling the `handle()` method on it, then create a `TcpConfig` and pass
11+
the handle.
12+
13+
Example:
14+
15+
```rust
16+
extern crate libp2p_tcp_transport;
17+
extern crate tokio_core;
18+
19+
use libp2p_tcp_transport::TcpConfig;
20+
use tokio_core::reactor::Core;
21+
22+
let mut core = Core::new().unwrap();
23+
let tcp = TcpConfig::new(core.handle());
24+
```
25+
26+
The `TcpConfig` structs implements the `Transport` trait of the `swarm` library. See the
27+
documentation of `swarm` and of libp2p in general to learn how to use the `Transport` trait.

libp2p-tcp-transport/src/lib.rs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,36 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21+
// TODO: use this once stable ; for now we just copy-paste the content of the README.md
22+
//#![doc(include = "../README.md")]
23+
2124
//! Implementation of the libp2p `Transport` trait for TCP/IP.
25+
//!
26+
//! Uses [the *tokio* library](https://tokio.rs).
27+
//!
28+
//! # Usage
29+
//!
30+
//! Create [a tokio `Core`](https://docs.rs/tokio-core/0.1/tokio_core/reactor/struct.Core.html),
31+
//! then grab a handle by calling the `handle()` method on it, then create a `TcpConfig` and pass
32+
//! the handle.
33+
//!
34+
//! Example:
35+
//!
36+
//! ```
37+
//! extern crate libp2p_tcp_transport;
38+
//! extern crate tokio_core;
39+
//!
40+
//! use libp2p_tcp_transport::TcpConfig;
41+
//! use tokio_core::reactor::Core;
42+
//!
43+
//! # fn main() {
44+
//! let mut core = Core::new().unwrap();
45+
//! let tcp = TcpConfig::new(core.handle());
46+
//! # }
47+
//! ```
48+
//!
49+
//! The `TcpConfig` structs implements the `Transport` trait of the `swarm` library. See the
50+
//! documentation of `swarm` and of libp2p in general to learn how to use the `Transport` trait.
2251
2352
extern crate libp2p_swarm as swarm;
2453
extern crate tokio_core;
@@ -35,22 +64,26 @@ use futures::stream::Stream;
3564
use multiaddr::{Multiaddr, Protocol, ToMultiaddr};
3665
use swarm::Transport;
3766

38-
/// Represents a TCP/IP transport capability for libp2p.
67+
/// Represents the configuration for a TCP/IP transport capability for libp2p.
3968
///
40-
/// Each `Tcp` struct is tied to a tokio reactor. The TCP sockets created by libp2p will need to
41-
/// be progressed by running the futures and streams obtained by libp2p through the tokio reactor.
69+
/// Each connection created by this config is tied to a tokio reactor. The TCP sockets created by
70+
/// libp2p will need to be progressed by running the futures and streams obtained by libp2p
71+
/// through the tokio reactor.
4272
#[derive(Debug, Clone)]
43-
pub struct Tcp {
73+
pub struct TcpConfig {
4474
event_loop: Handle,
4575
}
4676

47-
impl Tcp {
48-
pub fn new(handle: Handle) -> Result<Tcp, IoError> {
49-
Ok(Tcp { event_loop: handle })
77+
impl TcpConfig {
78+
/// Creates a new configuration object for TCP/IP. The `Handle` is a tokio reactor the
79+
/// connections will be created with.
80+
#[inline]
81+
pub fn new(handle: Handle) -> TcpConfig {
82+
TcpConfig { event_loop: handle }
5083
}
5184
}
5285

53-
impl Transport for Tcp {
86+
impl Transport for TcpConfig {
5487
/// The raw connection.
5588
type RawConn = TcpStream;
5689

@@ -135,7 +168,7 @@ fn multiaddr_to_socketaddr(addr: &Multiaddr) -> Result<SocketAddr, ()> {
135168

136169
#[cfg(test)]
137170
mod tests {
138-
use super::{Tcp, multiaddr_to_socketaddr};
171+
use super::{TcpConfig, multiaddr_to_socketaddr};
139172
use std;
140173
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
141174
use tokio_core::reactor::Core;
@@ -199,7 +232,7 @@ mod tests {
199232
std::thread::spawn(move || {
200233
let mut core = Core::new().unwrap();
201234
let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap();
202-
let tcp = Tcp::new(core.handle()).unwrap();
235+
let tcp = TcpConfig::new(core.handle());
203236
let handle = core.handle();
204237
let listener = tcp.listen_on(addr).unwrap().0.for_each(|(sock, _)| {
205238
// Define what to do with the socket that just connected to us
@@ -219,7 +252,7 @@ mod tests {
219252
std::thread::sleep(std::time::Duration::from_millis(100));
220253
let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap();
221254
let mut core = Core::new().unwrap();
222-
let tcp = Tcp::new(core.handle()).unwrap();
255+
let tcp = TcpConfig::new(core.handle());
223256
// Obtain a future socket through dialing
224257
let socket = tcp.dial(addr.clone()).unwrap();
225258
// Define what to do with the socket once it's obtained
@@ -238,7 +271,7 @@ mod tests {
238271
#[test]
239272
fn replace_port_0_in_returned_multiaddr() {
240273
let core = Core::new().unwrap();
241-
let tcp = Tcp::new(core.handle()).unwrap();
274+
let tcp = TcpConfig::new(core.handle());
242275

243276
let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/0").unwrap();
244277
assert!(addr.to_string().contains("tcp/0"));

0 commit comments

Comments
 (0)