-
Notifications
You must be signed in to change notification settings - Fork 19
/
multiplexer.go
177 lines (161 loc) · 4.83 KB
/
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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package udt
import (
"bytes"
"github.com/oxtoacart/bpool"
"log"
"net"
"sync"
"time"
)
/*
A multiplexer multiplexes multiple UDT sockets over a single PacketConn.
*/
type multiplexer struct {
laddr *net.UDPAddr // the local address handled by this multiplexer
conn *net.UDPConn // the UDPConn from which we read/write
mode uint8 // client or server
sockets map[uint32]*udtSocket // the server udtSockets handled by this multiplexer, by sockId
socketsMutex *sync.Mutex
sendQ *udtSocketQueue // priority queue of udtSockets awaiting a send (actually includes ones with no packets waiting too)
ctrlOut chan packet // control packets meant queued for sending
in chan packetHolder // packets inbound from the PacketConn
out chan packet // packets outbound to the PacketConn
writeBufferPool *bpool.BufferPool // leaky buffer pool for writing to conn
readBytePool *bpool.BytePool // leaky byte pool for reading from conn
}
type packetHolder struct {
packet packet
from *net.UDPAddr
}
/*
multiplexerFor gets or creates a multiplexer for the given local address. If a
new multiplexer is created, the given init function is run to obtain an
io.ReadWriter.
*/
func multiplexerFor(laddr *net.UDPAddr, init func() (*net.UDPConn, error)) (m *multiplexer, err error) {
multiplexersMutex.Lock()
defer multiplexersMutex.Unlock()
key := laddr.String()
m = multiplexers[key]
if m == nil {
// No multiplexer, need to create connection
var conn *net.UDPConn
if conn, err = init(); err == nil {
m = newMultiplexer(laddr, conn)
multiplexers[key] = m
}
}
return
}
func newMultiplexer(laddr *net.UDPAddr, conn *net.UDPConn) (m *multiplexer) {
m = &multiplexer{
laddr: laddr,
conn: conn,
sockets: make(map[uint32]*udtSocket),
socketsMutex: new(sync.Mutex),
sendQ: newUdtSocketQueue(),
ctrlOut: make(chan packet, 100), // todo: figure out how to size this
in: make(chan packetHolder, 100), // todo: make this tunable
out: make(chan packet, 100), // todo: make this tunable
writeBufferPool: bpool.NewBufferPool(25600), // todo: make this tunable
readBytePool: bpool.NewBytePool(25600, max_packet_size), // todo: make this tunable
}
go m.coordinate()
go m.read()
go m.write()
return
}
func (m *multiplexer) newClientSocket() (s *udtSocket, err error) {
m.socketsMutex.Lock()
defer m.socketsMutex.Unlock()
sids -= 1
sid := sids
if s, err = newClientSocket(m, sid); err == nil {
m.sockets[sid] = s
}
for {
s.initHandshake()
time.Sleep(200 * time.Millisecond)
}
return
}
/*
read runs in a goroutine and reads packets from conn using a buffer from the
readBufferPool, or a new buffer.
*/
func (m *multiplexer) read() {
for {
b := m.readBytePool.Get()
defer m.readBytePool.Put(b)
if _, from, err := m.conn.ReadFromUDP(b); err != nil {
log.Printf("Unable to read into buffer: %s", err)
} else {
r := bytes.NewReader(b)
if p, err := readPacketFrom(r); err != nil {
log.Printf("Unable to read packet: %s", err)
} else {
m.in <- packetHolder{p, from}
}
}
}
}
/*
write runs in a goroutine and writes packets to conn using a buffer from the
writeBufferPool, or a new buffer.
*/
func (m *multiplexer) write() {
for {
select {
case p := <-m.ctrlOut:
b := m.writeBufferPool.Get()
defer m.writeBufferPool.Put(b)
if err := p.writeTo(b); err != nil {
// TODO: handle write error
log.Fatalf("Unable to buffer out: %s", err)
} else {
log.Printf("Writing to %s", p.socketId())
if _, err := b.WriteTo(m.sockets[p.socketId()].boundWriter); err != nil {
// TODO: handle write error
log.Fatalf("Unable to write out: %s", err)
}
}
}
}
}
// coordinate runs in a goroutine and coordinates all of the multiplexer's work
func (m *multiplexer) coordinate() {
for {
select {
case p := <-m.in:
m.handleInbound(p)
}
}
}
func (m *multiplexer) handleInbound(ph packetHolder) {
switch p := ph.packet.(type) {
case *handshakePacket:
// Only process packet if version and type are supported
log.Println("Got handshake packet")
if p.udtVer == 4 && p.sockType == STREAM {
log.Println("Right version and type")
s := m.sockets[p.sockId]
if p.sockType == init_client_handshake {
if s == nil {
// create a new udt socket and remember it
var err error
if s, err = newServerSocket(m, ph.from, p); err == nil {
m.sockets[p.sockId] = s
log.Println("Responding to handshake")
s.respondInitHandshake()
}
}
}
if s == nil {
// s may still be nil if we couldn't create a new socket
// in this case, we ignore the error
} else {
// Okay, we have a socket
}
}
}
}