-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciphertext_queue.go
More file actions
59 lines (53 loc) · 1.1 KB
/
Copy pathciphertext_queue.go
File metadata and controls
59 lines (53 loc) · 1.1 KB
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
package tls
import "sync"
// ciphertextQueue 在握手协程与 Channel owner EventLoop 之间转移密文所有权。
type ciphertextQueue struct {
mu sync.Mutex
chunks []byteChunk
head int
closed bool
}
func (q *ciphertextQueue) push(chunk byteChunk) bool {
q.mu.Lock()
if q.closed {
q.mu.Unlock()
return false
}
q.chunks = append(q.chunks, chunk)
q.mu.Unlock()
return true
}
func (q *ciphertextQueue) pop() (chunk byteChunk, remaining bool, ok bool) {
q.mu.Lock()
defer q.mu.Unlock()
if q.head == len(q.chunks) {
return byteChunk{}, false, false
}
chunk = q.chunks[q.head]
q.chunks[q.head] = byteChunk{}
q.head++
remaining = q.head != len(q.chunks)
if q.head == len(q.chunks) {
q.chunks = q.chunks[:0]
q.head = 0
}
return chunk, remaining, true
}
func (q *ciphertextQueue) len() int {
q.mu.Lock()
n := len(q.chunks) - q.head
q.mu.Unlock()
return n
}
func (q *ciphertextQueue) closeAndRelease() {
q.mu.Lock()
chunks := q.chunks
head := q.head
q.closed = true
q.chunks = nil
q.head = 0
q.mu.Unlock()
for i := head; i < len(chunks); i++ {
chunks[i].releaseOwned()
}
}