-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmux_conn.go
330 lines (288 loc) · 6.86 KB
/
mux_conn.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
package smux
import (
"errors"
"io"
"net"
"sync"
"sync/atomic"
"time"
)
/*
只有在非堵塞模式下才有的返回错误
读情况下,没有数据可读;写情况下,不能写且已发送为0
*/
var ErrNonblock = errors.New("nonblock none. ")
type MuxConn struct {
ms *MuxSession
connID uint16
bufferRead uint32
buffer *Buffer
bufferLock sync.Mutex
readLock sync.Mutex
// notify a read/write event
chReadEvent chan struct{}
chWriteEvent chan struct{}
// 已发送待确认的字节数
waitConfirm uint32
writeLock sync.Mutex
// deadlines
readDeadline atomic.Value
writeDeadline atomic.Value
// 主动关闭调用 close
chClose chan struct{}
closeOnce sync.Once
// 被动关闭调用 fin
chFin chan struct{}
finOnce sync.Once
nonblock uint32 // 堵塞,默认堵塞
chNonblockEvent chan struct{}
}
func newMuxConn(cid uint16, ms *MuxSession) *MuxConn {
return &MuxConn{
ms: ms,
connID: cid,
buffer: NewBuffer(muxConnWindowSize),
bufferLock: sync.Mutex{},
chReadEvent: make(chan struct{}, 1),
chWriteEvent: make(chan struct{}, 1),
readDeadline: atomic.Value{},
writeDeadline: atomic.Value{},
chClose: make(chan struct{}),
closeOnce: sync.Once{},
chFin: make(chan struct{}),
finOnce: sync.Once{},
chNonblockEvent: make(chan struct{}, 1),
}
}
func (this *MuxConn) ID() uint16 {
return this.connID
}
func (this *MuxConn) SetNonblock(nonblocking bool) {
if nonblocking {
if atomic.CompareAndSwapUint32(&this.nonblock, 0, 1) {
notifyEvent(this.chNonblockEvent)
}
} else {
atomic.CompareAndSwapUint32(&this.nonblock, 1, 0)
}
}
// Readable returns length of read buffer
func (this *MuxConn) Readable() (int, bool) {
this.bufferLock.Lock()
defer this.bufferLock.Unlock()
return this.buffer.Len(), !this.buffer.Empty()
}
func (this *MuxConn) Read(b []byte) (n int, err error) {
select {
case <-this.chClose:
return 0, ErrClosedPipe
default:
}
this.readLock.Lock()
defer this.readLock.Unlock()
for {
if n, err = this.tryRead(b); err != nil {
return 0, err
} else if n == -1 {
if err = this.waitRead(); err != nil {
return 0, err
}
} else {
return
}
}
}
func (this *MuxConn) tryRead(b []byte) (n int, err error) {
if len(b) == 0 {
return
}
this.bufferLock.Lock()
defer this.bufferLock.Unlock()
if this.buffer.Empty() {
return -1, nil
} else {
n, err = this.buffer.Read(b)
this.bufferRead += uint32(n)
if this.bufferRead >= muxConnWindowSize/2 {
this.ms.writeHeader(cmdCFM, this.connID, this.bufferRead)
this.bufferRead = 0
}
return
}
}
func (this *MuxConn) waitRead() error {
var timer *time.Timer
var deadline <-chan time.Time
if d, ok := this.readDeadline.Load().(time.Time); ok && !d.IsZero() {
timer = time.NewTimer(time.Until(d))
defer timer.Stop()
deadline = timer.C
}
var nonblockC chan struct{}
if atomic.LoadUint32(&this.nonblock) == 1 {
nonblockC = make(chan struct{}, 1)
nonblockC <- struct{}{}
}
select {
case <-this.chReadEvent:
return nil
case <-this.chFin:
this.bufferLock.Lock()
defer this.bufferLock.Unlock()
if !this.buffer.Empty() {
return nil
}
return io.EOF
case <-this.chClose:
return ErrClosedPipe
case <-deadline:
return ErrTimeout
case <-this.ms.chClose:
return this.ms.closeReason.Load().(error)
case <-this.chNonblockEvent:
return nil
case <-nonblockC:
// 怎样降低优先级
return ErrNonblock
}
}
func (this *MuxConn) pushBytes(b []byte) {
select {
case <-this.chClose:
return
default:
}
this.bufferLock.Lock()
if this.buffer.Len()+len(b) > this.buffer.Cap() {
this.buffer.Grow(len(b))
}
_, _ = this.buffer.Write(b)
notifyEvent(this.chReadEvent)
this.bufferLock.Unlock()
}
// Writable returns write windows
func (this *MuxConn) Writable() (int, bool) {
writeWindows := muxConnWindowSize - atomic.LoadUint32(&this.waitConfirm)
return int(writeWindows), writeWindows > 0
}
func (this *MuxConn) Write(b []byte) (n int, err error) {
select {
case <-this.chClose:
return 0, ErrClosedPipe
case <-this.chFin:
return 0, ErrBrokenPipe
default:
}
this.writeLock.Lock()
defer this.writeLock.Unlock()
var timer *time.Timer
var deadline <-chan time.Time
if d, ok := this.writeDeadline.Load().(time.Time); ok && !d.IsZero() {
timer = time.NewTimer(time.Until(d))
defer timer.Stop()
deadline = timer.C
}
sentb := b
blen := len(sentb)
for n < blen {
wSize := muxConnWindowSize - atomic.LoadUint32(&this.waitConfirm)
if wSize <= 0 {
var nonblockC chan struct{}
if atomic.LoadUint32(&this.nonblock) == 1 {
nonblockC = make(chan struct{}, 1)
nonblockC <- struct{}{}
}
select {
case <-this.chWriteEvent:
case <-this.chFin:
return 0, ErrBrokenPipe
case <-deadline:
return n, ErrTimeout
case <-this.chClose:
return 0, ErrClosedPipe
case <-this.ms.chClose:
return 0, this.ms.closeReason.Load().(error)
case <-this.chNonblockEvent:
case <-nonblockC:
if n == 0 {
err = ErrNonblock
}
return
}
} else {
sz := blen - n
if sz > int(wSize) {
sz = int(wSize)
}
if sz > frameSize {
sz = frameSize
}
sendn, err := this.ms.writeData(this.connID, sentb[n:n+sz], deadline)
if sendn > 0 {
atomic.AddUint32(&this.waitConfirm, uint32(sendn))
n += sendn
}
if err != nil {
return n, err
}
}
}
return
}
func (this *MuxConn) bytesConfirm(count uint32) uint32 {
windows := muxConnWindowSize - atomic.AddUint32(&this.waitConfirm, -count)
notifyEvent(this.chWriteEvent)
return windows
}
func (this *MuxConn) Close() error {
var once bool
this.closeOnce.Do(func() {
close(this.chClose)
once = true
})
if once {
this.ms.closedMuxConn(this.connID)
return nil
} else {
return ErrClosedPipe
}
}
func (this *MuxConn) fin() {
this.finOnce.Do(func() {
close(this.chFin)
})
}
// SetReadDeadline sets the read deadline as defined by
// net.Conn.SetReadDeadline.
// A zero time value disables the deadline.
func (this *MuxConn) SetReadDeadline(t time.Time) error {
this.readDeadline.Store(t)
return nil
}
// SetWriteDeadline sets the write deadline as defined by
// net.Conn.SetWriteDeadline.
// A zero time value disables the deadline.
func (this *MuxConn) SetWriteDeadline(t time.Time) error {
this.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 (this *MuxConn) SetDeadline(t time.Time) error {
if err := this.SetReadDeadline(t); err != nil {
return err
}
if err := this.SetWriteDeadline(t); err != nil {
return err
}
return nil
}
// LocalAddr satisfies net.Conn interface
func (this *MuxConn) LocalAddr() net.Addr {
return this.ms.conn.LocalAddr()
}
// RemoteAddr satisfies net.Conn interface
func (this *MuxConn) RemoteAddr() net.Addr {
return this.ms.conn.RemoteAddr()
}