-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
112 lines (100 loc) · 2.75 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
109
110
111
112
package spectral
import (
"context"
"errors"
"net"
"slices"
"sync"
"time"
"github.com/cooldogedev/spectral/internal/frame"
"github.com/cooldogedev/spectral/internal/protocol"
)
type Listener struct {
conn *udpConn
connections map[protocol.ConnectionID]*ServerConnection
connectionsMu sync.Mutex
connectionID protocol.ConnectionID
incomingConnections chan *ServerConnection
ctx context.Context
cancelFunc context.CancelFunc
once sync.Once
}
func newListener(conn *udpConn) *Listener {
listener := &Listener{
conn: conn,
connections: make(map[protocol.ConnectionID]*ServerConnection),
incomingConnections: make(chan *ServerConnection, 100),
}
listener.ctx, listener.cancelFunc = context.WithCancel(context.Background())
go conn.Read(func(dgram *datagram) (err error) {
defer dgram.reset()
connectionID, sequenceID, frames, err := frame.Unpack(dgram.b)
if err != nil {
return nil
}
listener.connectionsMu.Lock()
defer listener.connectionsMu.Unlock()
c, ok := listener.connections[connectionID]
if !ok && slices.ContainsFunc(frames, func(fr frame.Frame) bool { return fr.ID() == frame.IDConnectionRequest }) {
c = newServerConnection(conn, dgram.peerAddr, listener.connectionID, listener.ctx)
c.logger.Log("connection_accepted", "addr", dgram.peerAddr.String())
listener.connections[listener.connectionID] = c
listener.connectionID++
listener.incomingConnections <- c
go func() {
<-c.ctx.Done()
listener.connectionsMu.Lock()
delete(listener.connections, protocol.ConnectionID(c.connectionID.Load()))
listener.connectionsMu.Unlock()
}()
}
if c == nil {
return
}
select {
case <-listener.ctx.Done():
return context.Cause(listener.ctx)
case <-c.ctx.Done():
default:
c.packets <- &receivedPacket{sequenceID, frames, time.Now()}
}
return
})
return listener
}
func Listen(address string) (*Listener, error) {
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return nil, err
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
return nil, err
}
c, err := newUDPConn(conn, true)
if err != nil {
return nil, err
}
return newListener(c), nil
}
func (l *Listener) Accept(ctx context.Context) (Connection, error) {
select {
case <-l.ctx.Done():
return nil, errors.New("listener closed")
case <-ctx.Done():
return nil, context.Cause(ctx)
case conn := <-l.incomingConnections:
return conn, nil
}
}
func (l *Listener) Close() (err error) {
l.once.Do(func() {
for i, conn := range l.connections {
_ = conn.CloseWithError(frame.ConnectionCloseGraceful, "closed listener")
delete(l.connections, i)
}
l.cancelFunc()
_ = l.conn.Close()
})
return
}