conn.go 1.78 KB
Newer Older
1 2 3
package network

import (
4
	"context"
5 6
	"io"

tavit ohanian's avatar
tavit ohanian committed
7 8
	ic "gitlab.dms3.io/p2p/go-p2p-core/crypto"
	"gitlab.dms3.io/p2p/go-p2p-core/peer"
9

tavit ohanian's avatar
tavit ohanian committed
10
	ma "gitlab.dms3.io/mf/go-multiaddr"
11 12 13 14 15 16 17 18 19 20 21
)

// Conn is a connection to a remote peer. It multiplexes streams.
// Usually there is no need to use a Conn directly, but it may
// be useful to get information about the peer on the other side:
//  stream.Conn().RemotePeer()
type Conn interface {
	io.Closer

	ConnSecurity
	ConnMultiaddrs
22
	ConnStat
23

24 25 26 27
	// ID returns an identifier that uniquely identifies this Conn within this
	// host, during this run. Connection IDs may repeat across restarts.
	ID() string

28
	// NewStream constructs a new Stream over this conn.
29
	NewStream(context.Context) (Stream, error)
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

	// GetStreams returns all open streams over this conn.
	GetStreams() []Stream
}

// ConnSecurity is the interface that one can mix into a connection interface to
// give it the security methods.
type ConnSecurity interface {
	// LocalPeer returns our peer ID
	LocalPeer() peer.ID

	// LocalPrivateKey returns our private key
	LocalPrivateKey() ic.PrivKey

	// RemotePeer returns the peer ID of the remote peer.
	RemotePeer() peer.ID

	// RemotePublicKey returns the public key of the remote peer.
	RemotePublicKey() ic.PubKey
}

// ConnMultiaddrs is an interface mixin for connection types that provide multiaddr
// addresses for the endpoints.
type ConnMultiaddrs interface {
	// LocalMultiaddr returns the local Multiaddr associated
	// with this connection
	LocalMultiaddr() ma.Multiaddr

	// RemoteMultiaddr returns the remote Multiaddr associated
	// with this connection
	RemoteMultiaddr() ma.Multiaddr
}
62

vyzo's avatar
vyzo committed
63
// ConnStat is an interface mixin for connection types that provide connection statistics.
64 65 66 67
type ConnStat interface {
	// Stat stores metadata pertaining to this conn.
	Stat() Stat
}