/
rmnp.go
298 lines (227 loc) · 7.02 KB
/
rmnp.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2017 Tim Oster. All rights reserved.
// Use of this source code is governed by the MIT license.
// More information can be found in the LICENSE file.
package rmnp
import (
"context"
"net"
"sync"
"sync/atomic"
"time"
)
// ConnectionCallback is the function called when connections change
type ConnectionCallback func(*Connection, []byte)
// ValidationCallback is the function called to validate a connnection
type ValidationCallback func(*net.UDPAddr, []byte) bool
// PacketCallback is the function called when a packet is received
type PacketCallback func(*Connection, []byte, Channel)
func invokeConnectionCallback(callback ConnectionCallback, connection *Connection, packet []byte) {
if callback != nil {
callback(connection, packet)
}
}
func invokeValidationCallback(callback ValidationCallback, addr *net.UDPAddr, packet []byte) bool {
if callback != nil {
return callback(addr, packet)
}
return true
}
func invokePacketCallback(callback PacketCallback, connection *Connection, packet []byte, channel Channel) {
if callback != nil {
callback(connection, packet, channel)
}
}
// ReadFunc is the function called to write information to a udp connection
type ReadFunc func(*net.UDPConn, []byte) (int, *net.UDPAddr, bool)
// WriteFunc is the function called to read information from a udp connection
type WriteFunc func(*net.UDPConn, *net.UDPAddr, []byte)
type disconnectType byte
const (
disconnectTypeDefault disconnectType = iota
disconnectTypeShutdown
disconnectTypeTimeout
)
type protocolImpl struct {
address *net.UDPAddr
socket *net.UDPConn
ctx context.Context
cancel context.CancelFunc
waitGroup sync.WaitGroup
connectGuard *execGuard
connectionsMutex sync.RWMutex
connections map[uint32]*Connection
readFunc ReadFunc
writeFunc WriteFunc
bufferPool sync.Pool
connectionPool sync.Pool
// callbacks
// for clients: only executed if client is still connected. if client disconnects callback will not be executed.
onConnect ConnectionCallback
onDisconnect ConnectionCallback
onTimeout ConnectionCallback
onValidation ValidationCallback
onPacket PacketCallback
}
func (impl *protocolImpl) init(address string) {
addr, err := net.ResolveUDPAddr("udp", address)
checkError("Failed to resolve udp address", err)
impl.address = addr
impl.connectGuard = newExecGuard()
impl.connections = make(map[uint32]*Connection)
impl.bufferPool = sync.Pool{
New: func() interface{} { return make([]byte, CfgMTU) },
}
impl.connectionPool = sync.Pool{
New: func() interface{} { return newConnection() },
}
}
// is blocking call!
func (impl *protocolImpl) destroy() {
if impl.address == nil {
return
}
impl.connectionsMutex.Lock()
for _, conn := range impl.connections {
impl.disconnectClient(conn, disconnectTypeShutdown, nil)
}
impl.connectionsMutex.Unlock()
impl.cancel()
impl.waitGroup.Wait()
impl.socket.Close()
impl.address = nil
impl.socket = nil
impl.ctx = nil
impl.cancel = nil
impl.connectGuard = nil
impl.connections = nil
}
func (impl *protocolImpl) setSocket(socket *net.UDPConn, err error) {
checkError("Error creating socket", err)
impl.socket = socket
impl.socket.SetReadBuffer(CfgMTU)
impl.socket.SetWriteBuffer(CfgMTU)
}
func (impl *protocolImpl) listen() {
impl.ctx, impl.cancel = context.WithCancel(context.Background())
for i := 0; i < CfgParallelListenerCount; i++ {
go impl.listeningWorker()
}
}
func (impl *protocolImpl) listeningWorker() {
defer antiPanic(impl.listeningWorker)
impl.waitGroup.Add(1)
defer impl.waitGroup.Done()
atomic.AddUint64(&StatRunningGoRoutines, 1)
defer atomic.AddUint64(&StatRunningGoRoutines, ^uint64(0))
for {
select {
case <-impl.ctx.Done():
return
default:
}
func() {
defer antiPanic(nil)
buffer := impl.bufferPool.Get().([]byte)
defer impl.bufferPool.Put(buffer)
impl.socket.SetDeadline(time.Now().Add(time.Second))
length, addr, next := impl.readFunc(impl.socket, buffer)
if !next {
return
}
sizedBuffer := buffer[:length]
if !validateHeader(sizedBuffer) {
return
}
packet := make([]byte, length)
copy(packet, sizedBuffer)
atomic.AddUint64(&StatReceivedBytes, uint64(length))
impl.handlePacket(addr, packet)
}()
}
}
func (impl *protocolImpl) handlePacket(addr *net.UDPAddr, packet []byte) {
hash := addrHash(addr)
impl.connectionsMutex.RLock()
connection, exists := impl.connections[hash]
impl.connectionsMutex.RUnlock()
if !exists {
if descriptor(packet[5])&descConnect == 0 {
return
}
if !impl.connectGuard.tryExecute(hash) {
return
}
header := headerSize(packet)
if !invokeValidationCallback(impl.onValidation, addr, packet[header:]) {
atomic.AddUint64(&StatDeniedConnects, 1)
return
}
connection = impl.connectClient(addr, nil)
}
// done this way to ensure that connect callback is executed on client-side
if descriptor(packet[5])&descConnect != 0 {
if connection.updateState(stateConnected) {
header := headerSize(packet)
// clear buffers so all remaining connection packets are deleted
if connection.IsServer {
connection.sendBuffer.reset()
connection.sendQueue.clear()
}
invokeConnectionCallback(impl.onConnect, connection, packet[header:])
impl.connectGuard.finish(hash)
}
return
}
if descriptor(packet[5])&descDisconnect != 0 {
header := headerSize(packet)
impl.disconnectClient(connection, disconnectTypeDefault, packet[header:])
return
}
atomic.AddUint64(&StatProcessedBytes, uint64(len(packet)))
connection.receiveQueue.push(packet)
}
func (impl *protocolImpl) connectClient(addr *net.UDPAddr, data []byte) *Connection {
atomic.AddUint64(&StatConnects, 1)
hash := addrHash(addr)
connection := impl.connectionPool.Get().(*Connection)
connection.init(impl, addr)
impl.connectionsMutex.Lock()
impl.connections[hash] = connection
impl.connectionsMutex.Unlock()
if data != nil {
connection.sendHighLevelPacket(descReliable|descConnect, data)
} else {
connection.sendLowLevelPacket(descReliable | descConnect)
}
connection.startRoutines()
return connection
}
func (impl *protocolImpl) disconnectClient(connection *Connection, disconnectType disconnectType, packet []byte) {
if !connection.updateState(stateDisconnected) {
return
}
if disconnectType == disconnectTypeTimeout {
atomic.AddUint64(&StatTimeouts, 1)
invokeConnectionCallback(impl.onTimeout, connection, nil)
}
atomic.AddUint64(&StatDisconnects, 1)
// send more than necessary so that the packet hopefully arrives
for i := 0; i < 10; i++ {
connection.sendHighLevelPacket(descDisconnect, packet)
}
// give the channel some time to process the packets
time.Sleep(20 * time.Millisecond)
connection.stopRoutines()
connection.waitGroup.Wait()
if disconnectType != disconnectTypeShutdown {
if connection.Addr != nil {
hash := addrHash(connection.Addr)
impl.connectionsMutex.Lock()
delete(impl.connections, hash)
impl.connectionsMutex.Unlock()
}
invokeConnectionCallback(impl.onDisconnect, connection, packet)
}
connection.reset()
impl.connectionPool.Put(connection)
}