-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmuxer.go
63 lines (55 loc) · 1.57 KB
/
muxer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package config
import (
"fmt"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
msmux "github.com/libp2p/go-libp2p/p2p/muxer/muxer-multistream"
)
// MuxC is a stream multiplex transport constructor.
type MuxC func(h host.Host) (network.Multiplexer, error)
// MsMuxC is a tuple containing a multiplex transport constructor and a protocol
// ID.
type MsMuxC struct {
MuxC
ID string
}
var muxArgTypes = newArgTypeSet(hostType, networkType, peerIDType, pstoreType)
// MuxerConstructor creates a multiplex constructor from the passed parameter
// using reflection.
func MuxerConstructor(m interface{}) (MuxC, error) {
// Already constructed?
if t, ok := m.(network.Multiplexer); ok {
return func(_ host.Host) (network.Multiplexer, error) {
return t, nil
}, nil
}
ctor, err := makeConstructor(m, muxType, muxArgTypes)
if err != nil {
return nil, err
}
return func(h host.Host) (network.Multiplexer, error) {
t, err := ctor(h, nil, nil, nil, nil)
if err != nil {
return nil, err
}
return t.(network.Multiplexer), nil
}, nil
}
func makeMuxer(h host.Host, tpts []MsMuxC) (network.Multiplexer, error) {
muxMuxer := msmux.NewBlankTransport()
transportSet := make(map[string]struct{}, len(tpts))
for _, tptC := range tpts {
if _, ok := transportSet[tptC.ID]; ok {
return nil, fmt.Errorf("duplicate muxer transport: %s", tptC.ID)
}
transportSet[tptC.ID] = struct{}{}
}
for _, tptC := range tpts {
tpt, err := tptC.MuxC(h)
if err != nil {
return nil, err
}
muxMuxer.AddTransport(tptC.ID, tpt)
}
return muxMuxer, nil
}