p2p

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: Apache-2.0, BSD-2-Clause Imports: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

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

func GetAddress(p *peer.Peer) string

GetAddress returns the address of the p2p service.

Types

type AcceptMatcher

type AcceptMatcher struct {
	Peer          *peer.Peer // connecting peer
	Libp2pID      libp2ppeer.ID
	StreamChMutex sync.RWMutex
	StreamCh      map[protocol.ID]chan *PacketsStream
	Ctx           context.Context
	CtxCancel     context.CancelFunc
}

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

func NewManager(libp2pHost host.Host, local *peer.Local, log *logger.Logger) *Manager

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

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

AllNeighbors returns all the neighbors that are currently connected.

func (*Manager) AllNeighborsIDs

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

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

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

func (m *Manager) GetNeighbor(id identity.ID) (*Neighbor, error)

GetNeighbor returns the neighbor by its id.

func (*Manager) GetNeighborsByID

func (m *Manager) GetNeighborsByID(ids []identity.ID) []*Neighbor

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

func (*Manager) GetP2PHost

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

GetP2PHost returns the libp2p host.

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

func (m *Manager) Send(packet proto.Message, protocolID protocol.ID, to ...identity.ID) []*Neighbor

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

func (*Manager) Stop

func (m *Manager) Stop()

Stop stops the manager and closes all established connections.

func (*Manager) UnregisterProtocol

func (m *Manager) UnregisterProtocol(protocolID protocol.ID)

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

func (n *Neighbor) GetStream(protocol protocol.ID) *PacketsStream

GetStream returns the stream for the given protocol.

func (*Neighbor) PacketsRead

func (n *Neighbor) PacketsRead() (count uint64)

PacketsRead returns number of packets this neighbor has received.

func (*Neighbor) PacketsWritten

func (n *Neighbor) PacketsWritten() (count uint64)

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

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

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.

Jump to

Keyboard shortcuts

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