This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive_payload_queue.go
186 lines (162 loc) · 4.63 KB
/
receive_payload_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
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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sctp
import (
"fmt"
"math/bits"
)
type receivePayloadQueue struct {
tailTSN uint32
chunkSize int
tsnBitmask []uint64
dupTSN []uint32
maxTSNOffset uint32
cumulativeTSN uint32
}
func newReceivePayloadQueue(maxTSNOffset uint32) *receivePayloadQueue {
maxTSNOffset = ((maxTSNOffset + 63) / 64) * 64
return &receivePayloadQueue{
tsnBitmask: make([]uint64, maxTSNOffset/64),
maxTSNOffset: maxTSNOffset,
}
}
func (q *receivePayloadQueue) init(cumulativeTSN uint32) {
q.cumulativeTSN = cumulativeTSN
q.tailTSN = cumulativeTSN
q.chunkSize = 0
for i := range q.tsnBitmask {
q.tsnBitmask[i] = 0
}
q.dupTSN = q.dupTSN[:0]
}
func (q *receivePayloadQueue) hasChunk(tsn uint32) bool {
if q.chunkSize == 0 || sna32LTE(tsn, q.cumulativeTSN) || sna32GT(tsn, q.tailTSN) {
return false
}
index, offset := int(tsn/64)%len(q.tsnBitmask), tsn%64
return q.tsnBitmask[index]&(1<<offset) != 0
}
func (q *receivePayloadQueue) canPush(tsn uint32) bool {
ok := q.hasChunk(tsn)
if ok || sna32LTE(tsn, q.cumulativeTSN) || sna32GT(tsn, q.cumulativeTSN+q.maxTSNOffset) {
return false
}
return true
}
// push pushes a payload data. If the payload data is already in our queue or
// older than our cumulativeTSN marker, it will be recored as duplications,
// which can later be retrieved using popDuplicates.
func (q *receivePayloadQueue) push(tsn uint32) bool {
if sna32GT(tsn, q.cumulativeTSN+q.maxTSNOffset) {
return false
}
if sna32LTE(tsn, q.cumulativeTSN) || q.hasChunk(tsn) {
// Found the packet, log in dups
q.dupTSN = append(q.dupTSN, tsn)
return false
}
index, offset := int(tsn/64)%len(q.tsnBitmask), tsn%64
q.tsnBitmask[index] |= (1 << offset)
q.chunkSize++
if sna32GT(tsn, q.tailTSN) {
q.tailTSN = tsn
}
return true
}
// pop advances cumulativeTSN and pops the oldest chunk's TSN if it matches the given TSN or force is true.
func (q *receivePayloadQueue) pop(force bool) bool {
tsn := q.cumulativeTSN + 1
if q.hasChunk(tsn) {
index, offset := int(tsn/64)%len(q.tsnBitmask), int(tsn%64)
q.tsnBitmask[index] &= ^uint64(1 << (offset))
q.chunkSize--
q.cumulativeTSN++
return true
}
if force {
q.cumulativeTSN++
if q.chunkSize == 0 {
q.tailTSN = q.cumulativeTSN
}
}
return false
}
// popDuplicates returns an array of TSN values that were found duplicate.
func (q *receivePayloadQueue) popDuplicates() []uint32 {
dups := q.dupTSN
q.dupTSN = []uint32{}
return dups
}
func (q *receivePayloadQueue) getGapAckBlocks() (gapAckBlocks []gapAckBlock) {
var b gapAckBlock
if q.chunkSize == 0 {
return nil
}
startTSN, endTSN := q.cumulativeTSN+1, q.tailTSN
var findEnd bool
for tsn := startTSN; sna32LTE(tsn, endTSN); {
index, offset := int(tsn/64)%len(q.tsnBitmask), int(tsn%64)
if !findEnd {
// find first received tsn as start
if nonZeroBit, ok := getFirstNonZeroBit(q.tsnBitmask[index], offset, 64); ok {
b.start = uint16(tsn + uint32(nonZeroBit-offset) - q.cumulativeTSN)
tsn += uint32(nonZeroBit - offset)
findEnd = true
} else {
// no result, find start bits in next uint64 bitmask
tsn += uint32(64 - offset)
}
} else {
if zeroBit, ok := getFirstZeroBit(q.tsnBitmask[index], offset, 64); ok {
b.end = uint16(tsn + uint32(zeroBit-offset) - 1 - q.cumulativeTSN)
tsn += uint32(zeroBit - offset)
if sna32LTE(tsn, endTSN) {
gapAckBlocks = append(gapAckBlocks, gapAckBlock{
start: b.start,
end: b.end,
})
}
findEnd = false
} else {
tsn += uint32(64 - offset)
}
// no zero bit at the end, close and append the last gap
if sna32GT(tsn, endTSN) {
b.end = uint16(endTSN - q.cumulativeTSN)
gapAckBlocks = append(gapAckBlocks, gapAckBlock{
start: b.start,
end: b.end,
})
break
}
}
}
return gapAckBlocks
}
func (q *receivePayloadQueue) getGapAckBlocksString() string {
gapAckBlocks := q.getGapAckBlocks()
str := fmt.Sprintf("cumTSN=%d", q.cumulativeTSN)
for _, b := range gapAckBlocks {
str += fmt.Sprintf(",%d-%d", b.start, b.end)
}
return str
}
func (q *receivePayloadQueue) getLastTSNReceived() (uint32, bool) {
if q.chunkSize == 0 {
return 0, false
}
return q.tailTSN, true
}
func (q *receivePayloadQueue) getcumulativeTSN() uint32 {
return q.cumulativeTSN
}
func (q *receivePayloadQueue) size() int {
return q.chunkSize
}
func getFirstNonZeroBit(val uint64, start, end int) (int, bool) {
i := bits.TrailingZeros64(val >> uint64(start))
return i + start, i+start < end
}
func getFirstZeroBit(val uint64, start, end int) (int, bool) {
return getFirstNonZeroBit(^val, start, end)
}