-
Notifications
You must be signed in to change notification settings - Fork 13
/
pipe.go
68 lines (51 loc) · 1015 Bytes
/
pipe.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
package spdy
import (
"io"
)
func Pipe(buffer int) (*PipeReader, *PipeWriter) {
p := &pipe{ch: make(chan Frame, buffer)}
return &PipeReader{pipe: p}, &PipeWriter{pipe: p}
}
type pipe struct {
ch chan Frame
err error
}
type PipeReader struct {
*pipe
NFrames int
}
type PipeWriter struct {
*pipe
NFrames int
}
func (p *pipe) CloseWithError(err error) error {
if p.err != nil {
return nil
}
p.err = err
close(p.ch)
return nil
}
func (writer *PipeWriter) WriteFrame(frame Frame) error {
if writer.err != nil {
return writer.err
}
writer.ch <- frame
writer.NFrames += 1
return nil
}
func (writer *PipeWriter) Close() error {
return writer.CloseWithError(io.EOF)
}
func (reader *PipeReader) ReadFrame() (Frame, error) {
/* This will not block if the channel is closed and empty */
frame, ok := <-reader.ch
if !ok {
return nil, reader.err
}
reader.NFrames += 1
return frame, nil
}
func (reader *PipeReader) Close() error {
return reader.CloseWithError(io.ErrClosedPipe)
}