forked from jpillora/chisel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
48 lines (42 loc) · 739 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
package cio
import (
"io"
"log"
"sync"
)
func Pipe(src io.ReadWriteCloser, dst io.ReadWriteCloser) (int64, int64) {
var sent, received int64
var wg sync.WaitGroup
var o sync.Once
close := func() {
src.Close()
dst.Close()
}
wg.Add(2)
go func() {
received, _ = io.Copy(src, dst)
o.Do(close)
wg.Done()
}()
go func() {
sent, _ = io.Copy(dst, src)
o.Do(close)
wg.Done()
}()
wg.Wait()
return sent, received
}
const vis = false
type pipeVisPrinter struct {
name string
}
func (p pipeVisPrinter) Write(b []byte) (int, error) {
log.Printf(">>> %s: %x", p.name, b)
return len(b), nil
}
func pipeVis(name string, r io.Reader) io.Reader {
if vis {
return io.TeeReader(r, pipeVisPrinter{name})
}
return r
}