status-go/vendor/github.com/libp2p/go-libp2p-transport-upgrader
Adam Babik c9e99c432d migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00
..
.travis.yml migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00
LICENSE Add rendezvous implementation for discovery interface 2018-07-25 15:10:57 +03:00
README.md migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00
codecov.yml migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00
conn.go migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00
go.mod migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00
go.sum migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00
listener.go migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00
threshold.go Add rendezvous implementation for discovery interface 2018-07-25 15:10:57 +03:00
upgrader.go migrate to go 1.12 and go modules 2019-06-12 13:12:00 +02:00

README.md

go-libp2p-transport-upgrader

GoDoc Build Status Discourse posts

Stream connection to libp2p connection upgrader

This package provides the necessary logic to upgrade multiaddr-net connections listeners into full libp2p-transport connections and listeners.

To use, construct a new Upgrader with:

Note: This package largely replaces the functionality of go-libp2p-conn but with half the code.

Install

go-libp2p-transport-upgrader is a standard Go module which can be installed with:

go get github.com/libp2p/go-libp2p-transport-upgrader

This repo is gomod-compatible, and users of go 1.11 and later with modules enabled will automatically pull the latest tagged release by referencing this package. Upgrades to future releases can be managed using go get, or by editing your go.mod file as described by the gomod documentation.

Usage

Example

Below is a simplified TCP transport implementation using the transport upgrader. In practice, you'll want to use go-tcp-transport (which has reuseport support).

package tcptransport

import (
	"context"

	tptu "github.com/libp2p/go-libp2p-transport-upgrader"

	ma "github.com/multiformats/go-multiaddr"
	mafmt "github.com/whyrusleeping/mafmt"
	manet "github.com/multiformats/go-multiaddr-net"
	tpt "github.com/libp2p/go-libp2p-transport"
)

// TcpTransport is a simple TCP transport.
type TcpTransport struct {
	// Connection upgrader for upgrading insecure stream connections to
	// secure multiplex connections.
	Upgrader *tptu.Upgrader
}

var _ tpt.Transport = &TcpTransport{}

// NewTCPTransport creates a new TCP transport instance.
func NewTCPTransport(upgrader *tptu.Upgrader) *TcpTransport {
	return &TcpTransport{Upgrader: upgrader}
}

// CanDial returns true if this transport believes it can dial the given
// multiaddr.
func (t *TcpTransport) CanDial(addr ma.Multiaddr) bool {
	return mafmt.TCP.Matches(addr)
}

// Dial dials the peer at the remote address.
func (t *TcpTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tpt.Conn, error) {
    var dialer manet.Dialer
    conn, err := dialer.DialContext(ctx, raddr)
	if err != nil {
		return nil, err
	}
	return t.Upgrader.UpgradeOutbound(ctx, t, conn, p)
}

// Listen listens on the given multiaddr.
func (t *TcpTransport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
	list, err := manet.Listen(laddr)
	if err != nil {
		return nil, err
	}
	return t.Upgrader.UpgradeListener(t, list), nil
}

// Protocols returns the list of terminal protocols this transport can dial.
func (t *TcpTransport) Protocols() []int {
	return []int{ma.P_TCP}
}

// Proxy always returns false for the TCP transport.
func (t *TcpTransport) Proxy() bool {
	return false
}

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the IPFS Code of Conduct.

Want to hack on IPFS?

License

MIT


The last gx published version of this module was: 0.1.28: QmeqC5shQjEBRG9B8roZqQCJ9xb7Pq6AbWxJFMyLgqBBWh