-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstream.go
251 lines (208 loc) · 4.36 KB
/
stream.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package multiplex
import (
"context"
"errors"
"io"
"sync"
"time"
pool "github.com/libp2p/go-buffer-pool"
streammux "github.com/libp2p/go-stream-muxer"
)
// streamID is a convenience type for operating on stream IDs
type streamID struct {
id uint64
initiator bool
}
// header computes the header for the given tag
func (id *streamID) header(tag uint64) uint64 {
header := id.id<<3 | tag
if !id.initiator {
header--
}
return header
}
type Stream struct {
id streamID
name string
dataIn chan []byte
mp *Multiplex
extra []byte
// exbuf is for holding the reference to the beginning of the extra slice
// for later memory pool freeing
exbuf []byte
deadlineLock sync.Mutex
wDeadline time.Time
rDeadline time.Time
clLock sync.Mutex
closedRemote bool
// Closed when the connection is reset.
reset chan struct{}
// Closed when the writer is closed (reset will also be closed)
closedLocal context.Context
doCloseLocal context.CancelFunc
}
func (s *Stream) Name() string {
return s.name
}
func (s *Stream) waitForData(ctx context.Context) error {
s.deadlineLock.Lock()
if !s.rDeadline.IsZero() {
dctx, cancel := context.WithDeadline(ctx, s.rDeadline)
defer cancel()
ctx = dctx
}
s.deadlineLock.Unlock()
select {
case <-s.reset:
// This is the only place where it's safe to return these.
s.returnBuffers()
return streammux.ErrReset
case read, ok := <-s.dataIn:
if !ok {
return io.EOF
}
s.extra = read
s.exbuf = read
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (s *Stream) returnBuffers() {
if s.exbuf != nil {
pool.Put(s.exbuf)
s.exbuf = nil
s.extra = nil
}
for {
select {
case read, ok := <-s.dataIn:
if !ok {
return
}
if read == nil {
continue
}
pool.Put(read)
default:
return
}
}
}
func (s *Stream) Read(b []byte) (int, error) {
if s.extra == nil {
err := s.waitForData(context.Background())
if err != nil {
return 0, err
}
}
n := copy(b, s.extra)
if n < len(s.extra) {
s.extra = s.extra[n:]
} else {
if s.exbuf != nil {
pool.Put(s.exbuf)
}
s.extra = nil
s.exbuf = nil
}
return n, nil
}
func (s *Stream) Write(b []byte) (int, error) {
var written int
for written < len(b) {
wl := len(b) - written
if wl > MaxMessageSize {
wl = MaxMessageSize
}
n, err := s.write(b[written : written+wl])
if err != nil {
return written, err
}
written += n
}
return written, nil
}
func (s *Stream) write(b []byte) (int, error) {
if s.isClosed() {
return 0, errors.New("cannot write to closed stream")
}
s.deadlineLock.Lock()
wDeadlineCtx, cleanup := func(s *Stream) (context.Context, context.CancelFunc) {
if s.wDeadline.IsZero() {
return s.closedLocal, nil
} else {
return context.WithDeadline(s.closedLocal, s.wDeadline)
}
}(s)
s.deadlineLock.Unlock()
err := s.mp.sendMsg(wDeadlineCtx, s.id.header(messageTag), b)
if cleanup != nil {
cleanup()
}
if err != nil {
if err == context.Canceled {
err = errors.New("cannot write to closed stream")
}
return 0, err
}
return len(b), nil
}
func (s *Stream) isClosed() bool {
return s.closedLocal.Err() != nil
}
func (s *Stream) Close() error {
err := s.mp.sendMsg(context.Background(), s.id.header(closeTag), nil)
if s.isClosed() {
return nil
}
s.clLock.Lock()
remote := s.closedRemote
s.clLock.Unlock()
s.doCloseLocal()
if remote {
s.mp.chLock.Lock()
delete(s.mp.channels, s.id)
s.mp.chLock.Unlock()
}
return err
}
func (s *Stream) Reset() error {
s.clLock.Lock()
isClosed := s.isClosed()
if s.closedRemote && isClosed {
s.clLock.Unlock()
return nil
}
if !s.closedRemote {
close(s.reset)
// We generally call this to tell the other side to go away. No point in waiting around.
go s.mp.sendMsg(context.Background(), s.id.header(resetTag), nil)
}
s.doCloseLocal()
s.closedRemote = true
s.clLock.Unlock()
s.mp.chLock.Lock()
delete(s.mp.channels, s.id)
s.mp.chLock.Unlock()
return nil
}
func (s *Stream) SetDeadline(t time.Time) error {
s.deadlineLock.Lock()
defer s.deadlineLock.Unlock()
s.rDeadline = t
s.wDeadline = t
return nil
}
func (s *Stream) SetReadDeadline(t time.Time) error {
s.deadlineLock.Lock()
defer s.deadlineLock.Unlock()
s.rDeadline = t
return nil
}
func (s *Stream) SetWriteDeadline(t time.Time) error {
s.deadlineLock.Lock()
defer s.deadlineLock.Unlock()
s.wDeadline = t
return nil
}