-
Notifications
You must be signed in to change notification settings - Fork 79
/
pending_queue.go
142 lines (123 loc) · 2.89 KB
/
pending_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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sctp
import (
"errors"
)
// pendingBaseQueue
type pendingBaseQueue struct {
queue []*chunkPayloadData
}
func newPendingBaseQueue() *pendingBaseQueue {
return &pendingBaseQueue{queue: []*chunkPayloadData{}}
}
func (q *pendingBaseQueue) push(c *chunkPayloadData) {
q.queue = append(q.queue, c)
}
func (q *pendingBaseQueue) pop() *chunkPayloadData {
if len(q.queue) == 0 {
return nil
}
c := q.queue[0]
q.queue = q.queue[1:]
return c
}
func (q *pendingBaseQueue) get(i int) *chunkPayloadData {
if len(q.queue) == 0 || i < 0 || i >= len(q.queue) {
return nil
}
return q.queue[i]
}
func (q *pendingBaseQueue) size() int {
return len(q.queue)
}
// pendingQueue
type pendingQueue struct {
unorderedQueue *pendingBaseQueue
orderedQueue *pendingBaseQueue
nBytes int
selected bool
unorderedIsSelected bool
}
// Pending queue errors
var (
ErrUnexpectedChuckPoppedUnordered = errors.New("unexpected chunk popped (unordered)")
ErrUnexpectedChuckPoppedOrdered = errors.New("unexpected chunk popped (ordered)")
ErrUnexpectedQState = errors.New("unexpected q state (should've been selected)")
)
func newPendingQueue() *pendingQueue {
return &pendingQueue{
unorderedQueue: newPendingBaseQueue(),
orderedQueue: newPendingBaseQueue(),
}
}
func (q *pendingQueue) push(c *chunkPayloadData) {
if c.unordered {
q.unorderedQueue.push(c)
} else {
q.orderedQueue.push(c)
}
q.nBytes += len(c.userData)
}
func (q *pendingQueue) peek() *chunkPayloadData {
if q.selected {
if q.unorderedIsSelected {
return q.unorderedQueue.get(0)
}
return q.orderedQueue.get(0)
}
if c := q.unorderedQueue.get(0); c != nil {
return c
}
return q.orderedQueue.get(0)
}
func (q *pendingQueue) pop(c *chunkPayloadData) error {
if q.selected {
var popped *chunkPayloadData
if q.unorderedIsSelected {
popped = q.unorderedQueue.pop()
if popped != c {
return ErrUnexpectedChuckPoppedUnordered
}
} else {
popped = q.orderedQueue.pop()
if popped != c {
return ErrUnexpectedChuckPoppedOrdered
}
}
if popped.endingFragment {
q.selected = false
}
} else {
if !c.beginningFragment {
return ErrUnexpectedQState
}
if c.unordered {
popped := q.unorderedQueue.pop()
if popped != c {
return ErrUnexpectedChuckPoppedUnordered
}
if !popped.endingFragment {
q.selected = true
q.unorderedIsSelected = true
}
} else {
popped := q.orderedQueue.pop()
if popped != c {
return ErrUnexpectedChuckPoppedOrdered
}
if !popped.endingFragment {
q.selected = true
q.unorderedIsSelected = false
}
}
}
q.nBytes -= len(c.userData)
return nil
}
func (q *pendingQueue) getNumBytes() int {
return q.nBytes
}
func (q *pendingQueue) size() int {
return q.unorderedQueue.size() + q.orderedQueue.size()
}