Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Discovered ¶
type Discovered[T comparable] struct { // Address is the local address of a discovered peer. Address string // Payload is the associated payload from discovered peer. Payload T Metadata *Metadata }
Discovered is the structure of the discovered peers, which holds their local address (port removed) and a payload if there is one.
func Discover ¶
func Discover[T comparable](ctx context.Context, settings ...Settings[T]) (discoveries []Discovered[T], err error)
Discover will use the created settings to scan for LAN peers. It will return an array of the discovered peers and their associate payloads. It will not return broadcasts sent to itself.
func (Discovered[T]) String ¶
func (d Discovered[T]) String() string
type IPVersion ¶
type IPVersion uint
IPVersion specifies the version of the Internet Protocol to be used.
type Metadata ¶
type Metadata struct {
Data interface{}
}
Metadata is the metadata associated with a discovered peer. To update the metadata, assign your own metadata to the Metadata.Data field. The metadata is not protected by a mutex, so you must do this yourself. The metadata update happens by pointer, to keep the library backwards compatible.
type NetPacketConn ¶
type PacketConn4 ¶
type PacketConn4 struct {
*ipv4.PacketConn
}
type PacketConn6 ¶
type PacketConn6 struct {
*ipv6.PacketConn
}
func (PacketConn6) SetMulticastTTL ¶
func (pc6 PacketConn6) SetMulticastTTL(i int) error
SetMulticastTTL wraps the hop limit of ipv6
type PeerDiscovery ¶
type PeerDiscovery[T comparable] struct { sync.RWMutex // contains filtered or unexported fields }
PeerDiscovery is the object that can do the discovery for finding LAN peers.
func NewPeerDiscovery ¶
func NewPeerDiscovery[T comparable](ctx context.Context, settings ...Settings[T]) (pd *PeerDiscovery[T], err error)
func (*PeerDiscovery[T]) ActivePeers ¶
func (p *PeerDiscovery[T]) ActivePeers() (peers []*PeerState)
func (*PeerDiscovery[T]) Shutdown ¶
func (p *PeerDiscovery[T]) Shutdown()
type PeerState ¶
type PeerState struct { Address string // contains filtered or unexported fields }
PeerState is the state of a peer that has been discovered. It contains the address of the peer, the last time it was seen, the last payload it sent, and the metadata associated with it. To update the metadata, assign your own metadata to the Metadata.Data field. The metadata is not protected by a mutex, so you must do this yourself.
type Settings ¶
type Settings[T comparable] struct { // Limit is the number of peers to discover, use < 1 for unlimited. Limit int // Port is the port to broadcast on (the peers must also broadcast using the same port). // The default port is 9999. Port string // MulticastAddress specifies the multicast address. // You should be able to use any of 224.0.0.0/4 or ff00::/8. // By default it uses the Simple Service Discovery Protocol // address (239.255.255.250 for IPv4 or ff02::c for IPv6). MulticastAddress string // Payload is the bytes that are sent out with each broadcast. Must be short. Payload T // PayloadFunc is the function that will be called to dynamically generate payload // before every broadcast. If this pointer is nil `Payload` field will be broadcasted instead. PayloadFunc func() T // Delay is the amount of time between broadcasts. The default delay is 1 second. Delay time.Duration // AllowSelf will allow discovery the local machine (default false) AllowSelf bool // DisableBroadcast will not allow sending out a broadcast DisableBroadcast bool // IPVersion specifies the version of the Internet Protocol (default IPv4) IPVersion IPVersion // Notify will be called each time a new peer was discovered. // The default is nil, which means no notification whatsoever. Notify func(Discovered[T]) // NotifyLost will be called each time a peer was lost. // The default is nil, which means no notification whatsoever. // This function should not take too long to execute, as it is called // from the peer garbage collector. NotifyLost func(LostPeer) // contains filtered or unexported fields }
Settings are the settings that can be specified for doing peer discovery.