p2p

package
v1.0.0-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NeighborsSendQueueSize = 20_000
)

Variables

View Source
var (
	// ErrNotRunning is returned when a neighbor is added to a stopped or not yet started p2p manager.
	ErrNotRunning = ierrors.New("manager not running")
	// ErrUnknownNeighbor is returned when the specified neighbor is not known to the p2p manager.
	ErrUnknownNeighbor = ierrors.New("unknown neighbor")
	// ErrLoopbackNeighbor is returned when the own peer is specified as a neighbor.
	ErrLoopbackNeighbor = ierrors.New("loopback connection not allowed")
	// ErrDuplicateNeighbor is returned when the same peer is added more than once as a neighbor.
	ErrDuplicateNeighbor = ierrors.New("already connected")
	// ErrNeighborQueueFull is returned when the send queue is already full.
	ErrNeighborQueueFull = ierrors.New("send queue is full")
)
View Source
var (
	// ErrTimeout is returned when an expected incoming connection was not received in time.
	ErrTimeout = ierrors.New("accept timeout")
	// ErrDuplicateAccept is returned when the server already registered an accept request for that peer ID.
	ErrDuplicateAccept = ierrors.New("accept request for that peer already exists")
	// ErrNoP2P means that the given peer does not support the p2p service.
	ErrNoP2P = ierrors.New("peer does not have a p2p service")
)

Functions

This section is empty.

Types

type ComparablePeerID

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

ComparablePeerID implements the constraints.ComparableStringer interface for the onChangeMap.

func NewComparablePeerID

func NewComparablePeerID(peerID peer.ID) *ComparablePeerID

func (*ComparablePeerID) Key

func (c *ComparablePeerID) Key() string

func (*ComparablePeerID) String

func (c *ComparablePeerID) String() string

type ConfigManager

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

ConfigManager handles the list of peers that are stored in the peering config. It calls a function if the list changed.

func NewConfigManager

func NewConfigManager(storeCallback func([]*PeerConfigItem) error) *ConfigManager

NewConfigManager creates a new config manager.

func (*ConfigManager) AddPeer

func (pm *ConfigManager) AddPeer(multiAddress multiaddr.Multiaddr, alias string) error

AddPeer adds a peer to the config manager.

func (*ConfigManager) Peers

func (pm *ConfigManager) Peers() []*PeerConfigItem

Peers returns all known peers.

func (*ConfigManager) RemovePeer

func (pm *ConfigManager) RemovePeer(peerID peer.ID) error

RemovePeer removes a peer from the config manager.

func (*ConfigManager) Store

func (pm *ConfigManager) Store() error

Store calls the storeCallback if storeOnChange is active.

func (*ConfigManager) StoreOnChange

func (pm *ConfigManager) StoreOnChange(enabled bool)

StoreOnChange sets whether storing changes to the config is active or not.

type ConnectPeerOption

type ConnectPeerOption func(conf *connectPeerConfig)

ConnectPeerOption defines an option for the DialPeer and AcceptPeer methods.

func WithNoDefaultTimeout

func WithNoDefaultTimeout() ConnectPeerOption

WithNoDefaultTimeout returns a ConnectPeerOption that disables the default timeout for dial or accept.

type Manager

type Manager struct {
	Events *NeighborEvents
	// contains filtered or unexported fields
}

The Manager handles the connected neighbors.

func NewManager

func NewManager(libp2pHost host.Host, peerDB *network.DB, logger log.Logger) *Manager

NewManager creates a new Manager.

func (*Manager) AllNeighbors

func (m *Manager) AllNeighbors() []*Neighbor

AllNeighbors returns all the neighbors that are currently connected.

func (*Manager) AllNeighborsIDs

func (m *Manager) AllNeighborsIDs() (ids []peer.ID)

AllNeighborsIDs returns all the ids of the neighbors that are currently connected.

func (*Manager) DialPeer

func (m *Manager) DialPeer(ctx context.Context, peer *network.Peer, opts ...ConnectPeerOption) error

DialPeer connects to a peer.

func (*Manager) DropNeighbor

func (m *Manager) DropNeighbor(id peer.ID) error

DropNeighbor disconnects the neighbor with the given ID and the group.

func (*Manager) LocalPeerID

func (m *Manager) LocalPeerID() peer.ID

LocalPeerID returns the local peer ID.

func (*Manager) NeighborsByID

func (m *Manager) NeighborsByID(ids []peer.ID) []*Neighbor

NeighborsByID returns all the neighbors that are currently connected corresponding to the supplied ids.

func (*Manager) P2PHost

func (m *Manager) P2PHost() host.Host

P2PHost returns the lib-p2p host.

func (*Manager) RegisterProtocol

func (m *Manager) RegisterProtocol(factory func() proto.Message, handler func(peer.ID, proto.Message) error)

RegisterProtocol registers the handler for the protocol within the manager.

func (*Manager) Send

func (m *Manager) Send(packet proto.Message, to ...peer.ID)

Send sends a message with the specific protocol to a set of neighbors.

func (*Manager) Shutdown

func (m *Manager) Shutdown()

Shutdown stops the manager and closes all established connections.

func (*Manager) UnregisterProtocol

func (m *Manager) UnregisterProtocol()

UnregisterProtocol unregisters the handler for the protocol.

type Neighbor

type Neighbor struct {
	*network.Peer
	// contains filtered or unexported fields
}

Neighbor describes the established p2p connection to another peer.

func NewNeighbor

func NewNeighbor(parentLogger log.Logger, p *network.Peer, stream *PacketsStream, packetReceivedCallback PacketReceivedFunc, disconnectedCallback NeighborDisconnectedFunc) *Neighbor

NewNeighbor creates a new neighbor from the provided peer and connection.

func (*Neighbor) Close

func (n *Neighbor) Close()

Close closes the connection with the neighbor.

func (*Neighbor) ConnectionEstablished

func (n *Neighbor) ConnectionEstablished() time.Time

ConnectionEstablished returns the connection established.

func (*Neighbor) Enqueue

func (n *Neighbor) Enqueue(packet proto.Message, protocolID protocol.ID)

func (*Neighbor) PacketsRead

func (n *Neighbor) PacketsRead() uint64

PacketsRead returns number of packets this neighbor has received.

func (*Neighbor) PacketsWritten

func (n *Neighbor) PacketsWritten() uint64

PacketsWritten returns number of packets this neighbor has sent.

type NeighborDisconnectedFunc

type NeighborDisconnectedFunc func(neighbor *Neighbor)

type NeighborEvents

type NeighborEvents struct {
	// Fired when a neighbor connection has been established.
	NeighborAdded *event.Event1[*Neighbor]

	// Fired when a neighbor has been removed.
	NeighborRemoved *event.Event1[*Neighbor]
}

NeighborEvents is a collection of events specific for a particular neighbors group, e.g "manual" or "auto".

func NewNeighborEvents

func NewNeighborEvents() *NeighborEvents

NewNeighborEvents returns a new instance of NeighborGroupEvents.

type PacketReceivedFunc

type PacketReceivedFunc func(neighbor *Neighbor, packet proto.Message)

type PacketsStream

type PacketsStream struct {
	p2pnetwork.Stream
	// contains filtered or unexported fields
}

PacketsStream represents a stream of packets.

func NewPacketsStream

func NewPacketsStream(stream p2pnetwork.Stream, packetFactory func() proto.Message) *PacketsStream

NewPacketsStream creates a new PacketsStream.

func (*PacketsStream) ReadPacket

func (ps *PacketsStream) ReadPacket(message proto.Message) error

ReadPacket reads a packet from the stream.

func (*PacketsStream) WritePacket

func (ps *PacketsStream) WritePacket(message proto.Message) error

WritePacket writes a packet to the stream.

type PeerConfig

type PeerConfig struct {
	MultiAddress string `json:"multiAddress" koanf:"multiAddress"`
	Alias        string `json:"alias" koanf:"alias"`
}

PeerConfig holds the initial information about peers.

type PeerConfigItem

type PeerConfigItem struct {
	*PeerConfig
	// contains filtered or unexported fields
}

PeerConfigItem implements the Item interface for the onChangeMap.

func NewPeerConfigItem

func NewPeerConfigItem(peerConfig *PeerConfig) (*PeerConfigItem, error)

func (*PeerConfigItem) Clone

func (*PeerConfigItem) ID

type ProtocolHandler

type ProtocolHandler struct {
	PacketFactory func() proto.Message
	PacketHandler func(peer.ID, proto.Message) error
}

ProtocolHandler holds callbacks to handle a protocol.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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