2018-07-04 10:51:47 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-06-16 20:19:45 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/connmgr"
|
2019-06-09 07:24:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/transport"
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2021-06-16 20:19:45 +00:00
|
|
|
type TptC func(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater) (transport.Transport, error)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
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.
|
2021-06-16 20:19:45 +00:00
|
|
|
// * A private network protection key.
|
|
|
|
// * A connection gater.
|
2018-07-04 10:51:47 +00:00
|
|
|
//
|
|
|
|
// And returns a type implementing transport.Transport and, optionally, an error
|
|
|
|
// (as the second argument).
|
|
|
|
func TransportConstructor(tpt interface{}) (TptC, error) {
|
|
|
|
// Already constructed?
|
|
|
|
if t, ok := tpt.(transport.Transport); ok {
|
2021-06-16 20:19:45 +00:00
|
|
|
return func(_ host.Host, _ *tptu.Upgrader, _ connmgr.ConnectionGater) (transport.Transport, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
return t, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
ctor, err := makeConstructor(tpt, transportType, transportArgTypes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
return func(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater) (transport.Transport, error) {
|
|
|
|
t, err := ctor(h, u, cg)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t.(transport.Transport), nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
func makeTransports(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater, tpts []TptC) ([]transport.Transport, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
transports := make([]transport.Transport, len(tpts))
|
|
|
|
for i, tC := range tpts {
|
2021-06-16 20:19:45 +00:00
|
|
|
t, err := tC(h, u, cg)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
transports[i] = t
|
|
|
|
}
|
|
|
|
return transports, nil
|
|
|
|
}
|