-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawconn.go
442 lines (404 loc) · 9.43 KB
/
rawconn.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package copper
import (
"bufio"
"fmt"
"net"
"sync"
"time"
)
const (
rawConnBufferSize = 4096
)
// RawConn is a multiplexed connection implementing the copper protocol
type RawConn interface {
Err() error
Close() error
Done() <-chan struct{}
Closed() <-chan struct{}
Shutdown() <-chan struct{}
LocalAddr() net.Addr
RemoteAddr() net.Addr
Ping(data PingData) <-chan error
NewStream() (stream Stream, err error)
}
type rawConn struct {
mu sync.RWMutex
conn net.Conn
handler Handler
failure error
closedchan chan struct{}
finishchan chan struct{}
finishgroup sync.WaitGroup
shutdown bool
shutdownchan chan struct{}
shutdowngroup sync.WaitGroup
readloopdone chan struct{}
pings rawConnPings
streams rawConnStreams
settings rawConnSettings
outgoing rawConnOutgoing
// only accessible from the readloop
br *bufio.Reader
reader *FrameReader
lastread time.Time
// only accessible from the writeloop
bw *bufio.Writer
writer *FrameWriter
lastwrite time.Time
}
var _ RawConn = &rawConn{}
// NewRawConn wraps the underlying network connection with the copper protocol
func NewRawConn(conn net.Conn, handler Handler, server bool) RawConn {
c := &rawConn{
conn: conn,
handler: handler,
closedchan: make(chan struct{}),
readloopdone: make(chan struct{}),
}
c.pings.init(c)
c.streams.init(c, server)
c.settings.init(c)
c.outgoing.init(c)
c.br = bufio.NewReaderSize(newRawConnReader(c), rawConnBufferSize)
c.reader = NewFrameReader(c.br)
c.lastread = time.Now()
c.bw = bufio.NewWriterSize(newRawConnWriter(c), rawConnBufferSize)
c.writer = NewFrameWriter(c.bw)
c.lastwrite = time.Now()
c.finishgroup.Add(2)
go c.readloop()
go c.writeloop()
return c
}
var debugPrefixByClientFlag = map[bool]string{
true: "client",
false: "server",
}
func (c *rawConn) closeWithError(err error) error {
if err == nil {
err = ECONNCLOSED
} else if isTimeout(err) {
err = &copperError{
error: err,
code: ECONNTIMEOUT,
}
}
c.mu.Lock()
preverror := c.failure
if c.failure == nil {
c.failure = err
close(c.closedchan)
c.outgoing.fail(err)
c.outgoing.clearPingQueue()
c.streams.failLocked(err)
}
c.mu.Unlock()
return preverror
}
func (c *rawConn) Err() error {
c.mu.RLock()
err := c.failure
c.mu.RUnlock()
return err
}
func (c *rawConn) Close() error {
return c.closeWithError(ECONNCLOSED)
}
func (c *rawConn) Done() <-chan struct{} {
c.mu.Lock()
if c.finishchan == nil {
c.finishchan = make(chan struct{})
go func() {
defer close(c.finishchan)
c.finishgroup.Wait()
}()
}
c.mu.Unlock()
return c.finishchan
}
func (c *rawConn) Closed() <-chan struct{} {
return c.closedchan
}
func (c *rawConn) Shutdown() <-chan struct{} {
c.mu.Lock()
if c.shutdownchan == nil {
c.shutdownchan = make(chan struct{})
go func() {
defer close(c.shutdownchan)
c.shutdowngroup.Wait()
}()
}
c.mu.Unlock()
return c.shutdownchan
}
func (c *rawConn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
func (c *rawConn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *rawConn) Ping(data PingData) <-chan error {
result := make(chan error, 1)
c.ping(data, func(err error) {
result <- err
close(result)
})
return result
}
func (c *rawConn) ping(data PingData, callback func(err error)) {
err := c.pings.addPing(data, callback)
if err != nil {
callback(err)
}
}
func (c *rawConn) NewStream() (Stream, error) {
c.mu.Lock()
streamID, err := c.streams.allocateLocked()
if err != nil {
c.mu.Unlock()
return nil, err
}
stream := newOutgoingStream(c, streamID)
c.mu.Unlock()
return stream, nil
}
func (c *rawConn) handleStream(handler Handler, stream Stream) {
defer c.finishgroup.Done()
defer c.shutdowngroup.Done()
if handler == nil {
// This connection does not support incoming streams
stream.CloseWithError(ENOTARGET)
return
}
defer stream.Close()
err := handler.ServeCopper(stream)
if err != nil {
stream.CloseWithError(err)
}
}
func (c *rawConn) processPingFrame(frame *PingFrame) error {
if frame.Flags.Has(FlagPingAck) {
c.pings.handleAck(frame.Data)
} else {
c.pings.handlePing(frame.Data)
}
return nil
}
func (c *rawConn) processDataFrame(frame *DataFrame) error {
if frame.StreamID == 0 {
if frame.Flags != 0 || len(frame.Data) != 0 {
return copperError{
error: fmt.Errorf("stream 0 cannot be used for data"),
code: EINVALIDSTREAM,
}
}
return nil
}
stream := c.streams.find(frame.StreamID)
if frame.Flags.Has(FlagDataOpen) {
// This frame is starting a new stream
if c.streams.isOwnedID(frame.StreamID) {
return copperError{
error: fmt.Errorf("stream 0x%08x cannot be used for opening streams", frame.StreamID),
code: EINVALIDSTREAM,
}
}
if stream != nil {
return copperError{
error: fmt.Errorf("stream 0x%08x cannot be reopened until fully closed", frame.StreamID),
code: EINVALIDSTREAM,
}
}
c.mu.Lock()
window := c.settings.localStreamWindowSize
if len(frame.Data) > window {
c.mu.Unlock()
return copperError{
error: fmt.Errorf("stream 0x%08x initial %d bytes, which is more than %d bytes window", frame.StreamID, len(frame.Data), window),
code: EWINDOWOVERFLOW,
}
}
stream = newIncomingStream(c, frame.StreamID)
if c.failure != nil {
// we are closed and ignore valid DATA frames
err := c.failure
c.mu.Unlock()
stream.CloseWithError(err)
return nil
}
if c.shutdownchan != nil {
// we are shutting down and should close incoming streams
c.mu.Unlock()
stream.CloseWithError(ECONNSHUTDOWN)
return nil
}
c.finishgroup.Add(1)
c.shutdowngroup.Add(1)
go c.handleStream(c.handler, stream)
c.mu.Unlock()
} else {
if stream == nil {
return copperError{
error: fmt.Errorf("stream 0x%08x cannot be found", frame.StreamID),
code: EINVALIDSTREAM,
}
}
c.mu.RLock()
if c.failure != nil {
// we are closed and ignore valid DATA frames
c.mu.RUnlock()
return nil
}
c.mu.RUnlock()
}
err := stream.processDataFrame(frame)
if err != nil {
return err
}
return nil
}
func (c *rawConn) processResetFrame(frame *ResetFrame) error {
if frame.StreamID == 0 {
// send ECONNCLOSED unless other error is pending
c.outgoing.fail(ECONNSHUTDOWN)
return frame.Error
}
stream := c.streams.find(frame.StreamID)
if stream == nil {
// it's ok to receive RESET for a dead stream
return nil
}
c.mu.RLock()
if c.failure != nil {
// we are closed and ignore valid RESET frames
c.mu.RUnlock()
return nil
}
c.mu.RUnlock()
err := stream.processResetFrame(frame)
if err != nil {
return err
}
return nil
}
func (c *rawConn) processWindowFrame(frame *WindowFrame) error {
stream := c.streams.find(frame.StreamID)
if stream == nil {
// it's ok to receive WINDOW for a dead stream
return nil
}
return stream.processWindowFrame(frame)
}
func (c *rawConn) processSettingsFrame(frame *SettingsFrame) error {
if frame.Flags.Has(FlagSettingsAck) {
c.settings.handleAck()
return nil
}
return c.settings.handleSettings(frame)
}
func (c *rawConn) processFrame(rawFrame Frame) bool {
var err error
switch frame := rawFrame.(type) {
case *PingFrame:
err = c.processPingFrame(frame)
case *DataFrame:
err = c.processDataFrame(frame)
case *ResetFrame:
err = c.processResetFrame(frame)
case *WindowFrame:
err = c.processWindowFrame(frame)
case *SettingsFrame:
err = c.processSettingsFrame(frame)
default:
err = EUNKNOWNFRAME
}
if err != nil {
c.closeWithError(err)
return false
}
return true
}
func (c *rawConn) readloop() {
defer close(c.readloopdone)
defer c.finishgroup.Done()
for {
rawFrame, err := c.reader.ReadFrame()
if err != nil {
c.closeWithError(err)
break
}
if !c.processFrame(rawFrame) {
break
}
}
err := c.Err()
c.pings.fail(err)
c.mu.Lock()
c.streams.failLocked(err)
c.settings.failLocked(err)
c.mu.Unlock()
}
func (c *rawConn) writeloop() {
defer c.finishgroup.Done()
defer func() {
// Wait up to 250ms for the read loop to finish on its own.
// This protects the remote from getting write failures instead of the
// error we intended it to receive (which might happen if we close the
// connection before the remote has a chance to receive our error).
grace := time.NewTimer(250 * time.Millisecond)
defer grace.Stop()
select {
case <-grace.C:
case <-c.readloopdone:
}
c.conn.Close()
}()
lastwrite := c.lastwrite
deadline := lastwrite.Add(c.settings.getRemoteInactivityTimeout() * 2 / 3)
t := time.AfterFunc(deadline.Sub(time.Now()), func() {
c.outgoing.wakeup()
})
defer t.Stop()
for {
ok := c.outgoing.writeFrames()
newlastwrite := c.lastwrite
if ok && lastwrite == newlastwrite && c.bw.Buffered() == 0 {
// We haven't written anything for a while, write an empty frame
err := c.writer.WriteData(0, 0, nil)
if err != nil {
c.closeWithError(err)
break
}
}
if c.bw.Buffered() > 0 {
err := c.bw.Flush()
if err != nil {
c.closeWithError(err)
break
}
}
if !ok {
break
}
newlastwrite = c.lastwrite
newdeadline := newlastwrite.Add(c.settings.getRemoteInactivityTimeout() * 2 / 3)
if newdeadline != deadline {
lastwrite = newlastwrite
deadline = newdeadline
t.Reset(deadline.Sub(time.Now()))
}
}
}
func (c *rawConn) blockWrite() {
c.outgoing.mu.Lock()
c.outgoing.blocked++
c.outgoing.mu.Unlock()
}
func (c *rawConn) unblockWrite() {
c.outgoing.mu.Lock()
c.outgoing.blocked--
if c.outgoing.blocked == 0 {
c.outgoing.unblocked.Broadcast()
}
c.outgoing.mu.Unlock()
}