Files
sower/parse/tee_conn.go
T
2019-01-06 12:17:01 +08:00

38 lines
513 B
Go

package parse
import (
"net"
)
type TeeConn struct {
net.Conn
buf []byte
offset int
tee bool // read
}
func (t *TeeConn) StartOrReset() {
t.offset = 0
t.tee = true
}
func (t *TeeConn) Stop() {
t.offset = 0
t.tee = false
}
func (t *TeeConn) Read(b []byte) (n int, err error) {
length := len(t.buf) - t.offset
if length > 0 {
n = copy(b, t.buf[t.offset:])
t.offset += n
return
}
n, err = t.Conn.Read(b)
if t.tee {
t.buf = append(t.buf, b[:n]...)
t.offset += n
}
return n, err
}