-
Notifications
You must be signed in to change notification settings - Fork 1
/
listen.go
196 lines (164 loc) · 4.37 KB
/
listen.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package bind
import (
"context"
"io"
"net"
"time"
)
var (
_ Listener = (*ListenConfig)(nil)
_ AllListener = (*ListenConfig)(nil)
_ TCPListener = (*ListenConfig)(nil)
_ AllTCPListener = (*ListenConfig)(nil)
_ UDPListener = (*ListenConfig)(nil)
_ AllUDPListener = (*ListenConfig)(nil)
)
// ListenConfig extends the standard net.ListeConfig with a central holder
// for the Context bound to the listeners
type ListenConfig struct {
net.ListenConfig
// Context used when registering the listeners
Context context.Context
}
// NewListenConfig assists creating a ListenConfig due to the two-layer definition
// making difficult static declaration when `net` is shadowed
func NewListenConfig(ctx context.Context, keepalive time.Duration) *ListenConfig {
if ctx == nil {
ctx = context.Background()
}
return &ListenConfig{
ListenConfig: net.ListenConfig{
KeepAlive: keepalive,
},
Context: ctx,
}
}
// Listen acts like the standard net.Listen but using the context.Context,
// KeepAlive, and optional Control function from our ListenConfig struct
func (lc ListenConfig) Listen(network, addr string) (net.Listener, error) {
ctx := lc.Context
if ctx == nil {
ctx = context.Background()
}
return lc.ListenConfig.Listen(ctx, network, addr)
}
// ListenPacket acts like the standard net.ListenPacket but using the context.Context,
// KeepAlive, and optional Control function from our ListenConfig struct
func (lc ListenConfig) ListenPacket(network, addr string) (net.PacketConn, error) {
ctx := lc.Context
if ctx == nil {
ctx = context.Background()
}
return lc.ListenConfig.ListenPacket(ctx, network, addr)
}
// ListenTCP acts like the standard net.ListenTCP but using the context.Context,
// KeepAlive, and optional Control function from our ListenConfig struct
func (lc ListenConfig) ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error) {
if laddr == nil {
laddr = &net.TCPAddr{}
}
ln, err := lc.Listen(network, laddr.String())
if err != nil {
return nil, err
}
return ln.(*net.TCPListener), nil
}
// ListenUDP acts like the standard net.ListenUDP but using the context.Context,
// KeepAlive, and optional Control function from our ListenConfig struct
func (lc ListenConfig) ListenUDP(network string, laddr *net.UDPAddr) (*net.UDPConn, error) {
if laddr == nil {
laddr = &net.UDPAddr{}
}
ln, err := lc.ListenPacket(network, laddr.String())
if err != nil {
return nil, err
}
return ln.(*net.UDPConn), nil
}
// ListenAll acts like Listen but on a list of addresses
func (lc ListenConfig) ListenAll(network string, addrs []string) ([]net.Listener, error) {
var ok bool
out := make([]net.Listener, 0, len(addrs))
// close all on error
defer func() {
if !ok {
closeAll(out)
}
}()
for _, addr := range addrs {
lsn, err := lc.Listen(network, addr)
if err != nil {
return nil, err
}
out = append(out, lsn)
}
ok = true
return out, nil
}
// ListenAllPacket acts like ListenPacket but on a list of addresses
func (lc ListenConfig) ListenAllPacket(network string, addrs []string) ([]net.PacketConn, error) {
var ok bool
out := make([]net.PacketConn, 0, len(addrs))
// close all on error
defer func() {
if !ok {
closeAll(out)
}
}()
for _, addr := range addrs {
lsn, err := lc.ListenPacket(network, addr)
if err != nil {
return nil, err
}
out = append(out, lsn)
}
ok = true
return out, nil
}
// ListenAllTCP acts like ListenTCP but on a list of addresses
func (lc ListenConfig) ListenAllTCP(network string, laddrs []*net.TCPAddr) (
[]*net.TCPListener, error) {
//
var ok bool
out := make([]*net.TCPListener, 0, len(laddrs))
// close all on error
defer func() {
if !ok {
closeAll(out)
}
}()
for _, addr := range laddrs {
lsn, err := lc.ListenTCP(network, addr)
if err != nil {
return nil, err
}
out = append(out, lsn)
}
ok = true
return out, nil
}
// ListenAllUDP acts like ListenUDP but on a list of addresses
func (lc ListenConfig) ListenAllUDP(network string, laddrs []*net.UDPAddr) ([]*net.UDPConn, error) {
var ok bool
out := make([]*net.UDPConn, 0, len(laddrs))
// close all on error
defer func() {
if !ok {
closeAll(out)
}
}()
for _, addr := range laddrs {
lsn, err := lc.ListenUDP(network, addr)
if err != nil {
return nil, err
}
out = append(out, lsn)
}
ok = true
return out, nil
}
func closeAll[T io.Closer](a []T) {
for _, v := range a {
_ = v.Close()
}
}