-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
49 lines (41 loc) · 1.19 KB
/
listener.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
package netx
import (
"crypto/tls"
"net"
"github.com/simia-tech/netx/value"
)
var listenFuncs = map[string]ListenFunc{}
// ListenFunc defines the signature of the Listen function.
type ListenFunc func(string, *value.Options) (net.Listener, error)
// RegisterListen registers the provided Listen method under the provided network name.
func RegisterListen(network string, listenFunc ListenFunc) {
listenFuncs[network] = listenFunc
}
// RegisteredListenNetworks returns the available networks for the Listen function.
func RegisteredListenNetworks() []string {
networks := []string{}
for network := range listenFuncs {
networks = append(networks, network)
}
return networks
}
// Listen creates a listener on the provided network at the provided address.
func Listen(network, address string, options ...value.Option) (net.Listener, error) {
o := &value.Options{}
for _, option := range options {
if option == nil {
continue
}
if err := option(o); err != nil {
return nil, err
}
}
listenFunc, ok := listenFuncs[network]
if ok {
return listenFunc(address, o)
}
if o.TLSConfig == nil {
return net.Listen(network, address)
}
return tls.Listen(network, address, o.TLSConfig)
}