status-go/vendor/github.com/libp2p/go-libp2p/config/transport.go

72 lines
2.5 KiB
Go
Raw Normal View History

package config
import (
2022-11-04 13:57:20 +00:00
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/pnet"
"github.com/libp2p/go-libp2p/core/transport"
madns "github.com/multiformats/go-multiaddr-dns"
)
// TptC is the type for libp2p transport constructors. You probably won't ever
// implement this function interface directly. Instead, pass your transport
// constructor to TransportConstructor.
2022-11-04 13:57:20 +00:00
type TptC func(host.Host, transport.Upgrader, pnet.PSK, connmgr.ConnectionGater, network.ResourceManager, *madns.Resolver) (transport.Transport, error)
var transportArgTypes = argTypes
// TransportConstructor uses reflection to turn a function that constructs a
// transport into a TptC.
//
// You can pass either a constructed transport (something that implements
// `transport.Transport`) or a function that takes any of:
//
// * The local peer ID.
// * A transport connection upgrader.
// * A private key.
// * A public key.
// * A Host.
// * A Network.
// * A Peerstore.
// * An address filter.
// * A security transport.
// * A stream multiplexer transport.
// * A private network protection key.
// * A connection gater.
//
// And returns a type implementing transport.Transport and, optionally, an error
// (as the second argument).
2022-04-01 16:16:46 +00:00
func TransportConstructor(tpt interface{}, opts ...interface{}) (TptC, error) {
// Already constructed?
if t, ok := tpt.(transport.Transport); ok {
2022-11-04 13:57:20 +00:00
return func(_ host.Host, _ transport.Upgrader, _ pnet.PSK, _ connmgr.ConnectionGater, _ network.ResourceManager, _ *madns.Resolver) (transport.Transport, error) {
return t, nil
}, nil
}
2022-04-01 16:16:46 +00:00
ctor, err := makeConstructor(tpt, transportType, transportArgTypes, opts...)
if err != nil {
return nil, err
}
2022-11-04 13:57:20 +00:00
return func(h host.Host, u transport.Upgrader, psk pnet.PSK, cg connmgr.ConnectionGater, rcmgr network.ResourceManager, resolver *madns.Resolver) (transport.Transport, error) {
t, err := ctor(h, u, psk, cg, rcmgr, resolver)
if err != nil {
return nil, err
}
return t.(transport.Transport), nil
}, nil
}
2022-11-04 13:57:20 +00:00
func makeTransports(h host.Host, u transport.Upgrader, cg connmgr.ConnectionGater, psk pnet.PSK, rcmgr network.ResourceManager, resolver *madns.Resolver, tpts []TptC) ([]transport.Transport, error) {
transports := make([]transport.Transport, len(tpts))
for i, tC := range tpts {
2022-11-04 13:57:20 +00:00
t, err := tC(h, u, psk, cg, rcmgr, resolver)
if err != nil {
return nil, err
}
transports[i] = t
}
return transports, nil
}