-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulticast_conn.go
46 lines (38 loc) · 1.3 KB
/
multicast_conn.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
package netx
import (
"fmt"
"io"
"github.com/simia-tech/netx/value"
)
var dialMulticastFuncs = map[string]DialMulticastFunc{}
// DialMulticastFunc defines the signature of the Dial function.
type DialMulticastFunc func(string, *value.Options) (io.WriteCloser, error)
// RegisterDialMulticast registers the provided DialMulticast method under the provided network name.
func RegisterDialMulticast(network string, dialMulticastFunc DialMulticastFunc) {
dialMulticastFuncs[network] = dialMulticastFunc
}
// RegisteredDialMulticastNetworks returns the available networks for the DialMulticast function.
func RegisteredDialMulticastNetworks() []string {
networks := []string{}
for network := range dialMulticastFuncs {
networks = append(networks, network)
}
return networks
}
// DialMulticast opens a multicast connection on the provided network to the provided address.
func DialMulticast(network, address string, options ...value.Option) (io.WriteCloser, error) {
o := &value.Options{}
for _, option := range options {
if option == nil {
continue
}
if err := option(o); err != nil {
return nil, err
}
}
dialMulticastFunc, ok := dialMulticastFuncs[network]
if ok {
return dialMulticastFunc(address, o)
}
return nil, fmt.Errorf("no DialMulticast function registered for network [%s]", network)
}