Documentation ¶
Index ¶
- Variables
- func GetAddress(p *peer.Peer) string
- type AcceptMatcher
- type ConnectPeerOption
- type Manager
- func (m *Manager) AddInbound(ctx context.Context, p *peer.Peer, group NeighborsGroup, ...) error
- func (m *Manager) AddOutbound(ctx context.Context, p *peer.Peer, group NeighborsGroup, ...) error
- func (m *Manager) AllNeighbors() []*Neighbor
- func (m *Manager) CloseStream(s network.Stream)
- func (m *Manager) DropNeighbor(id identity.ID, group NeighborsGroup) error
- func (m *Manager) GetNeighbor(id identity.ID) (*Neighbor, error)
- func (m *Manager) GetNeighborsByID(ids []identity.ID) []*Neighbor
- func (m *Manager) GetP2PHost() host.Host
- func (m *Manager) MatchNewStream(stream network.Stream) *AcceptMatcher
- func (m *Manager) NeighborGroupEvents(group NeighborsGroup) *NeighborGroupEvents
- func (m *Manager) RegisterProtocol(protocolID protocol.ID, protocolHandler *ProtocolHandler)
- func (m *Manager) Stop()
- func (m *Manager) UnregisterProtocol(protocolID protocol.ID)
- type Neighbor
- type NeighborAddedEvent
- type NeighborDisconnectedEvent
- type NeighborEvents
- type NeighborGroupEvents
- type NeighborPacketReceivedEvent
- type NeighborRemovedEvent
- type NeighborsGroup
- type PacketsStream
- type ProtocolHandler
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotRunning is returned when a neighbor is added to a stopped or not yet started p2p manager. ErrNotRunning = errors.New("manager not running") // ErrUnknownNeighbor is returned when the specified neighbor is not known to the p2p manager. ErrUnknownNeighbor = errors.New("unknown neighbor") // ErrLoopbackNeighbor is returned when the own peer is specified as a neighbor. ErrLoopbackNeighbor = errors.New("loopback connection not allowed") // ErrDuplicateNeighbor is returned when the same peer is added more than once as a neighbor. ErrDuplicateNeighbor = errors.New("already connected") // ErrNeighborQueueFull is returned when the send queue is already full. ErrNeighborQueueFull = errors.New("send queue is full") )
var ( // ErrTimeout is returned when an expected incoming connection was not received in time. ErrTimeout = errors.New("accept timeout") // ErrDuplicateAccept is returned when the server already registered an accept request for that peer ID. ErrDuplicateAccept = errors.New("accept request for that peer already exists") // ErrNoP2P means that the given peer does not support the p2p service. ErrNoP2P = errors.New("peer does not have a p2p service") )
Functions ¶
func GetAddress ¶
GetAddress returns the address of the p2p service.
Types ¶
type AcceptMatcher ¶
type AcceptMatcher struct { Peer *peer.Peer // connecting peer Libp2pID libp2ppeer.ID StreamCh chan *PacketsStream }
AcceptMatcher holds data to match an existing connection with a peer.
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 {
// contains filtered or unexported fields
}
The Manager handles the connected neighbors.
func NewManager ¶
NewManager creates a new Manager.
func (*Manager) AddInbound ¶
func (m *Manager) AddInbound(ctx context.Context, p *peer.Peer, group NeighborsGroup, connectOpts ...ConnectPeerOption, ) error
AddInbound tries to add a neighbor by accepting an incoming connection from that peer.
func (*Manager) AddOutbound ¶
func (m *Manager) AddOutbound(ctx context.Context, p *peer.Peer, group NeighborsGroup, connectOpts ...ConnectPeerOption, ) error
AddOutbound tries to add a neighbor by connecting to that peer.
func (*Manager) AllNeighbors ¶
AllNeighbors returns all the neighbors that are currently connected.
func (*Manager) CloseStream ¶
CloseStream closes a stream.
func (*Manager) DropNeighbor ¶
func (m *Manager) DropNeighbor(id identity.ID, group NeighborsGroup) error
DropNeighbor disconnects the neighbor with the given ID and the group.
func (*Manager) GetNeighbor ¶
GetNeighbor returns the neighbor by its id.
func (*Manager) GetNeighborsByID ¶
GetNeighborsByID returns all the neighbors that are currently connected corresponding to the supplied ids.
func (*Manager) GetP2PHost ¶
GetP2PHost returns the libp2p host.
func (*Manager) MatchNewStream ¶
func (m *Manager) MatchNewStream(stream network.Stream) *AcceptMatcher
MatchNewStream matches a new stream with a peer.
func (*Manager) NeighborGroupEvents ¶
func (m *Manager) NeighborGroupEvents(group NeighborsGroup) *NeighborGroupEvents
NeighborGroupEvents returns the events related to the neighbor group.
func (*Manager) RegisterProtocol ¶
func (m *Manager) RegisterProtocol(protocolID protocol.ID, protocolHandler *ProtocolHandler)
RegisterProtocol registers a new protocol.
func (*Manager) Stop ¶
func (m *Manager) Stop()
Stop stops the manager and closes all established connections.
func (*Manager) UnregisterProtocol ¶
UnregisterProtocol unregisters a protocol.
type Neighbor ¶
type Neighbor struct { *peer.Peer Group NeighborsGroup Events *NeighborEvents Log *logger.Logger // contains filtered or unexported fields }
Neighbor describes the established p2p connection to another peer.
func NewNeighbor ¶
func NewNeighbor(p *peer.Peer, group NeighborsGroup, protocols map[protocol.ID]*PacketsStream, log *logger.Logger) *Neighbor
NewNeighbor creates a new neighbor from the provided peer and connection.
func (*Neighbor) ConnectionEstablished ¶
ConnectionEstablished returns the connection established.
func (*Neighbor) GetStream ¶
func (n *Neighbor) GetStream(protocol protocol.ID) *PacketsStream
GetStream returns the stream for the given protocol.
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 NeighborAddedEvent ¶
type NeighborAddedEvent struct {
Neighbor *Neighbor
}
NeighborAddedEvent holds data about the added neighbor.
type NeighborDisconnectedEvent ¶
type NeighborDisconnectedEvent struct{}
NeighborDisconnectedEvent holds data about the disconnected neighbor.
type NeighborEvents ¶
type NeighborEvents struct { // Fired when a neighbor disconnects. Disconnected *event.Event[*NeighborDisconnectedEvent] PacketReceived *event.Event[*NeighborPacketReceivedEvent] }
NeighborEvents is a collection of events specific to a neighbor.
func NewNeighborEvents ¶
func NewNeighborEvents() (new *NeighborEvents)
NewNeighborEvents returns a new instance of NeighborEvents.
type NeighborGroupEvents ¶
type NeighborGroupEvents struct { // Fired when a neighbor connection has been established. NeighborAdded *event.Event[*NeighborAddedEvent] // Fired when a neighbor has been removed. NeighborRemoved *event.Event[*NeighborRemovedEvent] }
NeighborGroupEvents is a collection of events specific for a particular neighbors group, e.g "manual" or "auto".
func NewNeighborGroupEvents ¶
func NewNeighborGroupEvents() (new *NeighborGroupEvents)
NewNeighborGroupEvents returns a new instance of NeighborGroupEvents.
type NeighborPacketReceivedEvent ¶
type NeighborPacketReceivedEvent struct { Neighbor *Neighbor Protocol protocol.ID Packet proto.Message }
NeighborPacketReceivedEvent holds data about a protocol and packet received from a neighbor.
type NeighborRemovedEvent ¶
type NeighborRemovedEvent struct {
Neighbor *Neighbor
}
NeighborRemovedEvent holds data about the removed neighbor.
type NeighborsGroup ¶
type NeighborsGroup int8
NeighborsGroup is an enum type for various neighbors groups like auto/manual.
const ( // NeighborsGroupAuto represents a neighbors group that is managed automatically. NeighborsGroupAuto NeighborsGroup = iota // NeighborsGroupManual represents a neighbors group that is managed manually. NeighborsGroupManual )
type PacketsStream ¶
PacketsStream represents a stream of packets.
func NewPacketsStream ¶
func NewPacketsStream(stream network.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 ProtocolHandler ¶
type ProtocolHandler struct { PacketFactory func() proto.Message NegotiationSend func(ps *PacketsStream) error NegotiationReceive func(ps *PacketsStream) error PacketHandler func(*Neighbor, proto.Message) error }
ProtocolHandler holds callbacks to handle a protocol.