Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client option to detect disconnected clients #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 47 additions & 34 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@

// Client structure encapsulates both IPv4/IPv6 UDP connections.
type client struct {
ipv4conn *ipv4.PacketConn
ipv6conn *ipv6.PacketConn
ifaces []net.Interface
ipv4conn *ipv4.PacketConn
ipv6conn *ipv6.PacketConn
ifaces []net.Interface
unannouncements bool
}

type clientOpts struct {
listenOn IPType
ifaces []net.Interface
listenOn IPType
ifaces []net.Interface
unannouncements bool
}

// ClientOption fills the option struct to configure intefaces, etc.
Expand All @@ -63,6 +65,14 @@
}
}

// Emit an entry with an expiry in the past if a previously emitted entry is unannounced.
// This is never guaranteed to occur, but can speed up detection of disconnected clients.
func Unannouncements() ClientOption {
return func(o *clientOpts) {
o.unannouncements = true
}

Check warning on line 73 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L70-L73

Added lines #L70 - L73 were not covered by tests
}

// Browse for all services of a given type in a given domain.
// Received entries are sent on the entries channel.
// It blocks until the context is canceled (or an error occurs).
Expand Down Expand Up @@ -157,9 +167,10 @@
}

return &client{
ipv4conn: ipv4conn,
ipv6conn: ipv6conn,
ifaces: ifaces,
ipv4conn: ipv4conn,
ipv6conn: ipv6conn,
ifaces: ifaces,
unannouncements: opts.unannouncements,
}, nil
}

Expand All @@ -177,12 +188,12 @@
}

// Iterate through channels from listeners goroutines
var entries map[string]*ServiceEntry
sentEntries := make(map[string]*ServiceEntry)

ticker := time.NewTicker(cleanupFreq)
defer ticker.Stop()
for {
var entries map[string]*ServiceEntry
var now time.Time
select {
case <-ctx.Done():
Expand Down Expand Up @@ -269,35 +280,37 @@
}
}

if len(entries) > 0 {
for k, e := range entries {
if !e.Expiry.After(now) {
delete(entries, k)
delete(sentEntries, k)
continue
}
if _, ok := sentEntries[k]; ok {
continue
for k, e := range entries {
if !e.Expiry.After(now) {
// Implies TTL=0, meaning a "Goodbye Packet".
if _, ok := sentEntries[k]; ok && c.unannouncements {
params.Entries <- e

Check warning on line 287 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L285-L287

Added lines #L285 - L287 were not covered by tests
}
delete(sentEntries, k)
continue

Check warning on line 290 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L289-L290

Added lines #L289 - L290 were not covered by tests
}
if _, ok := sentEntries[k]; ok {
// Already sent, suppress duplicates
continue
}

// If this is an DNS-SD query do not throw PTR away.
// It is expected to have only PTR for enumeration
if params.ServiceRecord.ServiceTypeName() != params.ServiceRecord.ServiceName() {
// Require at least one resolved IP address for ServiceEntry
// TODO: wait some more time as chances are high both will arrive.
if len(e.AddrIPv4) == 0 && len(e.AddrIPv6) == 0 {
continue
}
}
// Submit entry to subscriber and cache it.
// This is also a point to possibly stop probing actively for a
// service entry.
params.Entries <- e
sentEntries[k] = e
if !params.isBrowsing {
params.disableProbing()
// If this is an DNS-SD query do not throw PTR away.
// It is expected to have only PTR for enumeration
if params.ServiceRecord.ServiceTypeName() != params.ServiceRecord.ServiceName() {
// Require at least one resolved IP address for ServiceEntry
// TODO: wait some more time as chances are high both will arrive.
if len(e.AddrIPv4) == 0 && len(e.AddrIPv6) == 0 {
continue

Check warning on line 303 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L303

Added line #L303 was not covered by tests
}
}
// Submit entry to subscriber and cache it.
// This is also a point to possibly stop probing actively for a
// service entry.
params.Entries <- e
sentEntries[k] = e
if !params.isBrowsing {
params.disableProbing()
}

Check warning on line 313 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L312-L313

Added lines #L312 - L313 were not covered by tests
}
}
}
Expand Down
Loading