v0.0.7 Latest Latest

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

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




This section is empty.


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")


This section is empty.


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 {
	// NetType is the network type of liquid net.
	NetType NetworkType
	// 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 *tls.Config
	// LoadPidFunc is a function which type is types.LoadPeerIdFromTlsCertFunc, used to load peer.ID from x509 certs.
	LoadPidFunc types.LoadPeerIdFromTlsCertFunc
	// QTlsCfg is the configuration for both QUIC tls server and client.
	QTlsCfg *tls.Config
	// LoadPidFunc is a function which type is types.LoadPeerIdFromQTlsCertFunc,
	// used to load peer.ID from QUIC x509 certs.
	LoadPidFuncQ types.LoadPeerIdFromQTlsCertFunc
	// GMTlsServerCfg is the configuration for gm tls server.
	GMTlsServerCfg *gmtls.Config
	// GMTlsClientCfg is the configuration for gm tls client.
	GMTlsClientCfg *gmtls.Config
	// LoadPidFuncGm is a function which type is types.LoadPeerIdFromGMTlsCertFunc,
	// used to load peer.ID from gmx509 certs.
	LoadPidFuncGm types.LoadPeerIdFromGMTlsCertFunc
	// UseGMTls decides whether to use gm tls security.
	UseGMTls bool
	// 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. "","","[::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 a 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 WithGMTlcClientCfg

func WithGMTlcClientCfg(cfg *gmtls.Config) Option

WithGMTlcClientCfg set the configuration for GM TLS client.

func WithGMTlcServerCfg

func WithGMTlcServerCfg(cfg *gmtls.Config) Option

WithGMTlcServerCfg set the configuration for GM TLS server.

func WithGMTls

func WithGMTls(enable bool) Option

WithGMTls make gm tls usable.

func WithLoadPidFunc

func WithLoadPidFunc(loadPidFunc types.LoadPeerIdFromTlsCertFunc) Option

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

func WithLoadPidFuncGm

func WithLoadPidFuncGm(loadPidFuncGm types.LoadPeerIdFromGMTlsCertFunc) Option

WithLoadPidFuncGm set a types.LoadPeerIdFromGMTlsCertFunc for loading peer.ID from gmx509 certs.

func WithLoadPidFuncQ

func WithLoadPidFuncQ(loadPidFunc types.LoadPeerIdFromQTlsCertFunc) Option

WithLoadPidFuncQ set a types.LoadPeerIdFromQTlsCertFunc for loading peer.ID from qx509 certs.

func WithLocalPID

func WithLocalPID(pid peer.ID) Option

WithLocalPID designate the local peer.ID of network.

func WithQTlsCfg

func WithQTlsCfg(cfg *tls.Config) Option

WithQTlsCfg set the configuration for quic TLS.

func WithTlcCfg

func WithTlcCfg(cfg *tls.Config) Option

WithTlcCfg set the configuration for TLS.


Path Synopsis

Jump to

Keyboard shortcuts

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