2019-07-02 12:40:25 +00:00
|
|
|
package lp2p
|
2019-07-01 09:09:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
smux "github.com/libp2p/go-libp2p-core/mux"
|
|
|
|
mplex "github.com/libp2p/go-libp2p-mplex"
|
|
|
|
yamux "github.com/libp2p/go-libp2p-yamux"
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeSmuxTransportOption(mplexExp bool) libp2p.Option {
|
|
|
|
const yamuxID = "/yamux/1.0.0"
|
|
|
|
const mplexID = "/mplex/6.7.0"
|
|
|
|
|
|
|
|
ymxtpt := *yamux.DefaultTransport
|
|
|
|
ymxtpt.AcceptBacklog = 512
|
|
|
|
|
|
|
|
if os.Getenv("YAMUX_DEBUG") != "" {
|
|
|
|
ymxtpt.LogOutput = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
muxers := map[string]smux.Multiplexer{yamuxID: &ymxtpt}
|
|
|
|
if mplexExp {
|
|
|
|
muxers[mplexID] = mplex.DefaultTransport
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow muxer preference order overriding
|
|
|
|
order := []string{yamuxID, mplexID}
|
|
|
|
if prefs := os.Getenv("LIBP2P_MUX_PREFS"); prefs != "" {
|
|
|
|
order = strings.Fields(prefs)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := make([]libp2p.Option, 0, len(order))
|
|
|
|
for _, id := range order {
|
|
|
|
tpt, ok := muxers[id]
|
|
|
|
if !ok {
|
2019-07-03 12:30:21 +00:00
|
|
|
log.Warnf("unknown or duplicate muxer in LIBP2P_MUX_PREFS: %s", id)
|
2019-07-01 09:09:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(muxers, id)
|
|
|
|
opts = append(opts, libp2p.Muxer(id, tpt))
|
|
|
|
}
|
|
|
|
|
|
|
|
return libp2p.ChainOptions(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SmuxTransport(mplex bool) func() (opts Libp2pOpts, err error) {
|
|
|
|
return func() (opts Libp2pOpts, err error) {
|
|
|
|
opts.Opts = append(opts.Opts, makeSmuxTransportOption(mplex))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|