-
Notifications
You must be signed in to change notification settings - Fork 27
/
option.go
77 lines (65 loc) · 1.53 KB
/
option.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
package ssdp
import "github.com/koron/go-ssdp/internal/multicast"
type config struct {
multicastConfig
advertiseConfig
}
func opts2config(opts []Option) (cfg config, err error) {
for _, o := range opts {
err := o.apply(&cfg)
if err != nil {
return config{}, err
}
}
return cfg, nil
}
type multicastConfig struct {
ttl int
sysIf bool
}
func (mc multicastConfig) options() (opts []multicast.ConnOption) {
if mc.ttl > 0 {
opts = append(opts, multicast.ConnTTL(mc.ttl))
}
if mc.sysIf {
opts = append(opts, multicast.ConnSystemAssginedInterface())
}
return opts
}
type advertiseConfig struct {
addHost bool
}
// Option is option set for SSDP API.
type Option interface {
apply(c *config) error
}
type optionFunc func(*config) error
func (of optionFunc) apply(c *config) error {
return of(c)
}
// TTL returns as Option that set TTL for multicast packets.
func TTL(ttl int) Option {
return optionFunc(func(c *config) error {
c.ttl = ttl
return nil
})
}
// OnlySystemInterface returns as Option that using only a system assigned
// multicast interface.
func OnlySystemInterface() Option {
return optionFunc(func(c *config) error {
c.sysIf = true
return nil
})
}
// AdvertiseHost returns as Option that add HOST header to response for
// M-SEARCH requests.
// This option works with Advertise() function only.
// This is added to support SmartThings.
// See https://github.com/koron/go-ssdp/issues/30 for details.
func AdvertiseHost() Option {
return optionFunc(func(c *config) error {
c.addHost = true
return nil
})
}