import "github.com/libp2p/go-libp2p/p2p/host/basic"
var ( // DefaultNegotiationTimeout is the default value for HostOpts.NegotiationTimeout. DefaultNegotiationTimeout = time.Second * 60 // DefaultAddrsFactory is the default value for HostOpts.AddrsFactory. DefaultAddrsFactory = func(addrs []ma.Multiaddr) []ma.Multiaddr { return addrs } )
AddrsFactory functions can be passed to New in order to override addresses returned by Addrs.
type BasicHost struct { AddrsFactory AddrsFactory AutoNat autonat.AutoNAT // contains filtered or unexported fields }
BasicHost is the basic implementation of the host.Host interface. This particular host implementation:
* uses a protocol muxer to mux per-protocol streams * uses an identity service to send + receive node information * uses a nat service to establish NAT port mappings
New constructs and sets up a new *BasicHost with given Network and options. The following options can be passed: * NATPortMap * AddrsFactory * connmgr.ConnManager * madns.Resolver
This function is deprecated in favor of NewHost and HostOpts.
NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
Addrs returns listening addresses that are safe to announce to the network. The output is the same as AllAddrs, but processed by AddrsFactory.
AllAddrs returns all the addresses of BasicHost at this moment in time. It's ok to not include addresses if they're not available to be used now.
Close shuts down the Host's services (network, etc).
func (h *BasicHost) ConnManager() connmgr.ConnManager
Connect ensures there is a connection between this host and the peer with given peer.ID. If there is not an active connection, Connect will issue a h.Network.Dial, and block until a connection is open, or an error is returned. Connect will absorb the addresses in pi into its internal peerstore. It will also resolve any /dns4, /dns6, and /dnsaddr addresses.
ID returns the (local) peer.ID associated with this Host
IDService returns
Mux returns the Mux multiplexing incoming streams to protocol handlers
Network returns the Network interface of the Host
func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (network.Stream, error)
NewStream opens a new stream to given peer p, and writes a p2p/protocol header with given protocol.ID. If there is no connection to p, attempts to create one. If ProtocolID is "", writes no header. (Threadsafe)
Peerstore returns the Host's repository of Peer Addresses and Keys.
RemoveStreamHandler returns ..
SetStreamHandler sets the protocol handler on the Host's Mux. This is equivalent to:
host.Mux().SetHandler(proto, handler)
(Threadsafe)
func (h *BasicHost) SetStreamHandlerMatch(pid protocol.ID, m func(string) bool, handler network.StreamHandler)
SetStreamHandlerMatch sets the protocol handler on the Host's Mux using a matching function to do protocol comparisons
SignalAddressChange signals to the host that it needs to determine whether our listen addresses have recently changed. Warning: this interface is unstable and may disappear in the future.
Start starts background tasks in the host
type HostOpts struct { // MultistreamMuxer is essential for the *BasicHost and will use a sensible default value if omitted. MultistreamMuxer *msmux.MultistreamMuxer // NegotiationTimeout determines the read and write timeouts on streams. // If 0 or omitted, it will use DefaultNegotiationTimeout. // If below 0, timeouts on streams will be deactivated. NegotiationTimeout time.Duration // AddrsFactory holds a function which can be used to override or filter the result of Addrs. // If omitted, there's no override or filtering, and the results of Addrs and AllAddrs are the same. AddrsFactory AddrsFactory // MultiaddrResolves holds the go-multiaddr-dns.Resolver used for resolving // /dns4, /dns6, and /dnsaddr addresses before trying to connect to a peer. MultiaddrResolver *madns.Resolver // NATManager takes care of setting NAT port mappings, and discovering external addresses. // If omitted, this will simply be disabled. NATManager func(network.Network) NATManager // ConnManager is a libp2p connection manager ConnManager connmgr.ConnManager // EnablePing indicates whether to instantiate the ping service EnablePing bool // UserAgent sets the user-agent for the host. Defaults to ClientVersion. UserAgent string // DisableSignedPeerRecord disables the generation of Signed Peer Records on this host. DisableSignedPeerRecord bool }
HostOpts holds options that can be passed to NewHost in order to customize construction of the *BasicHost.
type NATManager interface { // Get the NAT device managed by the NAT manager. NAT() *inat.NAT // Receive a notification when the NAT device is ready for use. Ready() <-chan struct{} // Close all resources associated with a NAT manager. Close() error }
A simple interface to manage NAT devices.
func NewNATManager(net network.Network) NATManager
Create a NAT manager.
Option is a type used to pass in options to the host.
Deprecated in favor of HostOpts and NewHost.
NATPortMap makes the host attempt to open port-mapping in NAT devices for all its listeners. Pass in this option in the constructor to asynchronously a) find a gateway, b) open port mappings, c) republish port mappings periodically. The NATed addresses are included in the Host's Addrs() list.
This option is deprecated in favor of HostOpts and NewHost.
Package basichost imports 31 packages (graph) and is imported by 65 packages. Updated 2021-01-19. Refresh now. Tools for package owners.