host

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrProtocolIDNotSupportedByPeer will be returned if protocol not supported by remote peer
	// when calling SendMsg method.
	ErrProtocolIDNotSupportedByPeer = errors.New("protocol id not supported by remote peer")
	// ErrPeerNotConnected will be returned if remote peer not connect to us when calling SendMsg method.
	ErrPeerNotConnected = errors.New("peer not connected")
	// ErrConnClosed will be returned if the current connection closed when calling SendMsg method.
	ErrConnClosed = errors.New("connection closed")
	// ErrStreamPoolNotFound will be returned if the stream pool of remote peer not found when calling SendMsg method.
	ErrStreamPoolNotFound = errors.New("peer stream pool not found")
	// ErrSendMsgIncompletely will be returned if the msg sent incompletely when calling SendMsg method.
	ErrSendMsgIncompletely = errors.New("send msg incompletely")
	// ErrPeerAddrNotFoundInPeerStore will be returned if peer address not found in peer store when calling Dial method.
	ErrPeerAddrNotFoundInPeerStore = errors.New("peer address not found in peer store")
	// ErrAllDialFailed will be returned if all dialing return errors when calling Dial method.
	ErrAllDialFailed = errors.New("all dial failed")
	// ErrBlackPeer will be returned if remote peer id in blacklist when handling new peer connected.
	ErrBlackPeer = errors.New("black peer")
)
View Source
var (
	// ErrUnknownNetworkType will be returned if network type is unsupported.
	ErrUnknownNetworkType = errors.New("unknown network type")
)

Functions

This section is empty.

Types

type BasicHost

type BasicHost struct {
	// contains filtered or unexported fields
}

BasicHost is an implementation of host.Host interface. BasicHost can build a network with the same one of different implementations of network.Network. It provides connections management and streams management and protocol management. It uses a mgr.ConnSupervisor to maintain the stat of connections with necessary directed peers.

func (*BasicHost) AddDirectPeer

func (bh *BasicHost) AddDirectPeer(mA ma.Multiaddr)

AddDirectPeer append a directed peer.

func (*BasicHost) Blacklist

func (bh *BasicHost) Blacklist() blacklist.BlackList

Blacklist return the blacklist.BlackList instance of the host.

func (*BasicHost) CheckClosedConnWithErr

func (bh *BasicHost) CheckClosedConnWithErr(conn network.Conn, err error) bool

CheckClosedConnWithErr return whether the connection has closed. If conn.IsClosed() is true, return true. If err contains closed info, return true. Otherwise return false.

func (*BasicHost) ClearDirectPeers

func (bh *BasicHost) ClearDirectPeers()

ClearDirectPeers remove all directed peers.

func (*BasicHost) ConnMgr

func (bh *BasicHost) ConnMgr() mgr.ConnMgr

ConnMgr return the mgr.ConnMgr instance of the host.

func (*BasicHost) Context

func (bh *BasicHost) Context() context.Context

Context of the host instance.

func (*BasicHost) Dial

func (bh *BasicHost) Dial(remoteAddr ma.Multiaddr) (network.Conn, error)

Dial try to establish a connection with peer whose address is the given.

func (*BasicHost) ID

func (bh *BasicHost) ID() peer.ID

ID is local peer id.

func (*BasicHost) IsPeerSupportProtocol

func (bh *BasicHost) IsPeerSupportProtocol(pid peer.ID, protocolID protocol.ID) bool

IsPeerSupportProtocol return true if peer which id is the given pid support the given protocol. Otherwise, return false.

func (*BasicHost) LocalAddresses

func (bh *BasicHost) LocalAddresses() []ma.Multiaddr

LocalAddresses return the list of net addresses for listener listening.

func (*BasicHost) Notify

func (bh *BasicHost) Notify(notifiee host.Notifiee)

Notify registers a Notifiee to host.

func (*BasicHost) PeerProtocols

func (bh *BasicHost) PeerProtocols(protocolIDs []protocol.ID) ([]*host.PeerProtocols, error)

PeerProtocols query peer.ID and the protocol.ID list supported by peer. If protocolIDs is nil ,return the list of all connected to us. Otherwise, return the list of part of all which support the protocols that id contains in the given protocolIDs.

func (*BasicHost) PeerStore

func (bh *BasicHost) PeerStore() store.PeerStore

PeerStore return the store.PeerStore instance of the host.

func (*BasicHost) PrivateKey

func (bh *BasicHost) PrivateKey() crypto.PrivateKey

PrivateKey of the crypto private key.

func (*BasicHost) ProtocolMgr

func (bh *BasicHost) ProtocolMgr() mgr.ProtocolManager

ProtocolMgr return the mgr.ProtocolManager instance of the host.

func (*BasicHost) RegisterMsgPayloadHandler

func (bh *BasicHost) RegisterMsgPayloadHandler(protocolID protocol.ID, handler handler.MsgPayloadHandler) error

RegisterMsgPayloadHandler register a handler.MsgPayloadHandler for handling the msg received with the protocol which id is the given protocolID .

func (*BasicHost) SendMsg

func (bh *BasicHost) SendMsg(protocolID protocol.ID, receiverPID peer.ID, msgPayload []byte) error

SendMsg will send a msg with the protocol which id is the given protocolID to the receiver whose peer.ID is the given receiverPID.

func (*BasicHost) Start

func (bh *BasicHost) Start() error

Start to listen on local addresses and run all managers.

func (*BasicHost) Stop

func (bh *BasicHost) Stop() error

Stop listening and close all the connections.

func (*BasicHost) UnregisterMsgPayloadHandler

func (bh *BasicHost) UnregisterMsgPayloadHandler(protocolID protocol.ID) error

UnregisterMsgPayloadHandler unregister the handler.MsgPayloadHandler for handling the msg received with the protocol which id is the given protocolID .

type HostConfig

type HostConfig struct {
	// PrivateKey of crypto.
	// Local peer.ID will be generated with it.
	PrivateKey crypto.PrivateKey
	// TlsCfg is the configuration for both tls server and client.
	TlsCfg *cmTls.Config
	// LoadPidFunc is a function which type is types.LoadPeerIdFromCMTlsCertFunc, used to load peer.ID from x509 certs.
	LoadPidFunc types.LoadPeerIdFromCMTlsCertFunc
	// SendStreamPoolInitSize is the size of sending streams will be created when a sending stream pool initialing.
	SendStreamPoolInitSize int32
	// SendStreamPoolCap is the max size of the sending stream pool of each conn.
	SendStreamPoolCap int32
	// PeerReceiveStreamMaxCount is the max limit count of receive streams for each peer.
	PeerReceiveStreamMaxCount int32
	// MaxPeerCountAllowed is the max count of peers allowed to connect to us.
	MaxPeerCountAllowed int
	// MaxConnCountEachPeerAllowed is the max count of connections for each peer allowed.
	MaxConnCountEachPeerAllowed int
	// ConnEliminationStrategy is the strategy for connection manager eliminating connections.
	ConnEliminationStrategy int
	// ListenAddresses is the local addresses for listeners listening.
	ListenAddresses []ma.Multiaddr
	// DirectPeers stores the peer.ID and its remote address of peers need keeping connected.
	// ConnSupervisor will check the connection stat of these peers.
	// If anyone disconnected to us, supervisor will try to dial to it automatically.
	DirectPeers map[peer.ID]ma.Multiaddr
	// BlackNetAddr is the list of net addresses that will be appended into blacklist.
	// e.g. "127.0.0.1","127.0.0.1:8080","[::1]","[::1]:8080"
	BlackNetAddr []string
	// BlackPeers is the list of peer.ID that will be appended into blacklist.
	BlackPeers []peer.ID
	// MsgCompress decides whether net message payload compress enable.
	MsgCompress bool
	// Insecurity decides whether insecurity enable.
	// It is invalid in some implementations of network.
	Insecurity bool
}

HostConfig contains necessary parameters for BasicHost.

func (*HostConfig) AddBlackPeers

func (c *HostConfig) AddBlackPeers(pidStr ...string) error

func (*HostConfig) AddDirectPeer

func (c *HostConfig) AddDirectPeer(addr string) error

func (*HostConfig) NewHost

func (c *HostConfig) NewHost(networkType NetworkType, ctx context.Context, logger api.Logger) (*BasicHost, error)

NewHost create a BasicHost instance. Supported network type : QuicNetwork, TcpNetwork

type NetworkType

type NetworkType string

NetworkType is the type of transport layer.

const (
	// UnknownNetwork type
	UnknownNetwork NetworkType = "UNKNOWN"
	// QuicNetwork type
	QuicNetwork NetworkType = "QUIC"
	// TcpNetwork type
	TcpNetwork NetworkType = "TCP"
)

func ConfirmNetworkTypeByAddr

func ConfirmNetworkTypeByAddr(addr ma.Multiaddr) NetworkType

ConfirmNetworkTypeByAddr return a network type supported that for the address. If the format of address is wrong, or it is an unsupported address, return UnknownNetwork.

type Option

type Option func(cfg *networkConfig) error

Option of network instance.

func WithCtx

func WithCtx(ctx context.Context) Option

WithCtx designate ctx given as the context of network.

func WithEnableTls

func WithEnableTls(enable bool) Option

WithEnableTls make tls usable.

func WithLoadPidFunc

func WithLoadPidFunc(loadPidFunc types.LoadPeerIdFromCMTlsCertFunc) Option

WithLoadPidFunc set a types.LoadPeerIdFromTlsCertFunc for loading peer.ID from x509 certs.

func WithLocalPID

func WithLocalPID(pid peer.ID) Option

WithLocalPID designate the local peer.ID of network.

func WithTlcCfg

func WithTlcCfg(cfg *cmTls.Config) Option

WithTlcCfg set the configuration for TLS.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL