-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_queue.go
154 lines (139 loc) · 2.82 KB
/
send_queue.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
package l2
import (
"bytes"
"encoding/binary"
"net"
"time"
)
type sendQueue struct {
initCountDown int
timerDuration time.Duration
timer *time.Timer
timerStarted bool
m map[queueKey]*queueValue
sendFunc queueSendFunc
writeOutbound WriteOutbound
mtu int
}
type queueKey struct {
IPLen int
IP [16]byte
HasAddr bool
Addr [6]byte
}
type queueValue struct {
countDown int
length int
datas [][]byte
}
type queueSendFunc func(*net.IP, *net.HardwareAddr, []byte)
type NewSendQueue func(
sendFunc queueSendFunc,
) *sendQueue
func (Network) NewSendQueue(
writeOutbound WriteOutbound,
mtu MTU,
) NewSendQueue {
return func(
sendFunc queueSendFunc,
) *sendQueue {
return &sendQueue{
initCountDown: 2,
timerDuration: time.Microsecond * 2000,
timer: time.NewTimer(time.Microsecond * 2000),
timerStarted: true,
m: make(map[queueKey]*queueValue),
sendFunc: sendFunc,
writeOutbound: writeOutbound,
mtu: int(mtu),
}
}
}
func (q *sendQueue) send(
key queueKey,
) {
value := q.m[key]
delete(q.m, key)
buf := new(bytes.Buffer)
for _, data := range value.datas {
ce(binary.Write(buf, binary.LittleEndian, uint16(len(data))))
_, err := buf.Write(data)
ce(err)
}
var ipPtr *net.IP
if key.IPLen > 0 {
ip := net.IP(key.IP[:key.IPLen])
ipPtr = &ip
}
var addrPtr *net.HardwareAddr
if key.HasAddr {
addr := net.HardwareAddr(key.Addr[:])
addrPtr = &addr
}
q.sendFunc(ipPtr, addrPtr, buf.Bytes())
}
func (q *sendQueue) enqueue(
outbound *Outbound,
) {
var key queueKey
if outbound.DestIP != nil {
key.IPLen = len(*outbound.DestIP)
copy(key.IP[:], *outbound.DestIP)
}
if outbound.DestAddr != nil {
key.HasAddr = true
copy(key.Addr[:], *outbound.DestAddr)
}
buf := new(bytes.Buffer)
if err := q.writeOutbound(buf, outbound); err != nil {
panic(err)
}
data := buf.Bytes()
inQueue, ok := q.m[key]
if ok {
if inQueue.length+len(data)+2 > int(q.mtu) {
q.send(key)
q.m[key] = &queueValue{
countDown: q.initCountDown,
length: len(data) + 2, // include uint16 length
datas: [][]byte{data},
}
} else {
inQueue.length += len(data) + 2
inQueue.datas = append(inQueue.datas, data)
}
} else {
q.m[key] = &queueValue{
countDown: q.initCountDown,
length: len(data) + 2,
datas: [][]byte{data},
}
}
if q.initCountDown <= 0 {
q.send(key)
}
if len(q.m) > 0 && !q.timerStarted {
if !q.timer.Stop() {
select {
case <-q.timer.C:
default:
}
}
q.timer.Reset(q.timerDuration)
q.timerStarted = true
}
}
func (q *sendQueue) tick() {
for key, value := range q.m {
value.countDown--
if value.countDown <= 0 {
q.send(key)
}
}
if len(q.m) > 0 {
q.timer.Reset(q.timerDuration)
q.timerStarted = true
} else {
q.timerStarted = false
}
}