forked from xtaci/smux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
388 lines (339 loc) · 8.28 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package smux
import (
"bytes"
"io"
"net"
"sync"
"sync/atomic"
"time"
)
// Stream implements net.Conn
type Stream struct {
id uint32
rstflag int32
sess *Session
buffer bytes.Buffer
bufferLock sync.Mutex
frameSize int
chReadEvent chan struct{} // notify a read event
die chan struct{} // flag the stream has closed
dieLock sync.Mutex
readDeadline atomic.Value
writeDeadline atomic.Value
writeLock sync.Mutex
bucket int32 // token bucket
bucketNotify chan struct{} // used for waiting for tokens
fulflag int32
empflagLock sync.Mutex
empflag int32
countRead int32 // for guess read speed
boostTimeout time.Time // for initial boost
guessBucket int32 // for guess needed stream buffer size
lastWrite atomic.Value
guessNeeded int32
}
// newStream initiates a Stream struct
func newStream(id uint32, frameSize int, sess *Session) *Stream {
s := new(Stream)
s.id = id
s.chReadEvent = make(chan struct{}, 1)
s.frameSize = frameSize
s.sess = sess
s.die = make(chan struct{})
s.bucket = int32(0)
s.bucketNotify = make(chan struct{}, 1)
s.empflag = int32(1)
s.countRead = int32(0)
s.boostTimeout = time.Now().Add(s.sess.config.BoostTimeout)
s.guessBucket = int32(s.sess.config.MaxStreamBuffer)
s.lastWrite.Store(time.Now())
return s
}
// ID returns the unique stream ID.
func (s *Stream) ID() uint32 {
return s.id
}
// Read implements net.Conn
func (s *Stream) Read(b []byte) (n int, err error) {
if len(b) == 0 {
select {
case <-s.die:
return 0, ErrBrokenPipe
default:
return 0, nil
}
}
var deadline <-chan time.Time
if d, ok := s.readDeadline.Load().(time.Time); ok && !d.IsZero() {
timer := time.NewTimer(time.Until(d))
defer timer.Stop()
deadline = timer.C
}
READ:
s.bufferLock.Lock()
n, _ = s.buffer.Read(b)
s.bufferLock.Unlock()
if n > 0 {
s.sess.returnTokens(n)
s.returnTokens(n)
return n, nil
} else if atomic.LoadInt32(&s.rstflag) == 1 {
_ = s.Close()
return 0, io.EOF
}
if s.sess.config.EnableStreamBuffer {
s.sendResume()
}
select {
case <-s.chReadEvent:
goto READ
case <-deadline:
return n, errTimeout
case <-s.die:
return 0, ErrBrokenPipe
}
}
// Write implements net.Conn
func (s *Stream) Write(b []byte) (n int, err error) {
var deadline <-chan time.Time
if d, ok := s.writeDeadline.Load().(time.Time); ok && !d.IsZero() {
timer := time.NewTimer(time.Until(d))
defer timer.Stop()
deadline = timer.C
}
select {
case <-s.die:
return 0, ErrBrokenPipe
default:
}
if atomic.LoadInt32(&s.fulflag) == 1 {
select {
case <-s.bucketNotify:
case <-s.die:
return 0, ErrBrokenPipe
case <-deadline:
return 0, errTimeout
}
}
frames := s.split(b, cmdPSH, s.id)
sent := 0
s.writeLock.Lock()
defer s.writeLock.Unlock()
for k := range frames {
req := writeRequest{
frame: frames[k],
result: make(chan writeResult, 1),
}
select {
case s.sess.writes <- req:
case <-s.die:
return sent, ErrBrokenPipe
case <-deadline:
return sent, errTimeout
}
select {
case result := <-req.result:
sent += result.n
if result.err != nil {
return sent, result.err
}
case <-s.die:
return sent, ErrBrokenPipe
case <-deadline:
return sent, errTimeout
}
}
return sent, nil
}
// Close implements net.Conn
func (s *Stream) Close() error {
s.dieLock.Lock()
select {
case <-s.die:
s.dieLock.Unlock()
if atomic.LoadInt32(&s.rstflag) == 1 {
return nil
}
return ErrBrokenPipe
default:
close(s.die)
s.dieLock.Unlock()
s.sess.streamClosed(s)
s.writeLock.Lock()
_, err := s.sess.writeFrameInternal(newFrame(cmdFIN, s.id), nil)
s.writeLock.Unlock()
return err
}
}
// GetDieCh returns a readonly chan which can be readable
// when the stream is to be closed.
func (s *Stream) GetDieCh() <-chan struct{} {
return s.die
}
// SetReadDeadline sets the read deadline as defined by
// net.Conn.SetReadDeadline.
// A zero time value disables the deadline.
func (s *Stream) SetReadDeadline(t time.Time) error {
s.readDeadline.Store(t)
return nil
}
// SetWriteDeadline sets the write deadline as defined by
// net.Conn.SetWriteDeadline.
// A zero time value disables the deadline.
func (s *Stream) SetWriteDeadline(t time.Time) error {
s.writeDeadline.Store(t)
return nil
}
// SetDeadline sets both read and write deadlines as defined by
// net.Conn.SetDeadline.
// A zero time value disables the deadlines.
func (s *Stream) SetDeadline(t time.Time) error {
if err := s.SetReadDeadline(t); err != nil {
return err
}
if err := s.SetWriteDeadline(t); err != nil {
return err
}
return nil
}
// session closes the stream
func (s *Stream) sessionClose() {
s.dieLock.Lock()
defer s.dieLock.Unlock()
select {
case <-s.die:
default:
close(s.die)
}
}
// LocalAddr satisfies net.Conn interface
func (s *Stream) LocalAddr() net.Addr {
if ts, ok := s.sess.conn.(interface {
LocalAddr() net.Addr
}); ok {
return ts.LocalAddr()
}
return nil
}
// RemoteAddr satisfies net.Conn interface
func (s *Stream) RemoteAddr() net.Addr {
if ts, ok := s.sess.conn.(interface {
RemoteAddr() net.Addr
}); ok {
return ts.RemoteAddr()
}
return nil
}
// pushBytes a slice into buffer
func (s *Stream) pushBytes(p []byte) {
s.bufferLock.Lock()
s.buffer.Write(p)
s.bufferLock.Unlock()
if !s.sess.config.EnableStreamBuffer {
return
}
n := len(p)
used := atomic.AddInt32(&s.bucket, int32(n))
lastReadOut := atomic.SwapInt32(&s.countRead, int32(0)) // reset read
// hard limit
if used > int32(s.sess.config.MaxStreamBuffer) {
s.sendPause()
return
}
if lastReadOut != 0 {
s.lastWrite.Store(time.Now())
needed := atomic.LoadInt32(&s.guessNeeded)
s.guessBucket = int32((int64(s.guessBucket) * 8 + int64(needed) * 2) / 10)
}
if used <= s.guessBucket {
s.boostTimeout = time.Now().Add(s.sess.config.BoostTimeout)
return
}
if time.Now().After(s.boostTimeout) {
s.sendPause()
}
}
// recycleTokens transform remaining bytes to tokens(will truncate buffer)
func (s *Stream) recycleTokens() (n int) {
s.bufferLock.Lock()
n = s.buffer.Len()
s.buffer.Reset()
s.bufferLock.Unlock()
return
}
// split large byte buffer into smaller frames, reference only
func (s *Stream) split(bts []byte, cmd byte, sid uint32) []Frame {
frames := make([]Frame, 0, len(bts)/s.frameSize+1)
for len(bts) > s.frameSize {
frame := newFrame(cmd, sid)
frame.data = bts[:s.frameSize]
bts = bts[s.frameSize:]
frames = append(frames, frame)
}
if len(bts) > 0 {
frame := newFrame(cmd, sid)
frame.data = bts
frames = append(frames, frame)
}
return frames
}
// notify read event
func (s *Stream) notifyReadEvent() {
select {
case s.chReadEvent <- struct{}{}:
default:
}
}
// mark this stream has been reset
func (s *Stream) markRST() {
atomic.StoreInt32(&s.rstflag, 1)
}
// mark this stream has been pause write
func (s *Stream) pauseWrite() {
atomic.StoreInt32(&s.fulflag, 1)
}
// mark this stream has been resume write
func (s *Stream) resumeWrite() {
atomic.StoreInt32(&s.fulflag, 0)
select {
case s.bucketNotify <- struct{}{}:
default:
}
}
// returnTokens is called by stream to return token after read
func (s *Stream) returnTokens(n int) {
if !s.sess.config.EnableStreamBuffer {
return
}
used := atomic.AddInt32(&s.bucket, -int32(n))
totalRead := atomic.AddInt32(&s.countRead, int32(n))
lastWrite, _ := s.lastWrite.Load().(time.Time)
dt := time.Now().Sub(lastWrite) + 1
rtt := s.sess.rtt.Load().(time.Duration)
needed := totalRead * int32(rtt / dt)
atomic.StoreInt32(&s.guessNeeded, needed)
if used <= 0 || needed >= used {
s.sendResume()
}
}
// send cmdFUL to pause write
func (s *Stream) sendPause() {
s.empflagLock.Lock()
if atomic.SwapInt32(&s.empflag, 0) == 1 {
s.sess.writeFrameCtrl(newFrame(cmdFUL, s.id), time.After(s.sess.config.KeepAliveTimeout))
}
s.empflagLock.Unlock()
}
// send cmdEMP to resume write
func (s *Stream) sendResume() {
s.empflagLock.Lock()
if atomic.SwapInt32(&s.empflag, 1) == 0 {
s.sess.writeFrameHalf(newFrame(cmdEMP, s.id), time.After(s.sess.config.KeepAliveTimeout))
}
s.empflagLock.Unlock()
}
var errTimeout error = &timeoutError{}
type timeoutError struct{}
func (e *timeoutError) Error() string { return "i/o timeout" }
func (e *timeoutError) Timeout() bool { return true }
func (e *timeoutError) Temporary() bool { return true }