forked from ekr/minq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connbuffer.go
62 lines (51 loc) · 1.25 KB
/
connbuffer.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
package minq
import (
"bytes"
"io"
"net"
"time"
)
type connBuffer struct {
r *bytes.Buffer
w *bytes.Buffer
}
func (p *connBuffer) Read(data []byte) (n int, err error) {
logf(logTypeConnBuffer, "Reading %v", n)
n, err = p.r.Read(data)
// Suppress bytes.Buffer's EOF on an empty buffer
if err == io.EOF {
err = nil
}
return
}
func (p *connBuffer) Write(data []byte) (n int, err error) {
logf(logTypeConnBuffer, "Writing %v", n)
return p.w.Write(data)
}
func (p *connBuffer) Close() error {
return nil
}
func (p *connBuffer) LocalAddr() net.Addr { return nil }
func (p *connBuffer) RemoteAddr() net.Addr { return nil }
func (p *connBuffer) SetDeadline(t time.Time) error { return nil }
func (p *connBuffer) SetReadDeadline(t time.Time) error { return nil }
func (p *connBuffer) SetWriteDeadline(t time.Time) error { return nil }
func newConnBuffer() *connBuffer {
return &connBuffer{
bytes.NewBuffer(nil),
bytes.NewBuffer(nil),
}
}
func (p *connBuffer) input(data []byte) error {
logf(logTypeConnBuffer, "input %v", len(data))
_, err := p.r.Write(data)
return err
}
func (p *connBuffer) getOutput() []byte {
b := p.w.Bytes()
p.w.Reset()
return b
}
func (p *connBuffer) OutputLen() int {
return p.w.Len()
}