-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
68 lines (57 loc) · 1.54 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package exnet
import (
"net"
)
// Listener listen and return a exnet.Conn
type Listener struct {
_l net.Listener
_acceptCallback func(net.Conn) error
}
var _ net.Listener = &Listener{}
// WithListener return an exnet.Listener with an underlying net.Listener,
// if l is already an exnet.Listener, return its-self.
func WithListener(l net.Listener) *Listener {
if el, ok := l.(*Listener); ok {
return el
}
return &Listener{_l: l}
}
// Listen works like net.Listen, but return an exnet.Listener
func Listen(network, addr string) (*Listener, error) {
l, err := net.Listen(network, addr)
if err != nil {
return nil, err
}
return &Listener{_l: l}, nil
}
// Underlying return underlying net.Listener
func (l *Listener) Underlying() net.Listener {
return l._l
}
// SetAcceptCallback add an accept callback to every new connection
// when it's accepted
func (l *Listener) SetAcceptCallback(f func(net.Conn) error) {
l._acceptCallback = f
}
// Accept new connections, return an exnet.Conn
// if SetAcceptCallback called, accpet callback will call on new
// connection, if callback return error, accpet return error.
func (l *Listener) Accept() (net.Conn, error) {
rwc, err := l._l.Accept()
if err != nil {
return nil, err
}
c := &Conn{_conn: rwc}
if l._acceptCallback != nil {
err = l._acceptCallback(c)
if err != nil {
c.Close()
return nil, err
}
}
return c, nil
}
// Close underlying listener
func (l *Listener) Close() error { return l._l.Close() }
// Addr return underlying addr
func (l *Listener) Addr() net.Addr { return l._l.Addr() }