This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbrowse.go
133 lines (118 loc) · 3.08 KB
/
browse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package dnssd
import "unsafe"
// BrowseCallbackFunc is called when an error occurs or a service is lost or found.
type BrowseCallbackFunc func(op *BrowseOp, err error, add bool, interfaceIndex int, name string, serviceType string, domain string)
// BrowseOp represents a query for services of a particular type.
type BrowseOp struct {
baseOp
stype string
domain string
callback BrowseCallbackFunc
}
// NewBrowseOp creates a new BrowseOp with the given service type and call back set.
func NewBrowseOp(serviceType string, f BrowseCallbackFunc) *BrowseOp {
op := &BrowseOp{}
op.SetType(serviceType)
op.SetCallback(f)
return op
}
// StartBrowseOp returns the equivalent of calling NewBrowseOp and Start().
func StartBrowseOp(serviceType string, f BrowseCallbackFunc) (*BrowseOp, error) {
op := NewBrowseOp(serviceType, f)
return op, op.Start()
}
// Type returns the service type associated with the op.
func (o *BrowseOp) Type() string {
o.m.Lock()
defer o.m.Unlock()
return o.stype
}
// SetType sets the service type associated with the op.
func (o *BrowseOp) SetType(s string) error {
o.m.Lock()
defer o.m.Unlock()
if o.started {
return ErrStarted
}
o.stype = s
return nil
}
// Domain returns the domain associated with the op.
func (o *BrowseOp) Domain() string {
o.m.Lock()
defer o.m.Unlock()
return o.domain
}
// SetDomain sets the domain associated with the op.
func (o *BrowseOp) SetDomain(s string) error {
o.m.Lock()
defer o.m.Unlock()
if o.started {
return ErrStarted
}
o.domain = s
return nil
}
// SetCallback sets the function to call when an error occurs or a service is lost or found.
func (o *BrowseOp) SetCallback(f BrowseCallbackFunc) error {
o.m.Lock()
defer o.m.Unlock()
if o.started {
return ErrStarted
}
o.callback = f
return nil
}
// Start begins the browse query.
func (o *BrowseOp) Start() error {
o.m.Lock()
defer o.m.Unlock()
if o.started {
return ErrStarted
}
if o.callback == nil {
return ErrMissingCallback
}
err := pollServer.startOp(o)
o.started = err == nil || err == ErrStarted
return err
}
func (o *BrowseOp) init(sharedref uintptr) (ref uintptr, err error) {
ref = sharedref
o.setFlag(_FlagsShareConnection, ref != 0)
if err = browseStart(&ref, o.flags, o.interfaceIndexC(), o.stype, o.domain, unsafe.Pointer(o)); err != nil {
ref = 0
}
return
}
// Stop stops the operation.
func (o *BrowseOp) Stop() {
o.m.Lock()
defer o.m.Unlock()
if !o.started {
return
}
o.started = false
pollServer.stopOp(o)
}
func (o *BrowseOp) handleError(e error) {
if !o.started {
return
}
o.started = false
pollServer.removePollOp(o)
queueCallback(func() { o.callback(o, e, false, 0, "", "", "") })
}
func dnssdBrowseCallback(sdRef unsafe.Pointer, flags, interfaceIndex uint32, err int32, name, stype, domain unsafe.Pointer, ctx unsafe.Pointer) {
o := (*BrowseOp)(ctx)
if e := getError(err); e != nil {
o.handleError(e)
} else {
a := flags&_FlagsAdd != 0
i := int(interfaceIndex)
n := cStringToString(name)
t := cStringToString(stype)
d := cStringToString(domain)
queueCallback(func() { o.callback(o, nil, a, i, n, t, d) })
}
}