-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
net.go
47 lines (37 loc) · 802 Bytes
/
net.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
package proxy
import (
"errors"
"net"
)
var ErrAlreadyAccepted = errors.New("listener already accepted")
// OnceListener implements net.Listener.
//
// Accepts a connection once and returns an error on subsequent
// attempts.
type OnceAcceptListener struct {
c net.Conn
}
func (l *OnceAcceptListener) Accept() (net.Conn, error) {
if l.c == nil {
return nil, ErrAlreadyAccepted
}
c := l.c
l.c = nil
return c, nil
}
func (l *OnceAcceptListener) Close() error {
return nil
}
func (l *OnceAcceptListener) Addr() net.Addr {
return l.c.LocalAddr()
}
// ConnNotify embeds net.Conn and adds a channel field for notifying
// that the connection was closed.
type ConnNotify struct {
net.Conn
closed chan struct{}
}
func (c *ConnNotify) Close() {
c.Conn.Close()
c.closed <- struct{}{}
}