1
1
use libp2p:: {
2
- dns, identity, mplex, secio, tcp, yamux, Multiaddr , PeerId , Transport , TransportError ,
2
+ core:: {
3
+ muxing:: { StreamMuxer , StreamMuxerBox } ,
4
+ upgrade:: { SelectUpgrade , Version } ,
5
+ } ,
6
+ dns:: DnsConfig ,
7
+ identity,
8
+ mplex:: MplexConfig ,
9
+ secio:: SecioConfig ,
10
+ tcp:: TcpConfig ,
11
+ yamux, PeerId , Transport ,
3
12
} ;
4
- use libp2p_core:: { muxing, upgrade} ;
5
13
use std:: { error, io, time:: Duration } ;
6
14
7
- /// Builds a `Transport` that supports the most commonly-used protocols that
8
- /// libp2p supports.
15
+ /// Builds a libp2p transport with the following features:
16
+ /// - TcpConnection
17
+ /// - DNS name resolution
18
+ /// - authentication via secio
19
+ /// - multiplexing via yamux or mplex
9
20
pub fn build_comit_transport (
10
21
keypair : identity:: Keypair ,
11
22
) -> impl Transport <
12
23
Output = (
13
24
PeerId ,
14
- impl muxing :: StreamMuxer <
25
+ impl StreamMuxer <
15
26
OutboundSubstream = impl Send ,
16
27
Substream = impl Send ,
17
28
Error = impl Into < io:: Error > ,
@@ -23,71 +34,16 @@ pub fn build_comit_transport(
23
34
Dial = impl Send ,
24
35
ListenerUpgrade = impl Send ,
25
36
> + Clone {
26
- build_tcp_ws_secio_mplex_yamux ( keypair )
27
- }
37
+ let transport = TcpConfig :: new ( ) . nodelay ( true ) ;
38
+ let transport = DnsConfig :: new ( transport ) ;
28
39
29
- /// Builds an implementation of `Transport` that is suitable for usage with the
30
- /// `Swarm`.
31
- ///
32
- /// The implementation supports TCP/IP, secio as the encryption layer, and mplex
33
- /// or yamux as the multiplexing layer.
34
- pub fn build_tcp_ws_secio_mplex_yamux (
35
- keypair : identity:: Keypair ,
36
- ) -> impl Transport <
37
- Output = (
38
- PeerId ,
39
- impl muxing:: StreamMuxer <
40
- OutboundSubstream = impl Send ,
41
- Substream = impl Send ,
42
- Error = impl Into < io:: Error > ,
43
- > + Send
44
- + Sync ,
45
- ) ,
46
- Error = impl error:: Error + Send ,
47
- Listener = impl Send ,
48
- Dial = impl Send ,
49
- ListenerUpgrade = impl Send ,
50
- > + Clone {
51
- ComitTransport :: new ( )
52
- . upgrade ( upgrade:: Version :: V1 )
53
- . authenticate ( secio:: SecioConfig :: new ( keypair) )
54
- . multiplex ( upgrade:: SelectUpgrade :: new (
40
+ transport
41
+ . upgrade ( Version :: V1 )
42
+ . authenticate ( SecioConfig :: new ( keypair) )
43
+ . multiplex ( SelectUpgrade :: new (
55
44
yamux:: Config :: default ( ) ,
56
- mplex :: MplexConfig :: new ( ) ,
45
+ MplexConfig :: new ( ) ,
57
46
) )
58
- . map ( |( peer, muxer) , _| ( peer, muxing :: StreamMuxerBox :: new ( muxer) ) )
47
+ . map ( |( peer, muxer) , _| ( peer, StreamMuxerBox :: new ( muxer) ) )
59
48
. timeout ( Duration :: from_secs ( 20 ) )
60
49
}
61
-
62
- #[ derive( Debug , Clone ) ]
63
- struct ComitTransport {
64
- inner : InnerImplementation ,
65
- }
66
-
67
- type InnerImplementation = dns:: DnsConfig < tcp:: TcpConfig > ;
68
-
69
- impl ComitTransport {
70
- /// Initializes the `ComitTransport`.
71
- pub fn new ( ) -> ComitTransport {
72
- let tcp = tcp:: TcpConfig :: new ( ) . nodelay ( true ) ;
73
- let transport = dns:: DnsConfig :: new ( tcp) ;
74
-
75
- ComitTransport { inner : transport }
76
- }
77
- }
78
-
79
- impl Transport for ComitTransport {
80
- type Output = <InnerImplementation as Transport >:: Output ;
81
- type Error = <InnerImplementation as Transport >:: Error ;
82
- type Listener = <InnerImplementation as Transport >:: Listener ;
83
- type ListenerUpgrade = <InnerImplementation as Transport >:: ListenerUpgrade ;
84
- type Dial = <InnerImplementation as Transport >:: Dial ;
85
-
86
- fn listen_on ( self , addr : Multiaddr ) -> Result < Self :: Listener , TransportError < Self :: Error > > {
87
- self . inner . listen_on ( addr)
88
- }
89
-
90
- fn dial ( self , addr : Multiaddr ) -> Result < Self :: Dial , TransportError < Self :: Error > > {
91
- self . inner . dial ( addr)
92
- }
93
- }
0 commit comments