-
Notifications
You must be signed in to change notification settings - Fork 8
/
resolver.go
36 lines (29 loc) · 924 Bytes
/
resolver.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
package mwgp
import (
"context"
"fmt"
"net"
"strings"
)
type UDPAddrResolver interface {
ResolveUDPAddr(ctx context.Context, address string) (addr *net.UDPAddr, err error)
}
type UDPAddrResolverCreator = func(url string) (resolver UDPAddrResolver, err error)
var UDPAddrResolverCreators = map[string]UDPAddrResolverCreator{} // Type => Creator
type defaultUDPAddrResolver struct{}
func (d *defaultUDPAddrResolver) ResolveUDPAddr(ctx context.Context, address string) (addr *net.UDPAddr, err error) {
return net.ResolveUDPAddr("udp", address)
}
func newUDPAddrResolver(url string) (resolver UDPAddrResolver, err error) {
if url == "" {
resolver = &defaultUDPAddrResolver{}
return
}
resolverType := strings.SplitN(url, "+", 2)[0]
if creator, ok := UDPAddrResolverCreators[resolverType]; ok {
resolver, err = creator(url)
return
}
err = fmt.Errorf("unknown resolver type: %s", resolverType)
return
}