mirror of
https://github.com/wweir/sower.git
synced 2024-04-21 12:42:15 +00:00
38 lines
513 B
Go
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
|
|
}
|