Skip to content
Merged
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
19 changes: 19 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package mdns

import (
"context"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -67,13 +68,31 @@ func DefaultParams(service string) *QueryParam {
// to a channel. Sends will not block, so clients should make sure to
// either read or buffer.
func Query(params *QueryParam) error {
return QueryContext(context.Background(), params)
}

// QueryContext looks up a given service, in a domain, waiting at most
// for a timeout before finishing the query. The results are streamed
// to a channel. Sends will not block, so clients should make sure to
// either read or buffer. QueryContext will attempt to stop the query
// on cancellation.
func QueryContext(ctx context.Context, params *QueryParam) error {
// Create a new client
client, err := newClient(!params.DisableIPv4, !params.DisableIPv6)
if err != nil {
return err
}
defer client.Close()

go func() {
select {
case <-ctx.Done():
client.Close()
case <-client.closedCh:
return
}
}()

// Set the multicast interface
if params.Interface != nil {
if err := client.setInterface(params.Interface); err != nil {
Expand Down