forked from xtaci/kcp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.go
50 lines (41 loc) · 773 Bytes
/
emitter.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
package kcp
import (
"net"
"sync/atomic"
)
var defaultEmitter Emitter
const emitQueue = 8192
func init() {
defaultEmitter.init()
}
type (
emitPacket struct {
conn net.PacketConn
to net.Addr
data []byte
recycle bool
}
// Emitter is the global packet sender
Emitter struct {
ch chan emitPacket
}
)
func (e *Emitter) init() {
e.ch = make(chan emitPacket, emitQueue)
go e.emitTask()
}
// keepon writing packets to kernel
func (e *Emitter) emitTask() {
for p := range e.ch {
if n, err := p.conn.WriteTo(p.data, p.to); err == nil {
atomic.AddUint64(&DefaultSnmp.OutSegs, 1)
atomic.AddUint64(&DefaultSnmp.OutBytes, uint64(n))
}
if p.recycle {
xmitBuf.Put(p.data)
}
}
}
func (e *Emitter) emit(p emitPacket) {
e.ch <- p
}