-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
121 lines (107 loc) · 2.94 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
113
114
115
116
117
118
119
120
121
package spectral
import (
"context"
"errors"
"fmt"
"net"
"slices"
"sync"
"github.com/cooldogedev/spectral/internal"
"github.com/cooldogedev/spectral/internal/frame"
"github.com/cooldogedev/spectral/internal/protocol"
)
type Listener struct {
conn *net.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 *net.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 func() {
defer listener.Close()
p := make([]byte, 1500)
for {
n, addr, err := conn.ReadFromUDP(p)
if err != nil {
return
}
connectionID, sequenceID, frames, err := frame.Unpack(p[:n])
if err != nil {
continue
}
listener.connectionsMu.Lock()
c, ok := listener.connections[connectionID]
if !ok && slices.ContainsFunc(frames, func(fr frame.Frame) bool { return fr.ID() == frame.IDConnectionRequest }) {
c = newServerConnection(internal.NewConn(conn, addr, false), listener.connectionID, listener.ctx)
listener.connections[listener.connectionID] = c
listener.connectionID++
listener.incomingConnections <- c
go func() {
<-c.ctx.Done()
listener.connectionsMu.Lock()
delete(listener.connections, c.connectionID)
listener.connectionsMu.Unlock()
}()
}
listener.connectionsMu.Unlock()
if c == nil {
continue
}
if err := c.receive(sequenceID, frames); err != nil {
_ = c.CloseWithError(frame.ConnectionCloseInternal, fmt.Sprintf("error while handling frame: %v", err.Error()))
continue
}
}
}()
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
}
if err := conn.SetReadBuffer(protocol.ReceiveBufferSize); err != nil {
_ = conn.Close()
return nil, err
}
if err := conn.SetWriteBuffer(protocol.SendBufferSize); err != nil {
_ = conn.Close()
return nil, err
}
return newListener(conn), 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, ctx.Err()
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
}