-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_data.go
111 lines (96 loc) · 1.93 KB
/
message_data.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
package sockety
import (
"errors"
"github.com/sockety/sockety-go/internal/buffer_pool"
"github.com/valyala/bytebufferpool"
"io"
"sync"
"sync/atomic"
)
// TODO: Consider maximum buffer size, so 'push' could have backpressure?
type messageData struct {
buf []*bytebufferpool.ByteBuffer
bufOffset uint64
wrote uint64
size uint64
mu sync.Mutex
err error
ch chan struct{}
}
type MessageData interface {
io.Reader
Received() uint64
Size() uint64
Done() bool
}
func newMessageData(size uint64) *messageData {
return &messageData{
size: size,
ch: make(chan struct{}),
}
}
func (m *messageData) push(p *bytebufferpool.ByteBuffer) error {
m.mu.Lock()
wrote := m.wrote + uint64(p.Len())
if wrote > m.size {
m.err = errors.New("malformed size")
close(m.ch)
m.mu.Unlock()
return m.err
}
m.buf = append(m.buf, p)
atomic.SwapUint64(&m.wrote, wrote)
if m.wrote == m.size {
close(m.ch)
} else {
putOptional(m.ch, struct{}{})
}
m.mu.Unlock()
return nil
}
func (m *messageData) Read(p []byte) (int, error) {
size := uint64(len(p))
for {
m.mu.Lock()
if m.err != nil {
err := m.err
m.mu.Unlock()
return 0, err
} else if len(m.buf) == 0 {
if m.size == m.wrote {
m.mu.Unlock()
return 0, io.EOF
}
go func() {
m.mu.Unlock()
}()
<-m.ch
continue
}
buf := m.buf[0]
if uint64(len(buf.B))-m.bufOffset > size {
copy(p, buf.B[m.bufOffset:])
m.bufOffset += size
m.mu.Unlock()
return len(p), nil
} else {
copy(p, buf.B[m.bufOffset:])
bytes := uint64(len(buf.B)) - m.bufOffset
m.bufOffset = 0
m.buf = m.buf[1:]
m.mu.Unlock()
buffer_pool.Release(buf)
return int(bytes), nil
}
}
}
func (m *messageData) Size() uint64 {
return m.size
}
func (m *messageData) Received() uint64 {
return atomic.LoadUint64(&m.wrote)
}
func (m *messageData) Done() bool {
return m.Received() == m.size
}
var emptyMessageData = newMessageData(0)