Documentation ¶
Index ¶
- Constants
- Variables
- type ComparablePeerID
- type ConfigManager
- type ConnectPeerOption
- type Manager
- func (m *Manager) AllNeighbors() []*Neighbor
- func (m *Manager) AllNeighborsIDs() (ids []peer.ID)
- func (m *Manager) DialPeer(ctx context.Context, peer *network.Peer, opts ...ConnectPeerOption) error
- func (m *Manager) DropNeighbor(id peer.ID) error
- func (m *Manager) LocalPeerID() peer.ID
- func (m *Manager) NeighborsByID(ids []peer.ID) []*Neighbor
- func (m *Manager) P2PHost() host.Host
- func (m *Manager) RegisterProtocol(factory func() proto.Message, handler func(peer.ID, proto.Message) error)
- func (m *Manager) Send(packet proto.Message, to ...peer.ID)
- func (m *Manager) Shutdown()
- func (m *Manager) UnregisterProtocol()
- type Neighbor
- type NeighborDisconnectedFunc
- type NeighborEvents
- type PacketReceivedFunc
- type PacketsStream
- type PeerConfig
- type PeerConfigItem
- type ProtocolHandler
Constants ¶
const (
NeighborsSendQueueSize = 20_000
)
Variables ¶
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") )
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 ¶
NewManager creates a new Manager.
func (*Manager) AllNeighbors ¶
AllNeighbors returns all the neighbors that are currently connected.
func (*Manager) AllNeighborsIDs ¶
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 ¶
DropNeighbor disconnects the neighbor with the given ID and the group.
func (*Manager) LocalPeerID ¶
LocalPeerID returns the local peer ID.
func (*Manager) NeighborsByID ¶
NeighborsByID returns all the neighbors that are currently connected corresponding to the supplied ids.
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) 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 ¶
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) ConnectionEstablished ¶
ConnectionEstablished returns the connection established.
func (*Neighbor) PacketsRead ¶
PacketsRead returns number of packets this neighbor has received.
func (*Neighbor) PacketsWritten ¶
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 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 (p *PeerConfigItem) Clone() onchangemap.Item[string, *ComparablePeerID]
func (*PeerConfigItem) ID ¶
func (p *PeerConfigItem) ID() *ComparablePeerID