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
2352extern crate libp2p_swarm as swarm;
2453extern crate tokio_core;
@@ -35,22 +64,26 @@ use futures::stream::Stream;
3564use multiaddr:: { Multiaddr , Protocol , ToMultiaddr } ;
3665use 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) ]
137170mod 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