-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
108 lines (98 loc) · 2.04 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
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
package packener
import (
"errors"
"net"
"sync"
"github.com/poohvpn/po"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var QueueSize = 64
type Listener struct {
conn net.PacketConn
connCh chan *datagramConn
sessions sync.Map // string -> *datagramConn
closeOnce po.Once
}
func New(conn net.PacketConn) *Listener {
l := &Listener{
conn: conn,
connCh: make(chan *datagramConn, QueueSize),
}
go l.run()
return l
}
func (l *Listener) run() {
buf := make([]byte, po.BufferSize)
for {
n, addr, err := l.conn.ReadFrom(buf)
if err != nil {
if errors.Is(err, net.ErrClosed) {
return
}
panic(err)
}
index := addr.String()
v, exist := l.sessions.Load(index)
if !exist {
log.Debug().Func(func(e *zerolog.Event) {
e.Str("index", index).Str("network", addr.Network()).Msg("packener: New Session")
})
v, exist = l.sessions.LoadOrStore(index, &datagramConn{
localAddr: l.Addr(),
remoteAddr: addr,
packetCh: make(chan []byte, QueueSize),
writeFunc: l.conn.WriteTo,
readDeadline: makePipeDeadline(),
superDelete: func(index string) func() {
return func() {
l.sessions.Delete(index)
}
}(index),
})
}
conn := v.(*datagramConn)
if !exist {
select {
case l.connCh <- conn:
case <-l.closeOnce.Wait():
return
}
}
select {
case conn.packetCh <- po.Duplicate(buf[:n]):
case <-conn.closeOnce.Wait():
case <-l.closeOnce.Wait():
return
}
}
}
func (l *Listener) Accept() (net.Conn, error) {
for {
select {
case <-l.closeOnce.Wait():
return nil, net.ErrClosed
case conn := <-l.connCh:
return conn, nil
}
}
}
func (l *Listener) Close() error {
return l.closeOnce.ErrorDo(func() error {
err := po.Close(l.conn)
l.sessions.Range(func(_, v interface{}) bool {
conn := v.(*datagramConn)
_ = conn.Close()
return true
})
return err
})
}
func (l *Listener) Addr() net.Addr {
return l.conn.LocalAddr()
}
func (l *Listener) closeConnCallback(index string) func() {
return func() {
l.sessions.Delete(index)
}
}