forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_multiplexer.go
96 lines (84 loc) · 2.36 KB
/
client_multiplexer.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
package quic
import (
"bytes"
"net"
"strings"
"sync"
"time"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
"github.com/lucas-clemente/quic-go/internal/wire"
)
var (
clientMuxerOnce sync.Once
clientMuxer *clientMultiplexer
)
// The clientMultiplexer listens on multiple net.PacketConns and dispatches
// incoming packets to the session handler.
type clientMultiplexer struct {
mutex sync.Mutex
conns map[net.PacketConn]packetHandlerManager
logger utils.Logger
}
func getClientMultiplexer() *clientMultiplexer {
clientMuxerOnce.Do(func() {
clientMuxer = &clientMultiplexer{
conns: make(map[net.PacketConn]packetHandlerManager),
logger: utils.DefaultLogger.WithPrefix("client muxer"),
}
})
return clientMuxer
}
func (m *clientMultiplexer) Add(c net.PacketConn, connID protocol.ConnectionID, handler packetHandler) {
m.mutex.Lock()
defer m.mutex.Unlock()
sessions, ok := m.conns[c]
if !ok {
sessions = newPacketHandlerMap()
m.conns[c] = sessions
}
sessions.Add(connID, handler)
if ok {
return
}
// If we didn't know this packet conn before, listen for incoming packets
// and dispatch them to the right sessions.
go m.listen(c, sessions)
}
func (m *clientMultiplexer) listen(c net.PacketConn, sessions packetHandlerManager) {
for {
data := *getPacketBuffer()
data = data[:protocol.MaxReceivePacketSize]
// The packet size should not exceed protocol.MaxReceivePacketSize bytes
// If it does, we only read a truncated packet, which will then end up undecryptable
n, addr, err := c.ReadFrom(data)
if err != nil {
if !strings.HasSuffix(err.Error(), "use of closed network connection") {
sessions.Close(err)
}
return
}
data = data[:n]
rcvTime := time.Now()
r := bytes.NewReader(data)
hdr, err := wire.ParseHeaderSentByServer(r)
// drop the packet if we can't parse the header
if err != nil {
m.logger.Debugf("error parsing packet from %s: %s", addr, err)
continue
}
hdr.Raw = data[:len(data)-r.Len()]
packetData := data[len(data)-r.Len():]
client, ok := sessions.Get(hdr.DestConnectionID)
if !ok {
m.logger.Debugf("received a packet with an unexpected connection ID %s", hdr.DestConnectionID)
continue
}
client.handlePacket(&receivedPacket{
remoteAddr: addr,
header: hdr,
data: packetData,
rcvTime: rcvTime,
})
}
}